aimcal-core 0.12.1

AIM - Analyze. Interact. Manage Your Time, with calendar support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// SPDX-FileCopyrightText: 2025-2026 Zexin Yuan <aim@yzx9.xyz>
//
// SPDX-License-Identifier: Apache-2.0

#![allow(
    clippy::indexing_slicing,
    clippy::format_push_string,
    clippy::missing_panics_doc
)]

use std::collections::HashMap;
use std::error::Error;
use std::path::{Path, PathBuf};

use crate::{DateTimeAnchor, Priority};
use aimcal_caldav::AuthMethod;

/// The name of the AIM application.
pub const APP_NAME: &str = "aim";

fn default_timeout_secs() -> u64 {
    30
}

fn default_user_agent() -> String {
    "aimcal/0.11.0".to_string()
}

/// Store definition for shared connection configuration.
///
/// Stores define how to connect to a calendar storage. Multiple calendars
/// can reference the same store, avoiding duplication of connection details.
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(tag = "type")]
pub enum StoreDef {
    /// Local ICS file store.
    #[serde(rename = "local")]
    Local {
        /// Path to the calendar directory for ICS files.
        calendar_path: Option<String>,
    },
    /// `CalDAV` server store.
    #[serde(rename = "caldav")]
    Caldav {
        /// Base URL of the `CalDAV` server.
        base_url: String,
        /// Calendar home path on the server.
        calendar_home: String,
        /// Authentication method.
        auth: AuthMethod,
        /// Request timeout in seconds.
        #[serde(default = "default_timeout_secs")]
        timeout_secs: u64,
        /// User agent string for HTTP requests.
        #[serde(default = "default_user_agent")]
        user_agent: String,
    },
}

/// Calendar entry in the TOML configuration.
///
/// Each calendar references a store by ID and provides calendar-specific fields
/// such as the calendar href (for `CalDAV`) or an optional path override (for local).
#[derive(Debug, Clone, serde::Deserialize)]
pub struct CalendarEntry {
    /// Unique calendar identifier.
    pub id: String,
    /// Display name for the calendar.
    pub name: String,
    /// Reference to a store definition in `stores`.
    pub store: String,
    /// Href of the calendar collection on the server (required for caldav stores).
    pub calendar_href: Option<String>,
    /// Path to the calendar directory (optional override for local backends).
    pub calendar_path: Option<String>,
    /// Priority for conflict resolution and display ordering.
    #[serde(default)]
    pub priority: i32,
    /// Whether the calendar is enabled.
    #[serde(default = "default_enabled")]
    pub enabled: bool,
}

fn default_enabled() -> bool {
    true
}

/// Configuration for the AIM application.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct Config {
    /// Path to the calendar directory (optional ICS export/import).
    ///
    /// Deprecated: Use `calendars` array instead.
    ///
    /// If set, AIM will sync events/todos with ICS files in this directory.
    /// If not set, AIM will only use the `Db` for storage.
    #[serde(default)]
    pub calendar_path: Option<PathBuf>,

    /// Directory for storing application state.
    #[serde(default)]
    pub state_dir: Option<PathBuf>,

    /// Default due time for new tasks.
    #[serde(default)]
    pub default_due: Option<DateTimeAnchor>,

    /// Default priority for new tasks.
    #[serde(default)]
    pub default_priority: Priority,

    /// If true, items with no priority will be listed first.
    #[serde(default)]
    pub default_priority_none_fist: bool,

    /// Parent directory of the config file.
    ///
    /// Set by the CLI layer after parsing. Used by `normalize()` to resolve
    /// relative paths against the config file location rather than the CWD.
    #[serde(skip)]
    pub config_dir: Option<PathBuf>,

    /// Whether development mode is active.
    ///
    /// When true, `normalize()` enforces explicit `state_dir` configuration
    /// to prevent accidental use of the system default state directory.
    #[serde(skip)]
    pub dev_mode: bool,

    /// Store definitions — shared connection configurations keyed by name.
    #[serde(default)]
    pub stores: HashMap<String, StoreDef>,

    /// Multi-calendar configuration.
    ///
    /// If present, each entry references a store from `stores`.
    #[serde(default)]
    pub calendars: Vec<CalendarEntry>,

    /// Default calendar ID for new items.
    #[serde(default = "default_calendar_id")]
    pub default_calendar: String,
}

