agd-core 0.1.0

Org-mode parsing and task operations for agd
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
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::{AgdError, CaptureTemplate, SORTING_KEYS, Settings, ViewBlock, ViewSettings};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ImportStatus {
    Exact,
    Difference,
    Unsupported,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ImportDiagnostic {
    pub status: ImportStatus,
    pub key: String,
    pub message: String,
}

/// One capture template as the editor reports it; disabled templates are
/// reported, not imported.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct ImportedCapture {
    pub desc: String,
    pub file: Option<String>,
    pub heading: Option<String>,
    pub outline_path: Option<Vec<String>>,
    pub id: Option<String>,
    #[serde(default)]
    pub datetree: bool,
    pub template: String,
    pub template_file: Option<String>,
    pub enabled: bool,
    #[serde(default)]
    pub diagnostics: Vec<String>,
}

/// One custom agenda command as the editor reports it. Enabled commands
/// become views; `diagnostics` name what was left out.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ImportedCommand {
    pub key: String,
    pub description: String,
    pub enabled: bool,
    #[serde(default)]
    pub blocks: Vec<ViewBlock>,
    #[serde(default)]
    pub diagnostics: Vec<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ResolvedEditorConfig {
    pub editor: String,
    #[serde(default)]
    pub editor_version: String,
    /// org-directory when the config sets it; it becomes the root on a first
    /// import.
    #[serde(default)]
    pub org_directory: Option<String>,
    /// Agenda entries as written in the editor config, made absolute: files,
    /// directories or globs.
    pub agenda_files: Vec<String>,
    pub todo_sequences: Vec<Vec<String>>,
    pub tag_alist: Vec<String>,
    pub persistent_tags: Vec<String>,
    pub tags_column: i64,
    pub tag_inheritance_exclusions: Vec<String>,
    pub highest_priority: String,
    pub lowest_priority: String,
    pub default_priority: String,
    pub deadline_warning_days: i64,
    pub agenda_span: String,
    pub agenda_start_day: Option<String>,
    pub week_start: String,
    pub skip_scheduled_if_done: bool,
    pub skip_deadline_if_done: bool,
    pub skip_timestamp_if_done: bool,
    pub sorting_strategy: Vec<String>,
    pub log_done: String,
    pub log_repeat: String,
    pub log_reschedule: String,
    pub log_redeadline: String,
    pub log_into_drawer: bool,
    #[serde(default)]
    pub capture: BTreeMap<String, ImportedCapture>,
    #[serde(default)]
    pub commands: Vec<ImportedCommand>,
    #[serde(default)]
    pub diagnostics: Vec<ImportDiagnostic>,
    #[serde(default)]
    pub verification: BTreeMap<String, String>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ConfigEditor {
    Emacs,
    Neovim,
}

impl ConfigEditor {
    pub fn name(self) -> &'static str {
        match self {
            Self::Emacs => "Emacs",
            Self::Neovim => "Neovim",
        }
    }
}

/// The editor config files an import reads, in load order, and what was
/// passed over.
#[derive(Clone, Debug)]
pub struct ConfigSources {
    pub editor: ConfigEditor,
    pub files: Vec<PathBuf>,
    pub notes: Vec<String>,
}

const MAX_DEPTH: usize = 4;
const MAX_FILES: usize = 300;
const MAX_BYTES: u64 = 1 << 20;
/// Package managers' and editors' own state: never the user's settings.
const SKIPPED_DIRECTORIES: [&str; 12] = [
    ".git",
    ".local",
    ".cache",
    "elpa",
    "straight",
    "eln-cache",
    "var",
    "auto-save-list",
    "pack",
    "plugged",
    "lazy",
    "node_modules",
];
/// Files read first, in this order, as the editors load them.
const ENTRY_POINTS: [&str; 7] = [
    "early-init.el",
    "init.el",
    "config.el",
    ".emacs",
    ".spacemacs",
    "config.org",
    "init.lua",
];

