Skip to main content

nwnrs_encoding/
errors.rs

1use std::{error::Error, fmt};
2
3/// An error returned when an encoding label cannot be resolved.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct UnknownEncodingError {
6    label: String,
7}
8
9impl UnknownEncodingError {
10    /// Creates a new unknown-encoding error.
11    pub fn new(label: impl Into<String>) -> Self {
12        Self {
13            label: label.into(),
14        }
15    }
16}
17
18impl fmt::Display for UnknownEncodingError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        write!(f, "unknown encoding: {}", self.label)
21    }
22}
23
24impl Error for UnknownEncodingError {}
25
26/// An error returned when a text conversion fails for a configured encoding.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct EncodingConversionError {
29    encoding:  String,
30    operation: &'static str,
31}
32
33impl EncodingConversionError {
34    pub(crate) fn new(encoding: impl Into<String>, operation: &'static str) -> Self {
35        Self {
36            encoding: encoding.into(),
37            operation,
38        }
39    }
40}
41
42impl fmt::Display for EncodingConversionError {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(
45            f,
46            "failed to {} with encoding {}",
47            self.operation, self.encoding
48        )
49    }
50}
51
52impl Error for EncodingConversionError {}
53
54/// An error returned when the native system encoding cannot be determined.
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct NativeEncodingError {
57    message: String,
58}
59
60impl NativeEncodingError {
61    pub(crate) fn new(message: impl Into<String>) -> Self {
62        Self {
63            message: message.into(),
64        }
65    }
66}
67
68impl fmt::Display for NativeEncodingError {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.write_str(&self.message)
71    }
72}
73
74impl Error for NativeEncodingError {}