Skip to main content

lattice_config/
origin.rs

1//! [`OptionOrigin`] — tracks which resolution layer supplied the
2//! effective value for a resolved option (`buffer-local-options.md` §4.4).
3//!
4//! Carried alongside each entry in [`crate::ResolvedOptions`] so
5//! `:set name?` / `:setlocal name?` can echo the origin in parens.
6
7/// The layer that supplied the winning value in a resolution cycle.
8#[derive(Debug, Clone, PartialEq, Eq, Default)]
9pub enum OptionOrigin {
10    /// The option's registered default — no override at any layer.
11    #[default]
12    Default,
13    /// Set via `:set` or `lattice.toml` (global config layer).
14    GlobalConfig,
15    /// Set via `:setlocal` for this buffer.
16    BufferLocal,
17    /// Contributed by a mode (major or minor).
18    ModeContribution {
19        /// The contributing mode's id (`rust-mode`), echoed as
20        /// `(mode: rust-mode)`.
21        mode_id: String,
22    },
23}
24
25impl std::fmt::Display for OptionOrigin {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            Self::Default => f.write_str("default"),
29            Self::GlobalConfig => f.write_str("global"),
30            Self::BufferLocal => f.write_str("buffer-local"),
31            Self::ModeContribution { mode_id } => write!(f, "mode: {mode_id}"),
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn display_variants() {
42        assert_eq!(OptionOrigin::Default.to_string(), "default");
43        assert_eq!(OptionOrigin::GlobalConfig.to_string(), "global");
44        assert_eq!(OptionOrigin::BufferLocal.to_string(), "buffer-local");
45        assert_eq!(
46            OptionOrigin::ModeContribution {
47                mode_id: "rust-mode".into()
48            }
49            .to_string(),
50            "mode: rust-mode"
51        );
52    }
53}