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 {}
41
42// ---------------------------------------------------------------------------
43// Tests
44// ---------------------------------------------------------------------------
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use crate::alloc::string::ToString;
50
51    #[test]
52    fn display_invalid_utf8() {
53        assert_eq!(KhamError::InvalidUtf8.to_string(), "invalid UTF-8 sequence");
54    }
55
56    #[test]
57    fn display_dict_load_error_includes_message() {
58        let e = KhamError::DictLoadError(String::from("file not found"));
59        assert_eq!(e.to_string(), "dictionary load error: file not found");
60    }
61
62    #[test]
63    fn display_corrupt_dict() {
64        assert_eq!(
65            KhamError::CorruptDict.to_string(),
66            "dictionary data is corrupt or has wrong version"
67        );
68    }
69
70    #[test]
71    fn display_empty_input() {
72        assert_eq!(KhamError::EmptyInput.to_string(), "input must not be empty");
73    }
74
75    #[test]
76    fn clone_produces_equal_value() {
77        let original = KhamError::DictLoadError(String::from("oops"));
78        assert_eq!(original.clone(), original);
79    }
80
81    #[test]
82    fn partial_eq_same_variant() {
83        assert_eq!(KhamError::InvalidUtf8, KhamError::InvalidUtf8);
84        assert_eq!(KhamError::CorruptDict, KhamError::CorruptDict);
85        assert_eq!(KhamError::EmptyInput, KhamError::EmptyInput);
86    }
87
88    #[test]
89    fn partial_eq_different_variants() {
90        assert_ne!(KhamError::InvalidUtf8, KhamError::CorruptDict);
91        assert_ne!(KhamError::EmptyInput, KhamError::InvalidUtf8);
92    }
93
94    #[test]
95    fn partial_eq_dict_load_error_compares_message() {
96        assert_eq!(
97            KhamError::DictLoadError(String::from("a")),
98            KhamError::DictLoadError(String::from("a"))
99        );
100        assert_ne!(
101            KhamError::DictLoadError(String::from("a")),
102            KhamError::DictLoadError(String::from("b"))
103        );
104    }
105}