scribe_scaling/
error.rs

1//! Error handling for scaling optimizations.
2
3use std::io;
4use std::path::PathBuf;
5use thiserror::Error;
6
7/// Type alias for Results using ScalingError
8pub type ScalingResult<T> = std::result::Result<T, ScalingError>;
9
10/// Scaling-specific error types
11#[derive(Error, Debug)]
12pub enum ScalingError {
13    /// I/O related errors
14    #[error("I/O error: {message}")]
15    Io {
16        message: String,
17        #[source]
18        source: io::Error,
19    },
20
21    /// Path-related errors
22    #[error("Path error: {message} (path: {path:?})")]
23    Path {
24        message: String,
25        path: PathBuf,
26        #[source]
27        source: Option<io::Error>,
28    },
29
30    /// Configuration errors
31    #[error("Configuration error: {message}")]
32    Config {
33        message: String,
34        field: Option<String>,
35    },
36
37    /// Caching errors
38    #[error("Cache error: {message}")]
39    Cache {
40        message: String,
41        #[source]
42        source: Option<Box<dyn std::error::Error + Send + Sync>>,
43    },
44
45    /// Streaming errors
46    #[error("Streaming error: {message}")]
47    Streaming {
48        message: String,
49        #[source]
50        source: Option<Box<dyn std::error::Error + Send + Sync>>,
51    },
52
53    /// Parallel processing errors
54    #[error("Parallel processing error: {message}")]
55    Parallel {
56        message: String,
57        #[source]
58        source: Option<Box<dyn std::error::Error + Send + Sync>>,
59    },
60
61    /// Memory management errors
62    #[error("Memory error: {message}")]
63    Memory {
64        message: String,
65        details: Option<String>,
66    },
67
68    /// Signature extraction errors
69    #[error("Signature error: {message}")]
70    Signature {
71        message: String,
72        #[source]
73        source: Option<Box<dyn std::error::Error + Send + Sync>>,
74    },
75
76    /// Repository profiling errors
77    #[error("Profiling error: {message}")]
78    Profiling {
79        message: String,
80        #[source]
81        source: Option<Box<dyn std::error::Error + Send + Sync>>,
82    },
83
84    /// Timeout errors
85    #[error("Timeout error: {message} (timeout: {timeout_ms}ms)")]
86    Timeout {
87        message: String,
88        timeout_ms: u64,
89    },
90
91    /// Resource limit exceeded
92    #[error("Resource limit exceeded: {message} (limit: {limit}, actual: {actual})")]
93    ResourceLimit {
94        message: String,
95        limit: u64,
96        actual: u64,
97    },
98
99    /// Internal errors
100    #[error("Internal error: {message}")]
101    Internal {
102        message: String,
103        location: Option<String>,
104    },
105}
106
107impl ScalingError {
108    /// Create a new I/O error
109    pub fn io<S: Into<String>>(message: S, source: io::Error) -> Self {
110        Self::Io {
111            message: message.into(),
112            source,
113        }
114    }
115
116    /// Create a new path error
117    pub fn path<S: Into<String>, P: Into<PathBuf>>(message: S, path: P) -> Self {
118        Self::Path {
119            message: message.into(),
120            path: path.into(),
121            source: None,
122        }
123    }
124
125    /// Create a new configuration error
126    pub fn config<S: Into<String>>(message: S) -> Self {
127        Self::Config {
128            message: message.into(),
129            field: None,
130        }
131    }
132
133    /// Create a new cache error
134    pub fn cache<S: Into<String>>(message: S) -> Self {
135        Self::Cache {
136            message: message.into(),
137            source: None,
138        }
139    }
140
141    /// Create a new streaming error
142    pub fn streaming<S: Into<String>>(message: S) -> Self {
143        Self::Streaming {
144            message: message.into(),
145            source: None,
146        }
147    }
148
149    /// Create a new parallel processing error
150    pub fn parallel<S: Into<String>>(message: S) -> Self {
151        Self::Parallel {
152            message: message.into(),
153            source: None,
154        }
155    }
156
157    /// Create a new memory error
158    pub fn memory<S: Into<String>>(message: S) -> Self {
159        Self::Memory {
160            message: message.into(),
161            details: None,
162        }
163    }
164
165    /// Create a new signature error
166    pub fn signature<S: Into<String>>(message: S) -> Self {
167        Self::Signature {
168            message: message.into(),
169            source: None,
170        }
171    }
172
173    /// Create a new profiling error
174    pub fn profiling<S: Into<String>>(message: S) -> Self {
175        Self::Profiling {
176            message: message.into(),
177            source: None,
178        }
179    }
180
181    /// Create a new timeout error
182    pub fn timeout<S: Into<String>>(message: S, timeout_ms: u64) -> Self {
183        Self::Timeout {
184            message: message.into(),
185            timeout_ms,
186        }
187    }
188
189    /// Create a new resource limit error
190    pub fn resource_limit<S: Into<String>>(message: S, limit: u64, actual: u64) -> Self {
191        Self::ResourceLimit {
192            message: message.into(),
193            limit,
194            actual,
195        }
196    }
197
198    /// Create a new internal error
199    pub fn internal<S: Into<String>>(message: S) -> Self {
200        Self::Internal {
201            message: message.into(),
202            location: None,
203        }
204    }
205}
206
207impl From<io::Error> for ScalingError {
208    fn from(error: io::Error) -> Self {
209        Self::Io {
210            message: "I/O operation failed".to_string(),
211            source: error,
212        }
213    }
214}
215
216impl From<serde_json::Error> for ScalingError {
217    fn from(error: serde_json::Error) -> Self {
218        Self::Config {
219            message: format!("JSON serialization/deserialization failed: {}", error),
220            field: None,
221        }
222    }
223}
224
225impl From<bincode::Error> for ScalingError {
226    fn from(error: bincode::Error) -> Self {
227        Self::Cache {
228            message: format!("Binary serialization failed: {}", error),
229            source: Some(Box::new(error)),
230        }
231    }
232}