File: from_type_param_default.rs

package info (click to toggle)
rust-darling 0.20.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: makefile: 2; sh: 1
file content (53 lines) | stat: -rw-r--r-- 1,403 bytes parent folder | download | duplicates (47)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use darling::FromTypeParam;
use syn::{parse_quote, DeriveInput, GenericParam, TypeParam};

#[derive(Default, FromTypeParam)]
#[darling(attributes(lorem), default)]
struct Lorem {
    foo: bool,
    bar: Option<String>,
    default: Option<syn::Type>,
}

fn extract_type(param: &GenericParam) -> &TypeParam {
    match *param {
        GenericParam::Type(ref ty) => ty,
        _ => unreachable!("Not a type param"),
    }
}

#[test]
#[allow(clippy::bool_assert_comparison)]
fn expand_many() {
    let di: DeriveInput = parse_quote! {
        struct Baz<
            #[lorem(foo)] T,
            #[lorem(bar = "x")] U: Eq + ?Sized,
            #[lorem(foo = false)] V = (),
        >(T, U, V);
    };
    let params = di.generics.params;

    {
        let ty = extract_type(&params[0]);
        let lorem = Lorem::from_type_param(ty).unwrap();
        assert_eq!(lorem.foo, true);
        assert_eq!(lorem.bar, None);
    }

    {
        let ty = extract_type(&params[1]);
        let lorem = Lorem::from_type_param(ty).unwrap();
        assert_eq!(lorem.foo, false);
        assert_eq!(lorem.bar, Some("x".to_string()));
        assert!(lorem.default.is_none());
    }

    {
        let ty = extract_type(&params[2]);
        let lorem = Lorem::from_type_param(ty).unwrap();
        assert_eq!(lorem.foo, false);
        assert_eq!(lorem.bar, None);
        assert!(lorem.default.is_some());
    }
}