Skip to main content

sentencepiece_rust/
error.rs

1//! Error type for the crate.
2
3use std::fmt;
4
5/// Errors produced while loading a model or encoding/decoding text.
6#[derive(Debug)]
7pub enum Error {
8    /// The model file/bytes could not be parsed as a `ModelProto`.
9    Proto(String),
10    /// The model parsed but is missing data required for inference.
11    Model(String),
12    /// A feature is recognised but not yet implemented in this port.
13    Unsupported(&'static str),
14    /// Underlying I/O error while reading a model file.
15    Io(std::io::Error),
16}
17
18impl fmt::Display for Error {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Error::Proto(m) => write!(f, "proto parse error: {m}"),
22            Error::Model(m) => write!(f, "invalid model: {m}"),
23            Error::Unsupported(m) => write!(f, "unsupported: {m}"),
24            Error::Io(e) => write!(f, "io error: {e}"),
25        }
26    }
27}
28
29impl std::error::Error for Error {
30    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
31        match self {
32            Error::Io(e) => Some(e),
33            _ => None,
34        }
35    }
36}
37
38impl From<std::io::Error> for Error {
39    fn from(e: std::io::Error) -> Self {
40        Error::Io(e)
41    }
42}
43
44pub type Result<T> = std::result::Result<T, Error>;