Skip to main content

reovim_kernel/core/option/
error.rs

1//! Option error types.
2
3use std::fmt;
4
5use super::{OptionScopeId, OptionValue, scope::OptionScope};
6
7/// Error type for option operations.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum OptionError {
10    /// Option not found in registry.
11    NotFound(String),
12
13    /// Option already registered.
14    AlreadyExists(String),
15
16    /// Validation failed.
17    ValidationFailed {
18        /// Option name
19        name: String,
20        /// Reason for failure
21        reason: String,
22    },
23
24    /// Type mismatch.
25    TypeMismatch {
26        /// Option name
27        name: String,
28        /// Expected type
29        expected: &'static str,
30        /// Actual type
31        got: &'static str,
32    },
33
34    /// Alias conflicts with existing name.
35    AliasConflict(String),
36
37    /// Scope mismatch (e.g., setting buffer-local for global-only option).
38    ScopeMismatch {
39        /// Option name
40        name: String,
41        /// Option's declared scope
42        option_scope: OptionScope,
43        /// Requested scope
44        requested: OptionScopeId,
45    },
46}
47
48impl fmt::Display for OptionError {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Self::NotFound(name) => write!(f, "option not found: {name}"),
52            Self::AlreadyExists(name) => write!(f, "option already exists: {name}"),
53            Self::ValidationFailed { name, reason } => {
54                write!(f, "validation failed for '{name}': {reason}")
55            }
56            Self::TypeMismatch {
57                name,
58                expected,
59                got,
60            } => {
61                write!(f, "type mismatch for '{name}': expected {expected}, got {got}")
62            }
63            Self::AliasConflict(alias) => {
64                write!(f, "alias conflicts with existing name: {alias}")
65            }
66            Self::ScopeMismatch {
67                name,
68                option_scope,
69                requested,
70            } => {
71                write!(
72                    f,
73                    "scope mismatch for '{name}': option is {option_scope}, requested {requested}"
74                )
75            }
76        }
77    }
78}
79
80impl std::error::Error for OptionError {}
81
82/// Result of a successful set operation.
83#[derive(Debug, Clone)]
84pub struct SetResult {
85    /// Previous value (None if was default).
86    pub old_value: Option<OptionValue>,
87    /// New value that was set.
88    pub new_value: OptionValue,
89}