use std::{borrow::Cow, marker::PhantomData};
pub trait Unpackable {}
pub trait Unpack<T> {
fn unpack(self) -> T;
}
impl<T: Unpackable> Unpack<T> for T {
#[inline]
fn unpack(self) -> T {
self
}
}
macro_rules! impl_unpack_identity {
($($ty:ty),+ $(,)?) => {
$(
impl Unpackable for $ty {}
)+
};
}
impl_unpack_identity!(
(),
char,
&str,
String,
Cow<'_, str>,
bool,
u8,
u16,
u32,
u64,
u128,
usize,
i8,
i16,
i32,
i64,
i128,
isize,
f32,
f64,
);
impl<T, U> Unpack<Vec<U>> for Vec<T>
where
T: Unpack<U>,
{
#[inline]
fn unpack(self) -> Vec<U> {
self.into_iter().map(crate::parse::Unpack::unpack).collect()
}
}
impl<T, U> Unpack<Option<U>> for Option<T>
where
T: Unpack<U>,
{
#[inline]
fn unpack(self) -> Option<U> {
self.map(crate::parse::Unpack::unpack)
}
}
macro_rules! unpack_tuple {
($($ty:ty),*) => {
pastey::paste! {
impl<$($ty),* , $([< T $ty >]),*> Unpack<($([< T $ty >]),*)> for ($($ty),*)
where
$($ty: Unpack<[< T $ty >]>),*
{
#[inline]
fn unpack(self) -> ($([< T $ty >]),*) {
let ($([<$ty:lower>]),*) = self;
($([<$ty:lower>].unpack()),*)
}
}
}
};
}
for_all_tuples!(unpack_tuple);
impl<T> Unpackable for PhantomData<T> {}
impl<T> Unpack<()> for PhantomData<T> {
#[inline]
fn unpack(self) {}
}
#[cfg(test)]
mod tests {
use std::marker::PhantomData;
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 identity_unpacks_to_itself() {
let value: i64 = Unpack::unpack(42);
assert_eq!(value, 42);
}
#[test]
fn tuple_unpacks_to_tuple() {
let value: (i64, i64) = Unpack::unpack((42, 43));
assert_eq!(value, (42, 43));
}
#[test]
fn vec_unpacks_wrapped_values() {
let values: Vec<Ws<i64>> = vec![Ws(1), Ws(2)];
let values: Vec<i64> = values.unpack();
assert_eq!(values, [1, 2]);
}
#[test]
fn option_unpacks_wrapped_value() {
let value: Option<Ws<i64>> = Some(Ws(7));
let value: Option<i64> = value.unpack();
assert_eq!(value, Some(7));
}
#[test]
fn box_unpacks_transparently() {
let value: Box<Ws<i64>> = Box::new(Ws(7));
let value: i64 = value.unpack();
assert_eq!(value, 7);
}
#[test]
fn phantom_data_unpacks_to_unit_and_itself() {
let unit: () = PhantomData::<i64>.unpack();
assert_eq!(unit, ());
let identity: PhantomData<i64> = PhantomData::<i64>.unpack();
assert_eq!(identity, PhantomData);
}
#[test]
fn tuple_unpacks_wrapped_values() {
let value: (i64, i64) = Unpack::unpack((Ws(42), Ws(43)));
assert_eq!(value, (42, 43));
}
#[test]
fn tuple_unpacks_one_wrapped_value() {
let value: (i64, i64) = Unpack::unpack((42, Ws(43)));
assert_eq!(value, (42, 43));
}
#[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);
}
}