Skip to main content

lindera_dependency/
error.rs

1//! The error type of this crate.
2
3use std::fmt;
4use std::io;
5
6/// The result type of this crate.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// An error of this crate.
10#[derive(Debug)]
11#[non_exhaustive]
12pub enum Error {
13    /// The input is not in the lattice format.
14    Format {
15        /// The number of the line, from 1.
16        line: usize,
17        /// What is wrong with the line.
18        message: String,
19    },
20    /// A sentence is not one the lattice format can hold: its bunsetsu do not
21    /// cover its morphemes, a head or a position is out of range, or a
22    /// string holds a tab or a line break.
23    Structure(String),
24    /// Reading or writing failed.
25    Io(io::Error),
26    /// The chunker was given something it cannot use: a template or a list
27    /// of columns that cannot be read, weights that are not finite, or
28    /// training data that cannot be trained on.
29    Chunker(String),
30    /// Bytes are not a model file this crate reads: another magic number or
31    /// version, bytes that end early or go on after the end, or content
32    /// that is not what the format says.
33    Model(String),
34    /// A model was used with another dictionary than the one it was trained
35    /// for: the name of the dictionary differs, or a morpheme has fewer
36    /// feature columns than the schema of the model reads.
37    Dictionary(String),
38    /// No model is embedded in the build for the dictionary asked for: no
39    /// feature embeds one for it, the build is without the feature, or it
40    /// is a documentation build, which embeds an empty file.
41    Embedded(String),
42    /// The parser was given something it cannot use: a feature set that
43    /// cannot be read, or weights that are not finite.
44    Parser(String),
45    /// A schema was given something it cannot hold: rules of
46    /// normalization that are not a table.
47    Schema(String),
48    /// A model of CaboCha cannot be converted: a file is not a text model
49    /// of CaboCha, or it is a model this crate has no schema for.
50    #[cfg(feature = "train")]
51    Cabocha(String),
52    /// The input is not a CoNLL-U file the corpus conversion can read.
53    #[cfg(feature = "train")]
54    Conllu {
55        /// The number of the line, from 1.
56        line: usize,
57        /// What is wrong with the line.
58        message: String,
59    },
60    /// The tokenizer given to the corpus conversion failed.
61    #[cfg(feature = "train")]
62    Tokenizer(Box<dyn std::error::Error + Send + Sync>),
63    /// The files given to the evaluation cannot be compared: they do not
64    /// hold the same number of sentences.
65    #[cfg(feature = "train")]
66    Evaluation(String),
67}
68
69impl Error {
70    /// Makes a [`Error::Format`].
71    ///
72    /// # Arguments
73    ///
74    /// * `line` - The number of the line, from 1.
75    /// * `message` - What is wrong with the line.
76    ///
77    /// # Returns
78    ///
79    /// The error.
80    pub(crate) fn format(line: usize, message: impl Into<String>) -> Self {
81        Error::Format {
82            line,
83            message: message.into(),
84        }
85    }
86
87    /// Makes a [`Error::Conllu`].
88    ///
89    /// # Arguments
90    ///
91    /// * `line` - The number of the line, from 1.
92    /// * `message` - What is wrong with the line.
93    ///
94    /// # Returns
95    ///
96    /// The error.
97    #[cfg(feature = "train")]
98    pub(crate) fn conllu(line: usize, message: impl Into<String>) -> Self {
99        Error::Conllu {
100            line,
101            message: message.into(),
102        }
103    }
104}
105
106impl fmt::Display for Error {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        match self {
109            Error::Format { line, message } => write!(f, "line {line}: {message}"),
110            Error::Structure(message) => write!(f, "invalid sentence: {message}"),
111            Error::Io(error) => write!(f, "{error}"),
112            Error::Chunker(message) => write!(f, "{message}"),
113            Error::Model(message) => write!(f, "{message}"),
114            Error::Dictionary(message) => write!(f, "{message}"),
115            Error::Embedded(message) => write!(f, "{message}"),
116            Error::Parser(message) => write!(f, "{message}"),
117            Error::Schema(message) => write!(f, "{message}"),
118            #[cfg(feature = "train")]
119            Error::Cabocha(message) => write!(f, "{message}"),
120            #[cfg(feature = "train")]
121            Error::Conllu { line, message } => write!(f, "line {line}: {message}"),
122            #[cfg(feature = "train")]
123            Error::Tokenizer(error) => write!(f, "tokenizer: {error}"),
124            #[cfg(feature = "train")]
125            Error::Evaluation(message) => write!(f, "{message}"),
126        }
127    }
128}
129
130impl std::error::Error for Error {
131    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
132        match self {
133            Error::Io(error) => Some(error),
134            #[cfg(feature = "train")]
135            Error::Tokenizer(error) => Some(error.as_ref()),
136            _ => None,
137        }
138    }
139}
140
141impl From<io::Error> for Error {
142    fn from(error: io::Error) -> Self {
143        Error::Io(error)
144    }
145}