/// Finds the files to read under `paths`. A file is taken as given; a
/// directory is walked (respecting .gitignore, at most MAX_DEPTH levels,
/// MAX_FILES files and MAX_BYTES each, skipping SKIPPED_DIRECTORIES), keeping
/// Emacs Lisp files that mention `org-`, Lua files that mention `orgmode`
/// and Org files with emacs-lisp source blocks. Both editors' files under
/// one path is an error: import one editor at a time.
pub fn find_config_sources(paths: &[PathBuf]) -> Result<ConfigSources, AgdError> {
    let mut files = Vec::new();
    let mut notes = Vec::new();
    for path in paths {
        let path = PathBuf::from(shellexpand::tilde(&path.to_string_lossy()).into_owned());
        if path.is_file() {
            if editor_of(&path).is_none() {
                return Err(import_error(format!(
                    "{} is not an Emacs Lisp (.el, .org) or Lua (.lua) file",
                    path.display()
                )));
            }
            files.push(path);
        } else if path.is_dir() {
            walk_config_directory(&path, &mut files, &mut notes)?;
        } else {
            return Err(import_error(format!("{} does not exist", path.display())));
        }
    }
    let editors: Vec<ConfigEditor> = files.iter().filter_map(|file| editor_of(file)).collect();
    let Some(&editor) = editors.first() else {
        return Err(import_error(
            "found no org settings: no .el file mentioning org-, .lua file mentioning orgmode, or config.org",
        ));
    };
    if editors.iter().any(|other| *other != editor) {
        return Err(import_error(
            "found both Emacs and Neovim files; import one editor's config at a time",
        ));
    }
    files.sort_by_key(|file| {
        let name = file
            .file_name()
            .map(|name| name.to_string_lossy().into_owned());
        let rank = ENTRY_POINTS
            .iter()
            .position(|entry| Some(*entry) == name.as_deref())
            .unwrap_or(ENTRY_POINTS.len());
        (rank, file.clone())
    });
    files.dedup();
    Ok(ConfigSources {
        editor,
        files,
        notes,
    })
}

fn walk_config_directory(
    root: &Path,
    files: &mut Vec<PathBuf>,
    notes: &mut Vec<String>,
) -> Result<(), AgdError> {
    let walker = ignore::WalkBuilder::new(root)
        .hidden(false)
        .max_depth(Some(MAX_DEPTH))
        .filter_entry(|entry| {
            !(entry.file_type().is_some_and(|kind| kind.is_dir())
                && SKIPPED_DIRECTORIES.contains(&entry.file_name().to_string_lossy().as_ref()))
        })
        .build();
    let mut seen = 0;
    for entry in walker {
        let entry = entry
            .map_err(|error| import_error(format!("cannot read {}: {error}", root.display())))?;
        let path = entry.path();
        let Some(editor) = editor_of(path) else {
            continue;
        };
        seen += 1;
        if seen > MAX_FILES {
            notes.push(format!(
                "stopped after {MAX_FILES} files; point at the config file instead"
            ));
            break;
        }
        if entry
            .metadata()
            .is_ok_and(|metadata| metadata.len() > MAX_BYTES)
        {
            notes.push(format!("{}: skipped, larger than 1 MB", path.display()));
            continue;
        }
        let Ok(text) = std::fs::read_to_string(path) else {
            notes.push(format!("{}: skipped, not UTF-8 text", path.display()));
            continue;
        };
        let relevant = match editor {
            ConfigEditor::Neovim => text.contains("orgmode"),
            ConfigEditor::Emacs if path.extension().is_some_and(|extension| extension == "org") => {
                text.contains("org-") && text.to_lowercase().contains("#+begin_src emacs-lisp")
            }
            ConfigEditor::Emacs => text.contains("org-"),
        };
        if relevant {
            files.push(path.to_path_buf());
        }
    }
    Ok(())
}

/// The editor a config file belongs to, by name: Emacs Lisp, literate Org
/// configs, `.emacs` and `.spacemacs`, or Lua.
fn editor_of(path: &Path) -> Option<ConfigEditor> {
    let name = path.file_name()?.to_string_lossy();
    match path.extension().and_then(|extension| extension.to_str()) {
        Some("el") => Some(ConfigEditor::Emacs),
        Some("lua") => Some(ConfigEditor::Neovim),
        Some("org")
            if ["config.org", "init.org", "emacs.org", "README.org"].contains(&name.as_ref()) =>
        {
            Some(ConfigEditor::Emacs)
        }
        _ if name == ".emacs" || name == ".spacemacs" => Some(ConfigEditor::Emacs),
        _ => None,
    }
}

fn import_error(message: impl Into<String>) -> AgdError {
    AgdError::new("import", message)
}

/// The editor's settings applied over `base`, and what could not be.
#[derive(Clone, Debug)]
pub struct ImportResult {
    pub settings: Settings,
    pub diagnostics: Vec<ImportDiagnostic>,
}

