1mod mutf8;
8
9#[cfg(feature = "use-structs")]
10mod str;
11
12pub use mutf8::mutf8_to_utf8;
13pub use mutf8::utf8_to_mutf8;
14
15#[cfg(feature = "use-structs")]
16pub use crate::str::MString;
17
18#[cfg(feature = "use-structs")]
19pub use crate::str::mstr;
20
21
22pub mod error {
23 use std::fmt::{Display, Formatter, Result as FResult};
24 use std::string::FromUtf8Error;
25 use std::str::Utf8Error;
26
27 pub type Result<T, E = Error> = std::result::Result<T, E>;
29
30 #[derive(Debug)]
31 pub enum Error {
32 EndOfInput(Mode, Expected, Position),
33 InvalidUtf8 {
34 bytes: Option<Vec<u8>>,
35 error: Utf8Error,
36 },
37 }
38
39 impl Display for Error {
40 fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
41 match self {
42 Self::EndOfInput(Mode::Encoding, Expected::TwoByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to encode a two byte encoding. (Second byte)]"),
43 Self::EndOfInput(Mode::Encoding, Expected::ThreeByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to encode a three byte encoding. (Second byte)]"),
44 Self::EndOfInput(Mode::Encoding, Expected::ThreeByte, Position::Three) => f.write_str("Unexpected end of input. [Unable to encode a three byte encoding. (Third byte)]"),
45 Self::EndOfInput(Mode::Encoding, Expected::FourByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to encode a four byte encoding. (Second byte)]"),
46 Self::EndOfInput(Mode::Encoding, Expected::FourByte, Position::Three) => f.write_str("Unexpected end of input. [Unable to encode a four byte encoding. (Third byte)]"),
47 Self::EndOfInput(Mode::Encoding, Expected::FourByte, Position::Four) => f.write_str("Unexpected end of input. [Unable to encode a four byte encoding. (Fourth byte)]"),
48
49 Self::EndOfInput(Mode::Decoding, Expected::TwoByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to decode a two byte encoding. (Second byte)]"),
50 Self::EndOfInput(Mode::Decoding, Expected::ThreeByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to decode a three byte encoding. (Second byte)]"),
51 Self::EndOfInput(Mode::Decoding, Expected::ThreeByte, Position::Three) => f.write_str("Unexpected end of input. [Unable to decode a three byte encoding. (Third byte)]"),
52 Self::EndOfInput(Mode::Decoding, Expected::SixByte, Position::Four) => f.write_str("Unexpected end of input. [Unable to decode a six byte encoding. (Fourth byte)]"),
53 Self::EndOfInput(Mode::Decoding, Expected::SixByte, Position::Five) => f.write_str("Unexpected end of input. [Unable to decode a six byte encoding. (Fifth byte)]"),
54 Self::EndOfInput(Mode::Decoding, Expected::SixByte, Position::Six) => f.write_str("Unexpected end of input. [Unable to decode a six byte encoding. (Sixth byte)]"),
55
56 Self::InvalidUtf8 {
57 bytes: _,
58 error
59 } => {
60 f.write_str("Invalid UTF-8 input. [Failed to decode string into UTF-8 (")?;
61 Display::fmt(error, f)?;
62 f.write_str(")]")
63 },
64
65 _ => unreachable!(),
66 }
67 }
68 }
69
70 impl std::error::Error for Error {
71 }
72
73 impl From<Utf8Error> for Error {
74 fn from(err: Utf8Error) -> Self {
75 Error::InvalidUtf8 {
76 bytes: None,
77 error: err
78 }
79 }
80 }
81
82 impl From<FromUtf8Error> for Error {
83 fn from(err: FromUtf8Error) -> Self {
84 let error = err.utf8_error();
85 let bytes = err.into_bytes();
86 Error::InvalidUtf8 {
87 bytes: Some(bytes),
88 error,
89 }
90 }
91 }
92
93 #[derive(Debug)]
96 pub enum Mode {
97 Encoding,
99 Decoding,
101 }
102
103 #[derive(Debug)]
105 pub enum Expected {
106 TwoByte,
109 ThreeByte,
114 FourByte,
117 SixByte,
124 }
125
126 #[derive(Debug)]
132 pub enum Position {
133 Two,
134 Three,
135 Four,
136 Five,
137 Six,
138 }
139}