Skip to main content

firered_vad/
error.rs

1//! Error type for the `firered-vad` crate.
2
3use std::path::PathBuf;
4
5/// Errors returned by the `firered-vad` crate.
6#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9  /// Failed to load an ONNX model from disk.
10  #[error("failed to load model from {path}: {source}")]
11  LoadModel {
12    /// Path that was being loaded.
13    path: PathBuf,
14    /// Underlying ONNX Runtime error.
15    source: ort::Error,
16  },
17
18  /// An ONNX Runtime error not specific to model loading.
19  #[error(transparent)]
20  Ort(#[from] ort::Error),
21
22  /// Failed to read a CMVN file from disk.
23  #[error("failed to read CMVN file from {path}: {source}")]
24  LoadCmvn {
25    /// Path that was being loaded.
26    path: PathBuf,
27    /// Underlying I/O error.
28    source: std::io::Error,
29  },
30
31  /// The CMVN bytes were not in the expected Kaldi binary format.
32  #[error("invalid CMVN format: {reason}")]
33  InvalidCmvn {
34    /// Human-readable reason describing what failed to parse.
35    reason: &'static str,
36  },
37
38  /// An ONNX output tensor had an unexpected shape.
39  #[error("ONNX output {tensor} had unexpected shape {shape:?}")]
40  UnexpectedOutputShape {
41    /// The output tensor name (e.g. `"probs"`, `"caches_out"`).
42    tensor: &'static str,
43    /// The actual shape returned by the ONNX runtime.
44    shape: Vec<i64>,
45  },
46}
47
48/// Convenience alias for `Result<T, firered_vad::Error>`.
49pub type Result<T> = std::result::Result<T, Error>;
50
51#[cfg(test)]
52mod tests {
53  use super::*;
54
55  #[test]
56  fn invalid_cmvn_carries_static_reason() {
57    let err = Error::InvalidCmvn {
58      reason: "missing magic",
59    };
60    assert!(err.to_string().contains("missing magic"));
61  }
62
63  #[test]
64  fn unexpected_output_shape_renders_shape() {
65    let err = Error::UnexpectedOutputShape {
66      tensor: "probs",
67      shape: vec![1, 2, 3],
68    };
69    assert!(err.to_string().contains("probs"));
70    assert!(err.to_string().contains("[1, 2, 3]"));
71  }
72}