/// Applies resolved editor settings over `base` (the current config). The
/// editor's values replace the keys it reports; agd-only keys (editor,
/// theme, agents, triage) keep their `base` values. On a first import
/// (`derive_roots`) the roots become org-directory, or the directories the
/// agenda entries live in; otherwise `base.roots` stay and entries outside
/// them are left out with a diagnostic.
pub fn apply_import(
    resolved: &ResolvedEditorConfig,
    base: &Settings,
    derive_roots: bool,
) -> Result<ImportResult, AgdError> {
    validate_resolved(resolved)?;
    let mut diagnostics = resolved.diagnostics.clone();
    let mut settings = base.clone();
    let entries: Vec<PathBuf> = resolved
        .agenda_files
        .iter()
        .map(|entry| PathBuf::from(shellexpand::tilde(entry).into_owned()))
        .collect();
    if derive_roots {
        let derived = derived_roots(resolved.org_directory.as_deref(), &entries);
        if !derived.is_empty() {
            settings.roots = derived;
        }
    }
    settings.agenda_files = Vec::new();
    for (entry, original) in entries.iter().zip(&resolved.agenda_files) {
        match agenda_pattern(entry, &settings.roots) {
            Some(pattern) => settings.agenda_files.push(pattern),
            None => diagnostics.push(ImportDiagnostic {
                status: ImportStatus::Unsupported,
                key: original.clone(),
                message: "agenda entry left out: outside the roots".to_string(),
            }),
        }
    }
    settings.agenda_files.dedup();

    settings.todo_keywords = resolved.todo_sequences.clone();
    settings.tag_alist = resolved.tag_alist.clone();
    settings.persistent_tags = resolved.persistent_tags.clone();
    settings.tags_column = resolved.tags_column;
    settings.agenda_span = resolved.agenda_span.clone();
    settings.agenda_start_day = resolved.agenda_start_day.clone();
    settings.week_start = resolved.week_start.clone();
    settings.deadline_warning_days = resolved.deadline_warning_days;
    settings.skip_scheduled_if_done = resolved.skip_scheduled_if_done;
    settings.skip_deadline_if_done = resolved.skip_deadline_if_done;
    settings.skip_timestamp_if_done = resolved.skip_timestamp_if_done;
    settings.sorting_strategy.clear();
    for key in &resolved.sorting_strategy {
        if SORTING_KEYS.contains(&key.as_str()) {
            settings.sorting_strategy.push(key.clone());
        } else {
            diagnostics.push(ImportDiagnostic {
                status: ImportStatus::Difference,
                key: "sorting_strategy".to_string(),
                message: format!("sorting key {key} is not supported and was left out"),
            });
        }
    }
    settings.highest_priority = resolved.highest_priority.clone();
    settings.lowest_priority = resolved.lowest_priority.clone();
    settings.default_priority = resolved.default_priority.clone();
    settings.tag_inheritance_exclusions = resolved.tag_inheritance_exclusions.clone();
    settings.log_done = resolved.log_done.clone();
    settings.log_repeat = resolved.log_repeat.clone();
    settings.log_reschedule = resolved.log_reschedule.clone();
    settings.log_redeadline = resolved.log_redeadline.clone();
    settings.log_into_drawer = resolved.log_into_drawer;

    settings.captures.clear();
    for (key, capture) in &resolved.capture {
        if !capture.enabled {
            diagnostics.push(ImportDiagnostic {
                status: ImportStatus::Unsupported,
                key: format!("capture {key}"),
                message: capture.diagnostics.join("; "),
            });
            continue;
        }
        settings.captures.push(CaptureTemplate {
            key: key.clone(),
            desc: capture.desc.clone(),
            file: capture.file.clone(),
            heading: capture.heading.clone(),
            outline_path: capture.outline_path.clone(),
            id: capture.id.clone(),
            datetree: capture.datetree,
            template: capture.template.clone(),
            template_file: capture.template_file.clone(),
        });
    }
    // The defaults stay only when the editor defines no templates at all.
    if resolved.capture.is_empty() {
        settings.captures = base.captures.clone();
    }

    settings.views.clear();
    for command in &resolved.commands {
        let key = format!("view {}", command.key);
        let mut command = command.clone();
        for block in &mut command.blocks {
            if let Some(rewritten) = block.match_expression.as_deref().and_then(todo_state_match) {
                diagnostics.push(ImportDiagnostic {
                    status: ImportStatus::Difference,
                    key: key.clone(),
                    message: format!(
                        "match {} imported as {rewritten}",
                        block.match_expression.as_deref().unwrap_or_default()
                    ),
                });
                block.match_expression = Some(rewritten);
            }
        }
        let unusable_match = command
            .blocks
            .iter()
            .find_map(|block| crate::agenda_engine::validate_view_block(block).err());
        if let Some(error) = &unusable_match {
            diagnostics.push(ImportDiagnostic {
                status: ImportStatus::Unsupported,
                key: key.clone(),
                message: error.to_string(),
            });
        }
        if command.enabled && unusable_match.is_none() {
            settings.views.push(ViewSettings {
                key: command.key.clone(),
                desc: command.description.clone(),
                blocks: command.blocks.clone(),
            });
        }
        let status = if command.enabled {
            ImportStatus::Difference
        } else {
            ImportStatus::Unsupported
        };
        for message in &command.diagnostics {
            diagnostics.push(ImportDiagnostic {
                status: status.clone(),
                key: key.clone(),
                message: message.clone(),
            });
        }
    }
    if resolved.commands.is_empty() {
        settings.views = base.views.clone();
    }

    Ok(ImportResult {
        settings,
        diagnostics,
    })
}