fn default_calendar_id() -> String {
    "default".to_string()
}

impl Config {
    /// Normalize the configuration.
    ///
    /// # Errors
    /// If path normalization fails.
    #[tracing::instrument(skip(self))]
    pub fn normalize(&mut self) -> Result<(), Box<dyn Error>> {
        let config_parent = self.config_dir.as_deref();

        // Normalize calendar path if set (legacy format)
        if let Some(ref calendar_path) = self.calendar_path {
            self.calendar_path = Some(expand_path(calendar_path, config_parent)?);
        }

        // Normalize state directory
        if let Some(a) = &self.state_dir {
            let state_dir = expand_path(a, config_parent)
                .map_err(|e| format!("Failed to expand state directory path: {e}"))?;
            self.state_dir = Some(state_dir);
        } else {
            if self.dev_mode {
                return Err(
                    "Development mode requires state_dir to be explicitly configured".into(),
                );
            }
            match get_state_dir() {
                Ok(a) => self.state_dir = Some(a.join(APP_NAME)),
                Err(err) => tracing::warn!(err, "failed to get state directory"),
            }
        }

        // Normalize calendar paths for multi-calendar configuration
        for i in 0..self.calendars.len() {
            let calendar = self.calendars.get(i).unwrap();
            let store_def = self.stores.get(&calendar.store);

            // Only resolve calendar_path for local stores
            let calendar_path =
                if matches!(store_def, Some(StoreDef::Local { .. })) || calendar.store == "local" {
                    if let Some(ref path) = calendar.calendar_path {
                        let p = expand_path(&PathBuf::from(path), None)
                            .map_err(|e| {
                                format!("Failed to expand calendar path for {}: {e}", calendar.id)
                            })?
                            .to_string_lossy()
                            .to_string();
                        Some(p)
                    } else if let Some(ref state_dir) = self.state_dir {
                        let p = state_dir
                            .join("calendar")
                            .join(&calendar.id)
                            .to_string_lossy()
                            .to_string();
                        Some(p)
                    } else {
                        calendar.calendar_path.clone()
                    }
                } else {
                    calendar.calendar_path.clone()
                };

            self.calendars[i] = CalendarEntry {
                id: calendar.id.clone(),
                name: calendar.name.clone(),
                store: calendar.store.clone(),
                calendar_href: calendar.calendar_href.clone(),
                calendar_path,
                priority: calendar.priority,
                enabled: calendar.enabled,
            };
        }

        Ok(())
    }

    /// Check if the configuration uses legacy single-calendar format.
    ///
    /// Returns `true` if `calendars` array is empty (legacy or default mode).
    #[must_use]
    pub fn is_legacy_format(&self) -> bool {
        // If calendars array is present and not empty, it's not legacy
        self.calendars.is_empty()
    }

    /// Generate a warning message for legacy configuration.
    ///
    /// This provides helpful guidance to users about migrating to multi-calendar format.
    #[must_use]
    pub fn legacy_warning(&self) -> String {
        if !self.calendars.is_empty() {
            return String::new();
        }

        let mut warning =
            String::from("Warning: Using legacy single-calendar configuration format.\n\n");

        if self.calendar_path.is_some() {
            warning += "Legacy 'calendar_path' is detected.\n";
        }

        warning +=
            "The multi-calendar feature requires updating to the 'calendars' array format.\n\n";
        warning += "Please update your aim.toml to use the following format:\n\n";
        warning += "[stores.local]\n";
        warning += "type = \"local\"\n\n";
        warning += "[[calendars]]\n";
        warning += "id = \"default\"\n";
        warning += "name = \"Default\"\n";
        warning += "store = \"local\"\n";
        warning += "priority = 0\n";
        warning += "enabled = true\n";

        if let Some(ref path) = self.calendar_path {
            warning.push_str("\n# Your existing path can be used as:\n");
            warning.push_str(&format!("calendar_path = \"{}\"\n", path.display()));
        }

        warning += "\nSee documentation for full migration guide.\n";
        warning
    }

