use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelPath(String);
impl ModelPath {
pub fn new(s: &str) -> Result<Self, GigasttError> {
if s.is_empty() {
return Err(GigasttError::InvalidAudio {
reason: "empty model path".into(),
});
}
Ok(ModelPath(s.to_string()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Reason(String);
impl Reason {
pub fn new(s: &str) -> Result<Self, GigasttError> {
if s.is_empty() {
return Err(GigasttError::InvalidAudio {
reason: "empty error reason".into(),
});
}
Ok(Reason(s.to_string()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum GigasttError {
#[error("model load error at {path}")]
ModelLoad {
path: String,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync>>,
},
#[error("inference failed")]
Inference {
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("invalid audio: {reason}")]
InvalidAudio {
reason: String,
},
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("invalid input: {message}")]
InvalidInput { message: String },
#[error("cancelled")]
Cancelled,
#[error("audio too long: {observed_secs:.0}s exceeds the maximum of {limit_secs:.0}s")]
AudioTooLong {
observed_secs: f64,
limit_secs: f64,
},
}
impl GigasttError {
pub fn code(&self) -> &'static str {
match self {
GigasttError::ModelLoad { .. } => "model_load_error",
GigasttError::Inference { .. } => "inference_error",
GigasttError::InvalidAudio { .. } => "invalid_audio",
GigasttError::Io(_) => "io_error",
GigasttError::InvalidInput { .. } => "invalid_input",
GigasttError::Cancelled => "cancelled",
GigasttError::AudioTooLong { .. } => "audio_too_long",
}
}
}
impl From<crate::runtime::RuntimeError> for GigasttError {
fn from(err: crate::runtime::RuntimeError) -> Self {
match err {
crate::runtime::RuntimeError::LoadFailed { path, message } => GigasttError::ModelLoad {
path: path.to_string_lossy().into_owned(),
source: Some(Box::new(std::io::Error::other(message))),
},
other => GigasttError::Inference {
source: Box::new(other),
},
}
}
}
#[cfg(test)]
mod tests;