stringcue 0.1.1

String Compressed Unicode Encoding - encode unicode more space efficient than utf-8 by sacrificing robustness like byte-boundaries.
Documentation
//! Options for encoding and decoding stringcue blobs.
//!
//! Blobs are always padded with `1` bits up to the next byte boundary after
//! the final encoded codepoint (and terminator); there is no alignment
//! option.

/// Whether a `CONTROL` terminator is appended to the encoded blob, and how
/// empty elements are treated.
///
/// The terminator is a single `CONTROL` for `encode` and two consecutive
/// `CONTROL`s for `encode_all`. Empty elements encode to *zero* codepoints
/// between consecutive separators, i.e. also to consecutive `CONTROL`s, so
/// they are only representable without an in-band terminator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EncodeTerminator {
    /// Append the terminator after the final element.
    ///
    /// Empty elements are *silently dropped* by `encode_all` under in-band
    /// termination: with a terminator they cannot be encoded, and encoding
    /// has no error channel to report the loss. Use
    /// [`EncodeTerminator::Omit`] to encode or skip empty elements.
    Write,
    /// Do not append a terminator (default) and decide how empty elements
    /// are treated by `encode_all`.
    Omit(EncodeEmpties),
}

impl Default for EncodeTerminator {
    fn default() -> Self {
        Self::Omit(EncodeEmpties::default())
    }
}

/// Treatment of empty elements by `encode_all`.
///
/// With [`EncodeTerminator::Write`], empty elements are unrepresentable and
/// therefore dropped, and [`EncodeTerminator::Omit`] is the only place they
/// can be configured.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum EncodeEmpties {
    /// Encode empty elements as zero codepoints between consecutive
    /// separators.
    Encode,
    /// Drop empty elements when encoding (default).
    #[default]
    Skip,
}

/// Whether a `CONTROL` terminator is required, tolerated or refused when
/// decoding a blob, and how empty elements are read without one.
///
/// The terminator is a single `CONTROL` at the end of a `decode` blob and
/// two consecutive `CONTROL`s at the end of a `decode_all` blob. Consecutive
/// `CONTROL`s are only meaningful as empty elements, so
/// [`DecodeTerminator::Refuse`] is the only variant that can read them:
/// with [`DecodeTerminator::Require`] or [`DecodeTerminator::Tolerate`], a
/// terminator at the end of a blob is indistinguishable from a trailing
/// empty element.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum DecodeTerminator {
    /// Require the terminator; a blob without it is an error.
    Require,
    /// Accept a terminator at the end of the blob, but do not require one
    /// (default).
    #[default]
    Tolerate,
    /// Treat a terminator as an error and decide how consecutive
    /// `CONTROL`s are read by `decode_all`.
    Refuse(DecodeEmpties),
}

/// Treatment of empty elements by `decode_all`.
///
/// Only meaningful with [`DecodeTerminator::Refuse`]; with
/// [`DecodeTerminator::Require`] or [`DecodeTerminator::Tolerate`], empty
/// elements are indistinguishable from a terminator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum DecodeEmpties {
    /// Decode empty elements to `String`s.
    Keep,
    /// Skip empty elements and continue (default).
    #[default]
    Skip,
    /// Treat consecutive `CONTROL`s as an error.
    Reject,
}

/// Options for encoding; see [`crate::encode`] and [`crate::encode_all`].
///
/// Defaults: write no `CONTROL` terminator to the
/// output and drop empty elements instead of encoding them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct EncodeOptions {
    /// Whether to append a `CONTROL` terminator.
    pub terminator: EncodeTerminator,
}

/// Options for decoding; see [`crate::decode`] and [`crate::decode_all`].
///
/// Defaults: accept a superfluous terminator on input.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct DecodeOptions {
    /// How to treat a `CONTROL` terminator on input.
    pub terminator: DecodeTerminator,
}

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

    #[test]
    fn default_options_match_the_readme() {
        let encode = EncodeOptions::default();
        assert_eq!(
            encode.terminator,
            EncodeTerminator::Omit(EncodeEmpties::Skip)
        );

        let decode = DecodeOptions::default();
        assert_eq!(decode.terminator, DecodeTerminator::Tolerate);
    }
}