codama_syn_helpers/extensions/
path.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::ToTokensExtension;
use codama_errors::CodamaResult;
use syn::{Path, PathArguments, PathSegment};

pub trait PathExtension {
    fn get_self(&self) -> &Path;

    /// Returns all segment idents as strings
    fn idents(&self) -> Vec<String> {
        let this = self.get_self();
        this.segments
            .iter()
            .map(|segment| segment.ident.to_string())
            .collect::<Vec<_>>()
    }

    /// Returns all segment idents joined by "::".
    /// E.g. for `a::b<B>::c::Option<T>` it returns `a::b::c::Option`.
    fn to_string(&self) -> String {
        self.idents().join("::")
    }

    /// Returns all segment idents joined by "::" except the last one.
    /// E.g. for `a::b<B>::c::Option<T>` it returns `a::b::c`.
    fn prefix(&self) -> String {
        let idents = self.idents();
        idents[..idents.len() - 1].join("::")
    }

    /// Returns the last segment.
    fn last(&self) -> &PathSegment {
        self.get_self().segments.last().unwrap()
    }

    /// Returns the ident of the last segment as a string.
    fn last_str(&self) -> String {
        self.last().ident.to_string()
    }

    /// Returns true if the path is equal to the given path including or excluding the prefix.
    fn is(&self, path: &str) -> bool {
        let mut segments = path.split("::").collect::<Vec<_>>();
        let last = segments.pop().unwrap();
        let prefix = segments.join("::");
        let this_prefix = self.prefix();
        (this_prefix == prefix || this_prefix.is_empty()) && last == self.last_str()
    }

    /// Returns true if the path is equal to the given path including the prefix.
    fn is_strict(&self, path: &str) -> bool {
        let mut segments = path.split("::").collect::<Vec<_>>();
        let last = segments.pop().unwrap();
        let prefix = segments.join("::");
        prefix == self.prefix() && last == self.last_str()
    }

    /// Returns the generic arguments of the last segment.
    /// E.g. for `a::b::c::Option<'a, T, U>` it returns `GenericArguments(Some(['a, T, U]))`.
    /// E.g. for `a::b::c::u32` it returns `GenericArguments(None)`.
    fn generic_arguments(&self) -> Vec<&syn::GenericArgument> {
        match &self.last().arguments {
            PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { args, .. }) => {
                args.iter().collect()
            }
            _ => vec![],
        }
    }

    /// Filters out all generic arguments that are not types.
    /// E.g. for `Option<'a, T, U>` it returns `[T, U]`.
    fn generic_types(&self) -> Vec<&syn::Type> {
        self.generic_arguments()
            .iter()
            .filter_map(|arg| match arg {
                syn::GenericArgument::Type(ty) => Some(ty),
                _ => None,
            })
            .collect()
    }

    /// Returns the first generic type argument if there is one.
    /// E.g. for `Vec<'a, T, U>` it returns `Ok(T)`.
    fn first_generic_type(&self) -> CodamaResult<&syn::Type> {
        let this = self.get_self();
        self.generic_types()
            .first()
            .copied()
            .ok_or_else(|| this.error("expected at least one generic type").into())
    }

    /// Returns the first generic type argument if there is exactly one.
    /// E.g. for `Vec<'a, T>` it returns `Ok(T)`.
    fn single_generic_type(&self) -> CodamaResult<&syn::Type> {
        let this = self.get_self();
        if self.generic_types().len() != 1 {
            return Err(this.error("expected a single generic type".to_string()).into());
        }
        self.first_generic_type()
    }
}

impl PathExtension for Path {
    fn get_self(&self) -> &Path {
        self
    }
}

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

    #[test]
    fn idents() {
        let path: Path = syn::parse_quote! { std::option<Foo>::Option<String> };
        assert_eq!(path.idents(), vec!["std", "option", "Option"]);
    }

    #[test]
    fn to_string() {
        let path: Path = syn::parse_quote! { std::option<Foo>::Option<String> };
        assert_eq!(path.to_string(), "std::option::Option");
    }

    #[test]
    fn prefix() {
        let path: Path = syn::parse_quote! { std::option<Foo>::Option<String> };
        assert_eq!(path.prefix(), "std::option");
    }

    #[test]
    fn prefix_with_inner_generics() {
        let path: Path = syn::parse_quote! { a<A>::b<B>::c::Final };
        assert_eq!(path.prefix(), "a::b::c");
    }

    #[test]
    fn prefix_empty() {
        let path: Path = syn::parse_quote! { Foo };
        assert_eq!(path.prefix(), "");
    }

    #[test]
    fn is() {
        let path: Path = syn::parse_quote! { prefix::Foo<'a, T> };
        assert!(path.is("prefix::Foo"));
        assert!(!path.is("Foo"));
        assert!(!path.is("wrong::prefix::Foo"));
        assert!(!path.is("Bar"));

        let path: Path = syn::parse_quote! { Foo<T> };
        assert!(path.is("Foo"));
        assert!(path.is("prefix::Foo"));
        assert!(!path.is("Bar"));
    }

    #[test]
    fn is_strict() {
        let path: Path = syn::parse_quote! { prefix::Foo<'a, T> };
        assert!(path.is_strict("prefix::Foo"));
        assert!(!path.is_strict("Foo"));
        assert!(!path.is_strict("wrong::prefix::Foo"));
        assert!(!path.is_strict("Bar"));

        let path: Path = syn::parse_quote! { Foo<T> };
        assert!(path.is_strict("Foo"));
        assert!(!path.is_strict("prefix::Foo"));
        assert!(!path.is_strict("Bar"));
    }

    #[test]
    fn generic_arguments() {
        let path: Path = syn::parse_quote! { prefix::Foo<'a, T, U> };
        assert_eq!(path.generic_arguments().len(), 3);
    }

    #[test]
    fn generic_types() {
        let path: Path = syn::parse_quote! { prefix::Foo<'a, T, U> };
        assert_eq!(path.generic_types().len(), 2);
    }

    #[test]
    fn first_generic_type_ok() {
        let path: Path = syn::parse_quote! { prefix::Foo<'a, T, U> };
        assert!(matches!(path.first_generic_type(), Ok(syn::Type::Path(_))));
    }

    #[test]
    fn first_generic_type_err() {
        let path: Path = syn::parse_quote! { prefix::Foo<'a> };
        assert!(path.first_generic_type().is_err());
    }

    #[test]
    fn single_generic_type_ok() {
        let path: Path = syn::parse_quote! { Foo<'a, String> };
        assert!(matches!(path.single_generic_type(), Ok(syn::Type::Path(_))));
    }

    #[test]
    fn single_generic_type_err() {
        let path: Path = syn::parse_quote! { Foo<'a, String, u32> };
        assert!(path.single_generic_type().is_err());

        let path: Path = syn::parse_quote! { Foo<'a> };
        assert!(path.single_generic_type().is_err());
    }
}