1 | /// Transform Rust paths to a readable and comparable string. |
2 | /// |
3 | /// # Limitations |
4 | /// * Leading colons are ignored. |
5 | /// * Angle brackets and `as` elements are ignored. |
6 | /// |
7 | /// # Example |
8 | /// ```rust |
9 | /// # use darling_core::util::path_to_string; |
10 | /// # use syn::parse_quote; |
11 | /// assert_eq!(path_to_string(&parse_quote!(a::b)), "a::b" ); |
12 | /// ``` |
13 | pub fn path_to_string(path: &syn::Path) -> String { |
14 | path.segments |
15 | .iter() |
16 | .map(|s| s.ident.to_string()) |
17 | .collect::<Vec<String>>() |
18 | .join(sep:"::" ) |
19 | } |
20 | |
21 | #[cfg (test)] |
22 | mod tests { |
23 | use syn::parse_quote; |
24 | |
25 | use super::path_to_string; |
26 | |
27 | #[test ] |
28 | fn simple_ident() { |
29 | assert_eq!(path_to_string(&parse_quote!(a)), "a" ); |
30 | } |
31 | |
32 | #[test ] |
33 | fn simple_path() { |
34 | assert_eq!(path_to_string(&parse_quote!(a::b)), "a::b" ); |
35 | } |
36 | } |
37 | |