Skip to main content

kham_core/
error.rs

1//! Error types for kham-core.
2
3use alloc::string::String;
4use core::fmt;
5
6/// All errors that kham-core can produce.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum KhamError {
9    /// The supplied text was not valid UTF-8.
10    InvalidUtf8,
11    /// A dictionary file could not be loaded.
12    ///
13    /// Contains a human-readable description of the problem.
14    DictLoadError(String),
15    /// The trie data is malformed or has an unexpected version.
16    CorruptDict,
17    /// An empty input was provided where non-empty input is required.
18    EmptyInput,
19}
20
21impl fmt::Display for KhamError {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            KhamError::InvalidUtf8 => f.write_str("invalid UTF-8 sequence"),
25            KhamError::DictLoadError(msg) => write!(f, "dictionary load error: {msg}"),
26            KhamError::CorruptDict => {
27                f.write_str("dictionary data is corrupt or has wrong version")
28            }
29            KhamError::EmptyInput => f.write_str("input must not be empty"),
30        }
31    }
32}
33
34// Bring in std explicitly since this crate is #![no_std].
35#[cfg(feature = "std")]
36extern crate std;
37
38// `std::error::Error` is only available when the `std` feature is enabled.
39#[cfg(feature = "std")]
40impl std::error::Error for KhamError {}