    /// Get all enabled calendars ordered by priority.
    #[must_use]
    pub fn enabled_calendars(&self) -> Vec<&CalendarEntry> {
        let mut calendars: Vec<_> = self.calendars.iter().filter(|c| c.enabled).collect();
        calendars.sort_by_key(|c| c.priority);
        calendars
    }

    /// Resolve the store definition for a calendar entry.
    ///
    /// Returns `None` if the calendar is not found or the store reference is invalid.
    #[must_use]
    pub fn resolve_store(&self, calendar_id: &str) -> Option<(&CalendarEntry, &StoreDef)> {
        let entry = self.calendars.iter().find(|c| c.id == calendar_id)?;
        let store = self.stores.get(&entry.store)?;
        Some((entry, store))
    }

    /// Get a specific calendar entry by ID.
    #[must_use]
    pub fn get_calendar(&self, id: &str) -> Option<&CalendarEntry> {
        self.calendars.iter().find(|c| c.id == id)
    }
}

/// Handle tilde (~) and environment variables in the path.
///
/// Relative paths that don't match any special prefix are resolved against
/// `config_parent` when provided, or returned as-is otherwise.
fn expand_path(path: &Path, config_parent: Option<&Path>) -> Result<PathBuf, Box<dyn Error>> {
    if path.is_absolute() {
        return Ok(path.to_owned());
    }

    let path = path.to_str().ok_or("Invalid path")?;

    // Handle tilde and home directory
    let home_prefixes: &[&str] = if cfg!(unix) {
        &["~/", "$HOME/", "${HOME}/"]
    } else {
        &[r"~\", "~/", r"%UserProfile%\", r"%UserProfile%/"]
    };

    for prefix in home_prefixes {
        if let Some(stripped) = path.strip_prefix(prefix) {
            return Ok(get_home_dir()?.join(stripped));
        }
    }

    // Handle config directories
    let config_prefixes: &[&str] = if cfg!(unix) {
        &["$XDG_CONFIG_HOME/", "${XDG_CONFIG_HOME}/"]
    } else {
        &[r"%LOCALAPPDATA%\", "%LOCALAPPDATA%"]
    };

    for prefix in config_prefixes {
        if let Some(stripped) = path.strip_prefix(prefix) {
            return Ok(get_config_dir()?.join(stripped));
        }
    }

    match config_parent {
        Some(parent) => Ok(parent.join(path)),
        None => Ok(path.into()),
    }
}

fn get_home_dir() -> Result<PathBuf, Box<dyn Error>> {
    dirs::home_dir().ok_or_else(|| "User-specific home directory not found".into())
}

fn get_config_dir() -> Result<PathBuf, Box<dyn Error>> {
    #[cfg(unix)]
    let config_dir = xdg::BaseDirectories::new().get_config_home();
    #[cfg(windows)]
    let config_dir = dirs::config_dir();

    config_dir.ok_or_else(|| "User-specific home directory not found".into())
}

fn get_state_dir() -> Result<PathBuf, Box<dyn Error>> {
    #[cfg(unix)]
    let state_dir = xdg::BaseDirectories::new().get_state_home();
    #[cfg(windows)]
    let state_dir = dirs::data_dir();

    state_dir.ok_or_else(|| "User-specific state directory not found".into())
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;

    #[test]
    fn parses_full_toml_config() {
        const TOML: &str = r#"
calendar_path = "calendar"
state_dir = "state"
default_due = "1d"
default_priority = "high"
default_priority_none_fist = true
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        assert_eq!(config.calendar_path, Some(PathBuf::from("calendar")));
        assert_eq!(config.state_dir, Some(PathBuf::from("state")));
        assert_eq!(config.default_due, Some(DateTimeAnchor::InDays(1)));
        assert_eq!(config.default_priority, Priority::P2);
        assert!(config.default_priority_none_fist);
    }

    #[test]
    #[allow(clippy::needless_raw_string_hashes)]
    fn parses_minimal_toml_with_defaults() {
        const TOML: &str = r#"
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        assert_eq!(config.calendar_path, None);
        assert_eq!(config.state_dir, None);
        assert_eq!(config.default_due, None);
        assert_eq!(config.default_priority, Priority::None);
        assert!(!config.default_priority_none_fist);
    }

    #[test]
    fn expands_path_with_home_env_vars() {
        let home = get_home_dir().unwrap();
        let home_prefixes: &[&str] = if cfg!(unix) {
            &["~", "$HOME", "${HOME}"]
        } else {
            &[r"~", r"%UserProfile%", r"%UserProfile%"]
        };

        for prefix in home_prefixes {
            let result = expand_path(&PathBuf::from(format!("{prefix}/Documents")), None).unwrap();
            assert_eq!(result, home.join("Documents"));
            assert!(result.is_absolute());
        }
    }

    #[test]
    fn expands_path_with_config_env_vars() {
        let config_dir = get_config_dir().unwrap();
        let config_prefixes: &[&str] = if cfg!(unix) {
            &["$XDG_CONFIG_HOME", "${XDG_CONFIG_HOME}"]
        } else {
            &[r"%LOCALAPPDATA%", "%LOCALAPPDATA%"]
        };

        for prefix in config_prefixes {
            let result =
                expand_path(&PathBuf::from(format!("{prefix}/config.toml")), None).unwrap();
            assert_eq!(result, config_dir.join("config.toml"));
            assert!(result.is_absolute());
        }
    }

    #[test]
    fn preserves_absolute_path() {
        let absolute_path = PathBuf::from("/etc/passwd");
        let result = expand_path(&absolute_path, None).unwrap();
        assert_eq!(result, absolute_path);
    }

    #[test]
    fn preserves_relative_path_without_config_parent() {
        let relative_path = PathBuf::from("relative/path/to/file");
        let result = expand_path(&relative_path, None).unwrap();
        assert_eq!(result, relative_path);
    }

    #[test]
    fn resolves_relative_path_against_config_parent() {
        let relative_path = PathBuf::from("relative/path/to/file");
        let config_parent = PathBuf::from("/etc/aim");

        let result = expand_path(&relative_path, Some(&config_parent)).unwrap();
        assert_eq!(result, PathBuf::from("/etc/aim/relative/path/to/file"));
    }

    #[test]
    fn parses_datetime_anchor_with_suffix_format() {
        // TODO: compatibility test, remove after v0.10.0
        assert_eq!(
            DateTimeAnchor::from_str("1d").unwrap(),
            DateTimeAnchor::InDays(1)
        );
        assert_eq!(
            DateTimeAnchor::from_str("2h").unwrap(),
            DateTimeAnchor::Relative(2 * 60 * 60)
        );
        assert_eq!(
            DateTimeAnchor::from_str("45m").unwrap(),
            DateTimeAnchor::Relative(45 * 60)
        );
        assert_eq!(
            DateTimeAnchor::from_str("1800s").unwrap(),
            DateTimeAnchor::Relative(1800)
        );
    }

    #[test]
    fn parses_multi_calendar_config() {
        const TOML: &str = r#"
default_calendar = "personal"

[stores.mylocal]
type = "local"

[stores.radicale]
type = "caldav"
base_url = "https://caldav.example.com"
calendar_home = "/dav/calendars/user/"
auth = { type = "basic", username = "user", password = "pass" }
timeout_secs = 30
user_agent = "aimcal/0.11.0"

[[calendars]]
id = "personal"
name = "Personal"
store ="mylocal"
priority = 0
enabled = true
calendar_path = "~/personal"

[[calendars]]
id = "work"
name = "Work"
store ="radicale"
priority = 1
enabled = true
calendar_href = "/dav/calendars/user/work/"
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        assert_eq!(config.calendars.len(), 2);
        assert_eq!(config.stores.len(), 2);

        assert_eq!(config.calendars[0].id, "personal");
        assert_eq!(config.calendars[0].name, "Personal");
        assert_eq!(config.calendars[0].store, "mylocal");
        assert_eq!(config.calendars[0].priority, 0);
        assert!(config.calendars[0].enabled);

        assert_eq!(config.calendars[1].id, "work");
        assert_eq!(config.calendars[1].name, "Work");
        assert_eq!(config.calendars[1].store, "radicale");
        assert_eq!(
            config.calendars[1].calendar_href,
            Some("/dav/calendars/user/work/".to_string())
        );
        assert_eq!(config.calendars[1].priority, 1);
        assert!(config.calendars[1].enabled);

        assert_eq!(config.default_calendar, "personal");

        // Verify backends
        assert!(matches!(
            config.stores.get("mylocal"),
            Some(StoreDef::Local { .. })
        ));
        assert!(matches!(
            config.stores.get("radicale"),
            Some(StoreDef::Caldav { .. })
        ));
    }

    #[test]
    fn is_legacy_format_detects_legacy_config() {
        const TOML: &str = r#"
calendar_path = "calendar"
state_dir = "state"
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        assert!(config.is_legacy_format());
    }

    #[test]
    fn is_legacy_format_returns_false_for_multi_calendar() {
        const TOML: &str = r#"
[stores.local]
type = "local"

[[calendars]]
id = "personal"
name = "Personal"
store ="local"
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        assert!(!config.is_legacy_format());
    }

    #[test]
    fn legacy_warning_provides_helpful_message() {
        const TOML: &str = r#"
calendar_path = "calendar"
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        let warning = config.legacy_warning();

        assert!(warning.contains("Warning: Using legacy single-calendar configuration"));
        assert!(warning.contains("calendars"));
        assert!(warning.contains("id = \"default\""));
    }

    #[test]
    fn enabled_calendars_returns_enabled_only() {
        const TOML: &str = r#"
[stores.local]
type = "local"

[[calendars]]
id = "personal"
name = "Personal"
store ="local"
enabled = true

[[calendars]]
id = "work"
name = "Work"
store ="local"
enabled = false

[[calendars]]
id = "archive"
name = "Archive"
store ="local"
enabled = true
priority = 5
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        let enabled = config.enabled_calendars();

        assert_eq!(enabled.len(), 2);
        assert!(
            enabled
                .iter()
                .all(|c| c.id == "personal" || c.id == "archive")
        );
        assert!(!enabled.iter().any(|c| c.id == "work"));
    }

    #[test]
    fn resolve_store_returns_entry_and_def() {
        const TOML: &str = r#"
[stores.radicale]
type = "caldav"
base_url = "https://caldav.example.com"
calendar_home = "/dav/"
auth = { type = "basic", username = "u", password = "p" }

[[calendars]]
id = "work"
name = "Work"
store ="radicale"
calendar_href = "/dav/work/"
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        let (entry, backend) = config.resolve_store("work").unwrap();
        assert_eq!(entry.id, "work");
        assert!(matches!(backend, StoreDef::Caldav { .. }));
        assert!(config.resolve_store("nonexistent").is_none());
    }

    #[test]
    fn get_calendar_returns_entry_by_id() {
        const TOML: &str = r#"
[stores.local]
type = "local"

[[calendars]]
id = "personal"
name = "Personal"
store ="local"
calendar_path = "~/personal"
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        let personal = config.get_calendar("personal");
        assert!(personal.is_some());
        assert_eq!(
            personal.unwrap().calendar_path.as_deref(),
            Some("~/personal")
        );

        assert!(config.get_calendar("work").is_none());
    }

    #[test]
    fn multiple_calendars_share_backend() {
        const TOML: &str = r#"
default_calendar = "home"

[stores.radicale]
type = "caldav"
base_url = "https://caldav.example.com/"
calendar_home = "/user/"
auth = { type = "basic", username = "u", password = "p" }

[[calendars]]
id = "home"
name = "Home"
store ="radicale"
calendar_href = "/user/home/"
priority = 0

[[calendars]]
id = "work"
name = "Work"
store ="radicale"
calendar_href = "/user/work/"
priority = 1

[[calendars]]
id = "test"
name = "Test"
store ="radicale"
calendar_href = "/user/test/"
priority = 2
"#;

        let config: Config = toml::from_str(TOML).expect("Failed to parse TOML");
        assert_eq!(config.calendars.len(), 3);
        assert_eq!(config.stores.len(), 1);

        // All calendars reference the same store
        for calendar in &config.calendars {
            assert_eq!(calendar.store, "radicale");
        }
    }
}