Skip to main content

mac_encoding/
error.rs

1//! The errors. Each error gives the item that caused it.
2//!
3//! Section 4.1 of the standard says that a handler returns "error optionally
4//! with a code point". The encoder always gives a code point. Refer to
5//! section 9.2, "return error with codePoint". Thus [`EncodeError`] has one.
6//! The decoder gives no item, but the byte is the only useful data. Thus
7//! [`DecodeError`] has the byte.
8//!
9//! Each error also gives a position. The standard does not ask for this. But
10//! a program that cannot encode a resource must tell you which part of the
11//! text caused the error.
12
13use core::fmt;
14
15use crate::Encoding;
16
17/// The encoding has no byte for this character.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct EncodeError {
20    /// The encoding with no byte for [`Self::code_point`].
21    pub encoding: Encoding,
22    /// The first code point with no mapping.
23    pub code_point: char,
24    /// The position of [`Self::code_point`] in the text, counted in bytes.
25    pub index: usize,
26}
27
28impl fmt::Display for EncodeError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        write!(
31            f,
32            "{} has no byte for U+{:04X} ({:?}) at index {}",
33            self.encoding.apple_name(),
34            self.code_point as u32,
35            self.code_point,
36            self.index
37        )
38    }
39}
40
41impl core::error::Error for EncodeError {}
42
43/// The encoding has no character for this byte.
44///
45/// Almost all of Apple's tables have a mapping for all 256 bytes. Thus almost
46/// all encodings do not give this error. [`Encoding::defines_every_byte`]
47/// tells you which encodings have a mapping for all bytes.
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub struct DecodeError {
50    /// The encoding with no code point for [`Self::byte`].
51    pub encoding: Encoding,
52    /// The first byte with no mapping.
53    pub byte: u8,
54    /// The position of [`Self::byte`] in the bytes.
55    pub index: usize,
56}
57
58impl fmt::Display for DecodeError {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(
61            f,
62            "{} has no character for byte {:#04X} at index {}",
63            self.encoding.apple_name(),
64            self.byte,
65            self.index
66        )
67    }
68}
69
70impl core::error::Error for DecodeError {}