Skip to main content

lindera_crf/
errors.rs

1//! Definition of errors.
2
3use core::fmt;
4
5#[cfg(feature = "std")]
6use std::error::Error;
7
8/// Error used when the argument is invalid.
9#[derive(Debug)]
10pub struct InvalidArgumentError {
11    msg: &'static str,
12}
13
14impl fmt::Display for InvalidArgumentError {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        write!(f, "InvalidArgumentError: {}", self.msg)
17    }
18}
19
20#[cfg(feature = "std")]
21impl Error for InvalidArgumentError {}
22
23/// Error used when the model becomes too large.
24#[derive(Debug)]
25pub struct ModelScaleError {
26    msg: &'static str,
27}
28
29impl fmt::Display for ModelScaleError {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        write!(f, "ModelScaleError: {}", self.msg)
32    }
33}
34
35#[cfg(feature = "std")]
36impl Error for ModelScaleError {}
37
38/// The error type for Rucrf.
39#[derive(Debug)]
40pub enum RucrfError {
41    /// Error variant for [`InvalidArgumentError`].
42    InvalidArgument(InvalidArgumentError),
43
44    /// Error variant for [`ModelScaleError`].
45    ModelScale(ModelScaleError),
46}
47
48impl RucrfError {
49    /// Creates a new [`InvalidArgumentError`].
50    pub(crate) const fn invalid_argument(msg: &'static str) -> Self {
51        Self::InvalidArgument(InvalidArgumentError { msg })
52    }
53
54    /// Creates a new [`ModelScaleError`].
55    pub(crate) const fn model_scale(msg: &'static str) -> Self {
56        Self::ModelScale(ModelScaleError { msg })
57    }
58}
59
60impl fmt::Display for RucrfError {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62        match self {
63            Self::InvalidArgument(e) => e.fmt(f),
64            Self::ModelScale(e) => e.fmt(f),
65        }
66    }
67}
68
69#[cfg(feature = "std")]
70impl Error for RucrfError {}
71
72/// A specialized Result type.
73pub type Result<T, E = RucrfError> = core::result::Result<T, E>;