Skip to main content

forge/
error.rs

1use std::fmt;
2
3/// Unified error type for all Forge operations.
4#[derive(Debug)]
5pub enum ForgeError {
6    Io(std::io::Error),
7    Json(serde_json::Error),
8    SafeTensors(String),
9    /// Shape or dtype mismatch in a tensor operation.
10    Shape(String),
11    /// WebGPU runtime failure (no adapter, device request, mapping, ...).
12    Wgpu(String),
13    Tokenizer(String),
14    /// A tensor expected on one device was found on another.
15    Device(String),
16}
17
18impl fmt::Display for ForgeError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            ForgeError::Io(e) => write!(f, "io error: {e}"),
22            ForgeError::Json(e) => write!(f, "json error: {e}"),
23            ForgeError::SafeTensors(m) => write!(f, "safetensors error: {m}"),
24            ForgeError::Shape(m) => write!(f, "shape error: {m}"),
25            ForgeError::Wgpu(m) => write!(f, "wgpu error: {m}"),
26            ForgeError::Tokenizer(m) => write!(f, "tokenizer error: {m}"),
27            ForgeError::Device(m) => write!(f, "device error: {m}"),
28        }
29    }
30}
31
32impl std::error::Error for ForgeError {}
33
34impl From<std::io::Error> for ForgeError {
35    fn from(e: std::io::Error) -> Self {
36        ForgeError::Io(e)
37    }
38}
39
40impl From<serde_json::Error> for ForgeError {
41    fn from(e: serde_json::Error) -> Self {
42        ForgeError::Json(e)
43    }
44}
45
46pub type Result<T> = std::result::Result<T, ForgeError>;