Skip to main content

temporal_vision/
error.rs

1use serde::{Deserialize, Serialize};
2
3stable_registry! {
4    /// Stable machine-readable classification for contract validation failures.
5    pub enum ErrorCode {
6        InvalidDimensions => "invalid_dimensions",
7        PixelLengthMismatch => "pixel_length_mismatch",
8        EmptySequence => "empty_sequence",
9        DuplicateIdentifier => "duplicate_identifier",
10        OutOfOrder => "out_of_order",
11        IncompatibleFrame => "incompatible_frame",
12        AnnotationOutOfRange => "annotation_out_of_range",
13        InvalidRegion => "invalid_region",
14        InvalidMask => "invalid_mask",
15        InvalidScale => "invalid_scale",
16        EmptyAnalysisDomain => "empty_analysis_domain",
17        ResourceLimitExceeded => "resource_limit_exceeded",
18        InvalidParameter => "invalid_parameter",
19        InvalidManifest => "invalid_manifest",
20        InvalidOutputHash => "invalid_output_hash",
21    }
22}
23
24/// Infrastructure-neutral validation error.
25#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error, Serialize, Deserialize)]
26#[error("{code}: {message}")]
27pub struct VisionError {
28    pub code: ErrorCode,
29    pub message: Box<str>,
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub index: Option<usize>,
32}
33
34impl VisionError {
35    pub(crate) fn new(code: ErrorCode, message: &'static str) -> Self {
36        Self {
37            code,
38            message: message.into(),
39            index: None,
40        }
41    }
42
43    pub(crate) fn at(code: ErrorCode, message: &'static str, index: usize) -> Self {
44        Self {
45            code,
46            message: message.into(),
47            index: Some(index),
48        }
49    }
50}
51
52pub type Result<T, E = VisionError> = std::result::Result<T, E>;
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn error_code_registry_has_stable_names_and_serde() {
60        for code in ErrorCode::ALL {
61            let json = serde_json::to_string(code).unwrap();
62            assert_eq!(json, format!("\"{}\"", code.as_str()));
63            assert_eq!(code.to_string(), code.as_str());
64            assert_eq!(serde_json::from_str::<ErrorCode>(&json).unwrap(), *code);
65        }
66    }
67}