stringcue 0.1.1

String Compressed Unicode Encoding - encode unicode more space efficient than utf-8 by sacrificing robustness like byte-boundaries.
Documentation
//! Errors returned when decoding stringcue blobs.

use core::fmt;

/// An error produced while decoding a stringcue blob.
///
/// A single bit error can corrupt a whole blob, so decoding failures carry
/// no recovery guarantee; re-encode the original strings instead of
/// attempting to fix up a blob.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DecodeError {
    /// The blob ended in the middle of an encoded codepoint.
    Truncated,
    /// The blob contained bits that do not form a valid Unicode codepoint.
    InvalidCodepoint,
}

impl fmt::Display for DecodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Truncated => write!(f, "stringcue data ends mid-codepoint"),
            Self::InvalidCodepoint => write!(f, "stringcue data is not valid unicode"),
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::ToString;

    #[test]
    fn display_mentions_the_problem() {
        assert_eq!(
            DecodeError::Truncated.to_string(),
            "stringcue data ends mid-codepoint"
        );
    }

    #[test]
    fn is_std_error() {
        fn assert_error<E: core::error::Error>(_: &E) {}
        assert_error(&DecodeError::InvalidCodepoint);
    }
}