Skip to main content

swink_agent_eval/
error.rs

1//! Error types for the evaluation framework.
2
3/// The top-level error type for eval operations.
4#[derive(Debug, thiserror::Error)]
5pub enum EvalError {
6    /// An error from the underlying agent during evaluation.
7    #[error("agent error during evaluation")]
8    Agent {
9        #[source]
10        source: swink_agent::AgentError,
11    },
12
13    /// The requested eval case was not found.
14    #[error("eval case not found: {id}")]
15    CaseNotFound { id: String },
16
17    /// The requested eval set was not found.
18    #[error("eval set not found: {id}")]
19    SetNotFound { id: String },
20
21    /// The requested eval result was not found.
22    #[error("eval result not found: {eval_set_id}/{timestamp}")]
23    ResultNotFound { eval_set_id: String, timestamp: u64 },
24
25    /// An eval case definition is invalid.
26    #[error("invalid eval case: {reason}")]
27    InvalidCase { reason: String },
28
29    /// An evaluator name was registered more than once in the same registry.
30    #[error("duplicate evaluator registration: {name}")]
31    DuplicateEvaluator { name: String },
32
33    /// A filesystem-facing identifier is invalid.
34    #[error("invalid {kind} identifier: {id}")]
35    InvalidIdentifier { kind: &'static str, id: String },
36
37    /// Filesystem or IO error during persistence.
38    #[error("io error")]
39    Io {
40        #[source]
41        source: std::io::Error,
42    },
43
44    /// Serialization or deserialization failure.
45    #[error("serialization error")]
46    Serde {
47        #[source]
48        source: serde_json::Error,
49    },
50
51    /// YAML deserialization failure.
52    #[cfg(feature = "yaml")]
53    #[error("yaml error")]
54    Yaml {
55        #[source]
56        source: serde_yaml::Error,
57    },
58}
59
60impl EvalError {
61    /// Convenience constructor for [`EvalError::Agent`].
62    pub const fn agent(source: swink_agent::AgentError) -> Self {
63        Self::Agent { source }
64    }
65
66    /// Convenience constructor for [`EvalError::InvalidCase`].
67    pub fn invalid_case(reason: impl Into<String>) -> Self {
68        Self::InvalidCase {
69            reason: reason.into(),
70        }
71    }
72
73    /// Convenience constructor for [`EvalError::DuplicateEvaluator`].
74    pub fn duplicate_evaluator(name: impl Into<String>) -> Self {
75        Self::DuplicateEvaluator { name: name.into() }
76    }
77
78    /// Convenience constructor for [`EvalError::InvalidIdentifier`].
79    pub fn invalid_identifier(kind: &'static str, id: impl Into<String>) -> Self {
80        Self::InvalidIdentifier {
81            kind,
82            id: id.into(),
83        }
84    }
85}
86
87impl From<std::io::Error> for EvalError {
88    fn from(source: std::io::Error) -> Self {
89        Self::Io { source }
90    }
91}
92
93impl From<serde_json::Error> for EvalError {
94    fn from(source: serde_json::Error) -> Self {
95        Self::Serde { source }
96    }
97}
98
99impl From<swink_agent::AgentError> for EvalError {
100    fn from(source: swink_agent::AgentError) -> Self {
101        Self::Agent { source }
102    }
103}
104
105#[cfg(feature = "yaml")]
106impl From<serde_yaml::Error> for EvalError {
107    fn from(source: serde_yaml::Error) -> Self {
108        Self::Yaml { source }
109    }
110}