Skip to main content

rust_voice_assistant/
error.rs

1//! Error types and standard Result alias for Tycho.
2
3use crate::desktop::DesktopError;
4use std::fmt;
5use std::io;
6use std::path::PathBuf;
7
8pub type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Debug)]
11pub enum Error {
12    Io(io::Error),
13    Json(serde_json::Error),
14    Toml(toml::de::Error),
15    Desktop(DesktopError),
16    Audio(String),
17    ModelDownload {
18        url: String,
19        message: String,
20    },
21    ModelNotFound(PathBuf),
22    ModelCorrupted {
23        path: PathBuf,
24        expected_bytes: u64,
25        actual_bytes: u64,
26    },
27    ModelMissing(String),
28    Routing(String),
29    Transcription(String),
30    Synthesis(String),
31    Inference(String),
32    Config(String),
33}
34
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::Io(err) => write!(f, "I/O error: {}", err),
39            Self::Json(err) => write!(f, "JSON serialization error: {}", err),
40            Self::Toml(err) => write!(f, "TOML configuration error: {}", err),
41            Self::Desktop(err) => write!(f, "{}", err),
42            Self::Audio(msg) => write!(f, "audio error: {}", msg),
43            Self::ModelDownload { url, message } => {
44                write!(f, "model download from {} failed: {}", url, message)
45            }
46            Self::ModelNotFound(path) => write!(f, "model file missing: {:?}", path),
47            Self::ModelCorrupted {
48                path,
49                expected_bytes,
50                actual_bytes,
51            } => {
52                write!(
53                    f,
54                    "corrupt model at {:?} (expected {} bytes, got {})",
55                    path, expected_bytes, actual_bytes
56                )
57            }
58            Self::ModelMissing(msg) => write!(f, "model missing: {}", msg),
59            Self::Routing(msg) => write!(f, "command routing error: {}", msg),
60            Self::Transcription(msg) => write!(f, "transcription error: {}", msg),
61            Self::Synthesis(msg) => write!(f, "speech synthesis error: {}", msg),
62            Self::Inference(msg) => write!(f, "model inference error: {}", msg),
63            Self::Config(msg) => write!(f, "configuration error: {}", msg),
64        }
65    }
66}
67
68impl std::error::Error for Error {
69    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
70        match self {
71            Self::Io(err) => Some(err),
72            Self::Json(err) => Some(err),
73            Self::Toml(err) => Some(err),
74            Self::Desktop(err) => Some(err),
75            _ => None,
76        }
77    }
78}
79
80impl From<io::Error> for Error {
81    fn from(err: io::Error) -> Self {
82        Self::Io(err)
83    }
84}
85
86impl From<serde_json::Error> for Error {
87    fn from(err: serde_json::Error) -> Self {
88        Self::Json(err)
89    }
90}
91
92impl From<toml::de::Error> for Error {
93    fn from(err: toml::de::Error) -> Self {
94        Self::Toml(err)
95    }
96}
97
98impl From<DesktopError> for Error {
99    fn from(err: DesktopError) -> Self {
100        Self::Desktop(err)
101    }
102}