use core::fmt::Display;

/// Error produced when unescaping from a haystack.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum Error {
    /// The haystack ends with a standalone backslash.
    TrailingBackslash,
    /// Found an unknown escape signal (e.g. '\a').
    UnknownEscapeSignal(u8),
    /// Found an unicode escape signal '\u' but not following with a braced value.
    UnbracedUnicode,
    /// The bytes are not hex representation of a valid unicode character.
    InvalidUnicodeHex,
    /// Characters followed by `\x` are not exactly one byte.
    NotOneByte,
}

impl core::error::Error for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::TrailingBackslash => write!(f, "haystack ends with a standalone backslash"),
            Self::UnknownEscapeSignal(sig) => {
                write!(f, "found an unknown escape signal '\\{}'", *sig as char)
            }
            Self::UnbracedUnicode => write!(
                f,
                "found an unicode escape signal '\\u' but not following with a braced value"
            ),
            Self::InvalidUnicodeHex => write!(
                f,
                "the bytes are not hex representation of a valid unicode character"
            ),
            Self::NotOneByte => write!(f, "characters followed by `\\x` are not exactly one byte"),
        }
    }
}
