mod mutf8;
#[cfg(feature = "use-structs")]
mod str;
pub use mutf8::mutf8_to_utf8;
pub use mutf8::utf8_to_mutf8;
#[cfg(feature = "use-structs")]
pub use crate::str::MString;
#[cfg(feature = "use-structs")]
pub use crate::str::mstr;
pub mod error {
use std::fmt::{Display, Formatter, Result as FResult};
use std::string::FromUtf8Error;
use std::str::Utf8Error;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub enum Error {
EndOfInput(Mode, Expected, Position),
InvalidUtf8 {
bytes: Option<Vec<u8>>,
error: Utf8Error,
},
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
match self {
Self::EndOfInput(Mode::Encoding, Expected::TwoByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to encode a two byte encoding. (Second byte)]"),
Self::EndOfInput(Mode::Encoding, Expected::ThreeByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to encode a three byte encoding. (Second byte)]"),
Self::EndOfInput(Mode::Encoding, Expected::ThreeByte, Position::Three) => f.write_str("Unexpected end of input. [Unable to encode a three byte encoding. (Third byte)]"),
Self::EndOfInput(Mode::Encoding, Expected::FourByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to encode a four byte encoding. (Second byte)]"),
Self::EndOfInput(Mode::Encoding, Expected::FourByte, Position::Three) => f.write_str("Unexpected end of input. [Unable to encode a four byte encoding. (Third byte)]"),
Self::EndOfInput(Mode::Encoding, Expected::FourByte, Position::Four) => f.write_str("Unexpected end of input. [Unable to encode a four byte encoding. (Fourth byte)]"),
Self::EndOfInput(Mode::Decoding, Expected::TwoByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to decode a two byte encoding. (Second byte)]"),
Self::EndOfInput(Mode::Decoding, Expected::ThreeByte, Position::Two) => f.write_str("Unexpected end of input. [Unable to decode a three byte encoding. (Second byte)]"),
Self::EndOfInput(Mode::Decoding, Expected::ThreeByte, Position::Three) => f.write_str("Unexpected end of input. [Unable to decode a three byte encoding. (Third byte)]"),
Self::EndOfInput(Mode::Decoding, Expected::SixByte, Position::Four) => f.write_str("Unexpected end of input. [Unable to decode a six byte encoding. (Fourth byte)]"),
Self::EndOfInput(Mode::Decoding, Expected::SixByte, Position::Five) => f.write_str("Unexpected end of input. [Unable to decode a six byte encoding. (Fifth byte)]"),
Self::EndOfInput(Mode::Decoding, Expected::SixByte, Position::Six) => f.write_str("Unexpected end of input. [Unable to decode a six byte encoding. (Sixth byte)]"),
Self::InvalidUtf8 {
bytes: _,
error
} => {
f.write_str("Invalid UTF-8 input. [Failed to decode string into UTF-8 (")?;
Display::fmt(error, f)?;
f.write_str(")]")
},
_ => unreachable!(),
}
}
}
impl std::error::Error for Error {
}
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Self {
Error::InvalidUtf8 {
bytes: None,
error: err
}
}
}
impl From<FromUtf8Error> for Error {
fn from(err: FromUtf8Error) -> Self {
let error = err.utf8_error();
let bytes = err.into_bytes();
Error::InvalidUtf8 {
bytes: Some(bytes),
error,
}
}
}
#[derive(Debug)]
pub enum Mode {
Encoding,
Decoding,
}
#[derive(Debug)]
pub enum Expected {
TwoByte,
ThreeByte,
FourByte,
SixByte,
}
#[derive(Debug)]
pub enum Position {
Two,
Three,
Four,
Five,
Six,
}
}