iconv_native/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use core::fmt;

#[cfg(doc)]
use crate::{convert, convert_lossy, decode, decode_lossy};

/// Error representation for [`decode`] and [`convert`].
#[derive(Debug, PartialEq, Eq)]
pub enum ConvertError {
    /// The encodings provided or the specific conversion pair is not supported
    /// by the implementation.
    UnknownConversion,
    /// The input data contains invalid data for the given encoding, or the
    /// input data contains a character that is not representable in the target
    /// encoding.
    InvalidInput,
}

/// Error representation for [`decode_lossy`] and [`convert_lossy`].
#[derive(Debug, PartialEq, Eq)]
pub enum ConvertLossyError {
    /// The encodings provided or the specific conversion pair is not supported
    /// by the implementation.
    UnknownConversion,
}

impl fmt::Display for ConvertError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            ConvertError::UnknownConversion => "invalid from_encoding or to_encoding",
            ConvertError::InvalidInput => "input contains invalid data for the given from_encoding",
        })
    }
}

impl fmt::Display for ConvertLossyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            ConvertLossyError::UnknownConversion => "invalid from_encoding or to_encoding",
        })
    }
}

impl From<ConvertLossyError> for ConvertError {
    fn from(err: ConvertLossyError) -> Self {
        match err {
            ConvertLossyError::UnknownConversion => ConvertError::UnknownConversion,
        }
    }
}