/// `TODO="NEXT"`, the common nvim-orgmode way to match a TODO state, as
/// agd's equivalent `/NEXT`; None for any other match.
fn todo_state_match(expression: &str) -> Option<String> {
    let state = expression
        .trim()
        .strip_prefix('+')
        .unwrap_or(expression.trim())
        .strip_prefix("TODO=\"")?
        .strip_suffix('"')?;
    (!state.is_empty()
        && state
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-'))
    .then(|| format!("/{state}"))
}

/// The literal directory an agenda entry starts from: a directory itself, a
/// file's parent, or a glob's components before its first wildcard.
fn entry_base(entry: &Path) -> PathBuf {
    let mut base = PathBuf::new();
    for component in entry.components() {
        if component
            .as_os_str()
            .to_string_lossy()
            .contains(['*', '?', '['])
        {
            return base;
        }
        base.push(component);
    }
    if entry.is_dir() {
        base
    } else {
        base.parent().map(Path::to_path_buf).unwrap_or(base)
    }
}

/// org-directory if set, else each agenda entry's directory; nested
/// directories collapse into their ancestor. Home-relative paths are written
/// with `~`.
fn derived_roots(org_directory: Option<&str>, entries: &[PathBuf]) -> Vec<String> {
    let mut directories: Vec<PathBuf> = match org_directory {
        Some(directory) => vec![PathBuf::from(shellexpand::tilde(directory).into_owned())],
        None => entries.iter().map(|entry| entry_base(entry)).collect(),
    };
    directories.retain(|directory| directory.is_dir());
    directories.sort();
    directories.dedup();
    let mut roots: Vec<PathBuf> = Vec::new();
    for directory in directories {
        if !roots.iter().any(|root| directory.starts_with(root)) {
            roots.push(directory);
        }
    }
    let home = dirs::home_dir();
    roots
        .into_iter()
        .map(|root| {
            match home
                .as_deref()
                .and_then(|home| root.strip_prefix(home).ok())
            {
                Some(rest) if rest.as_os_str().is_empty() => "~".to_string(),
                Some(rest) => format!("~/{}", rest.to_string_lossy()),
                None => root.to_string_lossy().into_owned(),
            }
        })
        .collect()
}

/// An agenda entry as a pattern relative to the root that holds it: a file
/// stays a file, a directory becomes its `*.org` files (Emacs does not
/// recurse), and a glob keeps its wildcards. None when no root holds it.
fn agenda_pattern(entry: &Path, roots: &[String]) -> Option<String> {
    let base = entry_base(entry);
    let root = roots
        .iter()
        .map(|root| PathBuf::from(shellexpand::tilde(root).into_owned()))
        .filter(|root| base.starts_with(root))
        .max_by_key(|root| root.components().count())?;
    let relative = entry
        .strip_prefix(&root)
        .ok()?
        .to_string_lossy()
        .into_owned();
    if entry.is_dir() {
        Some(if relative.is_empty() {
            "*.org".to_string()
        } else {
            format!("{relative}/*.org")
        })
    } else {
        Some(relative)
    }
}

fn validate_resolved(resolved: &ResolvedEditorConfig) -> Result<(), AgdError> {
    if resolved.todo_sequences.is_empty()
        || resolved
            .todo_sequences
            .iter()
            .any(|sequence| sequence.is_empty())
    {
        return Err(AgdError::new(
            "unsupported_import_shape",
            "editor returned no usable TODO sequence",
        ));
    }
    for (name, value) in [
        ("highest priority", &resolved.highest_priority),
        ("lowest priority", &resolved.lowest_priority),
        ("default priority", &resolved.default_priority),
    ] {
        if value.chars().count() != 1 {
            return Err(AgdError::new(
                "unsupported_import_shape",
                format!("{name} must be one character"),
            ));
        }
    }
    if resolved.week_start != "monday" && resolved.week_start != "sunday" {
        return Err(AgdError::new(
            "unsupported_import_shape",
            format!("unsupported week start: {}", resolved.week_start),
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn discovery_reads_entry_points_first_and_only_org_mentions() {
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path();
        fs::write(root.join("config.el"), "(setq org-log-done 'time)\n").unwrap();
        fs::write(root.join("init.el"), "(setq org-directory \"~/org\")\n").unwrap();
        fs::write(root.join("keys.el"), "(map! :n \"x\" #'ignore)\n").unwrap();
        fs::create_dir_all(root.join(".local/straight")).unwrap();
        fs::write(root.join(".local/straight/org.el"), "(setq org-x 1)\n").unwrap();
        let sources = find_config_sources(&[root.to_path_buf()]).unwrap();
        assert_eq!(sources.editor, ConfigEditor::Emacs);
        assert_eq!(
            sources.files,
            vec![root.join("init.el"), root.join("config.el")]
        );

        fs::write(root.join("init.lua"), "require('orgmode').setup({})\n").unwrap();
        let error = find_config_sources(&[root.to_path_buf()]).unwrap_err();
        assert!(
            error.to_string().contains("both Emacs and Neovim"),
            "{error}"
        );
    }

    #[test]
    fn first_import_derives_roots_and_relative_patterns() {
        let temp = tempfile::tempdir().unwrap();
        let org = temp.path().join("org");
        fs::create_dir_all(org.join("projects")).unwrap();
        let entries = [
            org.join("projects"),
            org.join("**/*.org"),
            org.join("inbox.org"),
        ];
        let roots = derived_roots(None, &entries);
        assert_eq!(roots, vec![org.to_string_lossy().into_owned()]);
        let patterns: Vec<Option<String>> = entries
            .iter()
            .map(|entry| agenda_pattern(entry, &roots))
            .collect();
        assert_eq!(
            patterns,
            vec![
                Some("projects/*.org".to_string()),
                Some("**/*.org".to_string()),
                Some("inbox.org".to_string())
            ]
        );
        assert_eq!(
            agenda_pattern(&temp.path().join("elsewhere.org"), &roots),
            None
        );
    }

    #[test]
    fn reimport_keeps_roots_and_agd_only_keys_and_drops_outside_entries() {
        let temp = tempfile::tempdir().unwrap();
        let base = Settings {
            roots: vec![temp.path().to_string_lossy().into_owned()],
            theme: "dark".to_string(),
            ..Settings::default()
        };
        let resolved: ResolvedEditorConfig = serde_json::from_value(serde_json::json!({
            "editor": "emacs",
            "org_directory": "/elsewhere",
            "agenda_files": [temp.path().join("inbox.org"), "/elsewhere/private.org"],
            "todo_sequences": [["TODO", "|", "DONE"]],
            "tag_alist": [], "persistent_tags": [], "tags_column": -77,
            "tag_inheritance_exclusions": [],
            "highest_priority": "A", "lowest_priority": "C", "default_priority": "B",
            "deadline_warning_days": 14, "agenda_span": "week", "agenda_start_day": null,
            "week_start": "monday", "skip_scheduled_if_done": false,
            "skip_deadline_if_done": false, "skip_timestamp_if_done": false,
            "sorting_strategy": ["time-up"], "log_done": "time", "log_repeat": "time",
            "log_reschedule": "nil", "log_redeadline": "nil", "log_into_drawer": false
        }))
        .unwrap();
        let result = apply_import(&resolved, &base, false).unwrap();
        assert_eq!(result.settings.roots, base.roots);
        assert_eq!(result.settings.theme, "dark");
        assert_eq!(result.settings.agenda_files, vec!["inbox.org"]);
        assert!(
            result
                .diagnostics
                .iter()
                .any(|diagnostic| diagnostic.key == "/elsewhere/private.org")
        );
        assert_eq!(result.settings.captures, base.captures);
    }

    #[test]
    fn only_a_bare_todo_state_match_is_rewritten() {
        assert_eq!(todo_state_match("TODO=\"NEXT\""), Some("/NEXT".to_string()));
        assert_eq!(
            todo_state_match("+TODO=\"WAIT\""),
            Some("/WAIT".to_string())
        );
        assert_eq!(todo_state_match("TODO=\"DONE\"+CLOSED>=\"<-3w>\""), None);
        assert_eq!(todo_state_match("+work/NEXT"), None);
    }
}