assert_parse_core/
lib.rs

1use proc_macro2::TokenStream as TokenStream2;
2use std::fmt::Display;
3use std::marker::PhantomData;
4use syn::parse::Parse;
5
6/// The container struct for assert method and its type.
7pub struct Assert<T: Parse, U: Display> {
8    _t: PhantomData<T>,
9    _u: PhantomData<U>,
10}
11
12/// This should be use with type signature.
13pub fn make_assert<T, U>() -> Assert<T, U>
14where
15    T: Parse,
16    U: Display,
17{
18    Assert::<T, U> {
19        _t: PhantomData,
20        _u: PhantomData,
21    }
22}
23
24impl<T: Parse, U: Display> Assert<T, U> {
25    /// # Panics
26    ///
27    /// * when it is success to parse
28    /// * when there is different in error message
29    pub fn error(&self, arg: TokenStream2, error: U) {
30        let arg: syn::Result<T> = syn::parse2(arg);
31        match arg {
32            Ok(_) => {
33                panic!("must occurre error.");
34            }
35            Err(e) => {
36                assert_eq!(e.to_string(), error.to_string());
37            }
38        }
39    }
40
41    /// # Panics
42    ///
43    /// * when it is failed to parse
44    /// * when it is failed to finish to call assert
45    pub fn ok<V>(&self, arg: TokenStream2, assert: V)
46    where
47        V: FnOnce(T),
48    {
49        let arg: syn::Result<T> = syn::parse2(arg);
50        match arg {
51            Ok(a) => {
52                assert(a);
53            }
54            Err(_) => {
55                panic!("must not occurre error.");
56            }
57        }
58    }
59}