darling_core/util/
path_to_string.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
/// Transform Rust paths to a readable and comparable string.
///
/// # Limitations
/// * Leading colons are ignored.
/// * Angle brackets and `as` elements are ignored.
///
/// # Example
/// ```rust
/// # use darling_core::util::path_to_string;
/// # use syn::parse_quote;
/// assert_eq!(path_to_string(&parse_quote!(a::b)), "a::b");
/// ```
pub fn path_to_string(path: &syn::Path) -> String {
    path.segments
        .iter()
        .map(|s| s.ident.to_string())
        .collect::<Vec<String>>()
        .join("::")
}

#[cfg(test)]
mod tests {
    use syn::parse_quote;

    use super::path_to_string;

    #[test]
    fn simple_ident() {
        assert_eq!(path_to_string(&parse_quote!(a)), "a");
    }

    #[test]
    fn simple_path() {
        assert_eq!(path_to_string(&parse_quote!(a::b)), "a::b");
    }
}