Skip to main content

kime_model/
error.rs

1use std::fmt;
2use std::path::PathBuf;
3
4/// Everything that can go wrong opening a checkpoint.
5#[derive(Debug)]
6pub enum Error {
7    /// A file could not be read or written.
8    Io(PathBuf, std::io::Error),
9    /// The bytes are not a valid file of the expected format. The message says where.
10    Format(String),
11    /// The file is valid but does not hold the model its config describes. One line per problem,
12    /// naming the tensor, as Laya's `_verify_compatibility` does.
13    Mismatch(Vec<String>),
14}
15
16impl Error {
17    pub(crate) fn format(msg: impl Into<String>) -> Self {
18        Self::Format(msg.into())
19    }
20}
21
22impl fmt::Display for Error {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Self::Io(path, e) => write!(f, "{}: {e}", path.display()),
26            Self::Format(msg) => f.write_str(msg),
27            Self::Mismatch(problems) => {
28                write!(f, "checkpoint does not match its config ({} problems)", problems.len())?;
29                for p in problems {
30                    write!(f, "\n  {p}")?;
31                }
32                Ok(())
33            }
34        }
35    }
36}
37
38impl std::error::Error for Error {}
39
40pub(crate) type Result<T> = std::result::Result<T, Error>;