Skip to main content

charset_norm/
error.rs

1use std::fmt;
2
3/// Errors reported by the lookup helpers of this crate, such as
4/// [`encoding::iana_name`](crate::encoding::iana_name) or
5/// [`coherence::get_target_features`](crate::coherence::get_target_features).
6///
7/// Detection itself never fails: an unusable payload simply yields no match.
8///
9/// ```
10/// use charset_norm::{Error, encoding};
11///
12/// assert_eq!(
13///     encoding::iana_name("klingon-8", true),
14///     Err(Error::UnknownEncoding("klingon_8".to_owned())),
15/// );
16/// ```
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub enum Error {
20    /// The name does not resolve to a supported code page.
21    UnknownEncoding(String),
22    /// The language has no frequency profile.
23    UnknownLanguage(String),
24    /// The operation only applies to single-byte code pages.
25    MultiByteEncoding(String),
26    /// The name is not a known Unicode range.
27    UnknownRange(String),
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Error::UnknownEncoding(name) => write!(f, "Unable to retrieve IANA for '{name}'"),
34            Error::UnknownLanguage(name) => write!(f, "{name} not available"),
35            Error::MultiByteEncoding(_) => {
36                f.write_str("Function not supported on multi-byte code page")
37            }
38            Error::UnknownRange(name) => write!(f, "unknown Unicode range '{name}'"),
39        }
40    }
41}
42
43impl std::error::Error for Error {}