abe/
abe_macro.rs

1// Copyright Anton Sol
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6#[macro_export]
7macro_rules! abe_item {
8    () => {
9        core::iter::empty()
10    };
11    ( : ) => {
12        core::iter::once($crate::ast::ABE::Ctr($crate::ast::Ctr::Colon))
13    };
14    ( / ) => {
15        core::iter::once($crate::ast::ABE::Ctr($crate::ast::Ctr::FSlash))
16    };
17    // ( $e:ident ) => { ABE::Expr(stringify!($e).into())};
18    //( $e:expr ) => { core::iter::once($crate::abe::ABE::Expr( $crate::abe::Expr::from($e)))};
19    ( $e:expr ) => {
20        core::iter::once($crate::ast::ABE::Expr($e.into()))
21    };
22}
23#[macro_export]
24macro_rules! abe {
25    ( ) => { core::iter::empty::<$crate::ast::ABE>() };
26    ( <$t:ty> $($arg:tt)*) => {
27        $crate::TypedABE::<$t>::from($crate::abe!($($arg)*).collect()).unwrap()
28    };
29    ( +( $($inner:tt)* ) $($arg:tt)*) => {
30        $($inner)*.into_iter().chain( $crate::abe!($($arg)*))
31    };
32    ( { $($inner:tt)* } $($arg:tt)*) => {
33        core::iter::once($crate::ast::ABE::from_iter($crate::abe!($($inner)*))).chain( $crate::abe!($($arg)*))
34    };
35    ( $a:tt $($arg:tt)*) => {
36        $crate::abe_item!($a).chain( $crate::abe!($($arg)*))
37    };
38}
39#[macro_export]
40macro_rules! abev {
41    ( $($arg:tt)*) => {
42        $crate::abe!($($arg)*).collect::<Vec<$crate::ast::ABE>>()
43    };
44}
45
46#[test]
47fn test() {
48    use crate::ast::*;
49    let ext: Vec<ABE> = abe!("OK").collect();
50    let _v: ABE = abe!( "#" : / : {/} :).collect();
51
52    let _ok = abe!(<Vec<u8>> "prefix" : "=" : );
53
54    let opt1: ABE = abe!( "#" : / : {/} :)
55        .chain(ext.clone().into_iter())
56        .collect();
57    let v: ABE = abe!( "#" : / : {/} : +(ext.into_iter())).collect();
58    assert_eq!(opt1, v);
59}