Skip to main content

mnemo_md_sync/
spec.rs

1//! Sync configuration (v0.4.0 P2-6).
2
3use std::path::PathBuf;
4use std::time::Duration;
5
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct MdSyncSpec {
10    /// Root of the git repo we sync against. Watcher polls every
11    /// file under `glob`.
12    pub repo: PathBuf,
13    /// Glob pattern (relative to `repo`) for files to sync.
14    /// Default `**/*.md`.
15    pub glob: String,
16    /// Author signature for commit-on-flush.
17    pub commit_author: String,
18    /// Buffer edits this long before issuing a single git commit.
19    /// Smaller values mean more commits but smaller windows for
20    /// data loss; the default 250ms matches Wuphf's published flush
21    /// cadence.
22    pub flush_every: Duration,
23}
24
25impl Default for MdSyncSpec {
26    fn default() -> Self {
27        Self {
28            repo: PathBuf::from("."),
29            glob: "**/*.md".to_string(),
30            commit_author: "mnemo-md-sync <mnemo@localhost>".to_string(),
31            flush_every: Duration::from_millis(250),
32        }
33    }
34}
35
36/// How aggressive the flush-to-disk side should be when an in-engine
37/// remember would overwrite a file the human is editing.
38#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39pub enum SyncFlushPolicy {
40    /// Always overwrite the on-disk file with the engine's version.
41    /// Used during a fresh import.
42    PreferEngine,
43    /// Always preserve the on-disk file; engine writes that would
44    /// overwrite are written to a sibling `.conflict.md`. Default.
45    #[default]
46    PreferDisk,
47    /// Pick whichever has the more recent timestamp; tie goes to disk.
48    NewerWins,
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn default_glob_matches_all_md() {
57        let s = MdSyncSpec::default();
58        assert_eq!(s.glob, "**/*.md");
59    }
60
61    #[test]
62    fn default_flush_is_under_one_second() {
63        let s = MdSyncSpec::default();
64        assert!(s.flush_every < Duration::from_secs(1));
65    }
66
67    #[test]
68    fn default_policy_prefers_disk() {
69        let p = SyncFlushPolicy::default();
70        assert_eq!(p, SyncFlushPolicy::PreferDisk);
71    }
72}