typeward 0.5.1

A type-driven parser combinator library for Rust.
Documentation
//! Typeward's unpacking integration.
//!
//! The [`Unpack`] trait itself is defined by the [`unpackable`] crate and is
//! re-exported here so that [`crate::parse::Unpack`] remains the canonical path.
//! See the [`crate::parse`] module documentation for the typeward-specific
//! conversion rules.

pub use unpackable::{Unpack, Unpackable};

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

    type Quoted = DelimitedExact<Ws<DoubleQuote>, DoubleQuote, TakeTillToken<DoubleQuote>>;
    type Values = Ws<BracketedList<Ws<i64>, Ws<Comma>>>;
    type Member = and!(Quoted, Ws<Colon>, Ws<i64>);

    #[test]
    fn unpacks_quoted_string() {
        let (value, rest) = Quoted::parse(r#""hello""#).unwrap();
        assert_eq!(rest, "");
        let value: String = value.unpack();
        assert_eq!(value, "hello");
    }

    #[test]
    fn unpacks_nested_list() {
        let (value, rest) = Values::parse("[1, 2, 3]").unwrap();
        assert_eq!(rest, "");
        let value: Vec<i64> = value.unpack();
        assert_eq!(value, [1, 2, 3]);
    }

    #[test]
    fn unpacks_nested_option() {
        let value = Ws::<Option<Ws<i64>>>::parse("42 43").unwrap().0;
        let value: Option<i64> = value.unpack();
        assert_eq!(value, Some(42));
    }

    #[test]
    fn unpacks_filtered_string() {
        let (value, rest) = Ws::<Identifier>::parse("name").unwrap();
        assert_eq!(rest, "");
        let value: String = value.unpack();
        assert_eq!(value, "name");
    }

    #[test]
    fn unpacks_and_into_tuple() {
        let (value, rest) = Member::parse(r#""x": 1"#).unwrap();
        assert_eq!(rest, "");
        let (key, _colon, value): (String, Colon, i64) = value.unpack();
        assert_eq!(key, "x");
        assert_eq!(value, 1);
    }
}