codama_syn_helpers/extensions/
type.rs

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use super::{PathExtension, ToTokensExtension};
use codama_errors::CodamaResult;
use syn::Type;

pub trait TypeExtension {
    fn get_self(&self) -> &Type;

    fn as_path(&self) -> CodamaResult<&syn::Path> {
        let this = self.get_self();
        match this {
            Type::Path(path) => Ok(&path.path),
            _ => Err(this.error("expected a path").into()),
        }
    }

    fn single_generic_type_from_path(&self, path: &str) -> CodamaResult<&Type> {
        let this = self.as_path()?;
        match this.is(path) {
            true => this.single_generic_type(),
            false => Err(this.error(format!("expected path: {}", path)).into()),
        }
    }
}

impl TypeExtension for Type {
    fn get_self(&self) -> &Type {
        self
    }
}

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

    #[test]
    fn as_path_ok() {
        let r#type: Type = syn::parse_quote! { std::option::Option<String> };
        assert!(r#type.as_path().is_ok());
    }

    #[test]
    fn as_path_err() {
        let r#type: Type = syn::parse_quote! { [u8; 32] };
        assert!(r#type.as_path().is_err());
    }

    #[test]
    fn single_generic_type_from_path_ok() {
        let r#type: Type = syn::parse_quote! { std::option::Option<String> };
        assert!(r#type.single_generic_type_from_path("std::option::Option").is_ok());

        let r#type: Type = syn::parse_quote! { Option<String> };
        assert!(r#type.single_generic_type_from_path("std::option::Option").is_ok());
        assert!(r#type.single_generic_type_from_path("Option").is_ok());
    }

    #[test]
    fn single_generic_type_from_path_err() {
        let r#type: Type = syn::parse_quote! { [u8; 32] };
        assert!(r#type.single_generic_type_from_path("Option").is_err());

        let r#type: Type = syn::parse_quote! { std::option::Option<String> };
        assert!(r#type.single_generic_type_from_path("Option").is_err());
        assert!(r#type.single_generic_type_from_path("wrong::prefix::Option").is_err());

        let r#type: Type = syn::parse_quote! { Option<String, u32> };
        assert!(r#type.single_generic_type_from_path("Option").is_err());
    }
}