Skip to main content

spatial_narrative/
error.rs

1//! Error types for the spatial-narrative library.
2
3use thiserror::Error;
4
5/// Result type alias for spatial-narrative operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur in spatial-narrative operations.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Invalid latitude value (must be between -90 and 90).
12    #[error("invalid latitude {0}: must be between -90 and 90")]
13    InvalidLatitude(f64),
14
15    /// Invalid longitude value (must be between -180 and 180).
16    #[error("invalid longitude {0}: must be between -180 and 180")]
17    InvalidLongitude(f64),
18
19    /// Invalid coordinate pair.
20    #[error("invalid coordinates: lat={lat}, lon={lon}")]
21    InvalidCoordinates { lat: f64, lon: f64 },
22
23    /// Invalid timestamp format.
24    #[error("invalid timestamp: {0}")]
25    InvalidTimestamp(String),
26
27    /// Missing required field in builder.
28    #[error("missing required field: {0}")]
29    MissingField(&'static str),
30
31    /// Event not found.
32    #[error("event not found: {0}")]
33    EventNotFound(String),
34
35    /// Narrative not found.
36    #[error("narrative not found: {0}")]
37    NarrativeNotFound(String),
38
39    /// I/O error.
40    #[error("I/O error: {0}")]
41    Io(#[from] std::io::Error),
42
43    /// JSON parsing/serialization error.
44    #[error("JSON error: {0}")]
45    Json(#[from] serde_json::Error),
46
47    /// CSV parsing error.
48    #[error("CSV error: {0}")]
49    Csv(#[from] csv::Error),
50
51    /// Invalid file format.
52    #[error("invalid format: {0}")]
53    InvalidFormat(String),
54
55    /// Index error.
56    #[error("index error: {0}")]
57    IndexError(String),
58
59    /// Graph error.
60    #[error("graph error: {0}")]
61    GraphError(String),
62
63    /// Analysis error.
64    #[error("analysis error: {0}")]
65    AnalysisError(String),
66
67    /// Parse error.
68    #[error("parse error: {0}")]
69    ParseError(String),
70
71    /// Generic error with context.
72    #[error("{context}: {source}")]
73    WithContext {
74        context: String,
75        #[source]
76        source: Box<Error>,
77    },
78}
79
80impl Error {
81    /// Add context to an error.
82    pub fn with_context(self, context: impl Into<String>) -> Self {
83        Error::WithContext {
84            context: context.into(),
85            source: Box::new(self),
86        }
87    }
88}