]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | //! A set of utils methods to reuse on other abstraction levels |
2 | ||
3 | use itertools::Itertools; | |
4 | ||
9ffffee4 | 5 | use crate::{ast, match_ast, AstNode, SyntaxKind}; |
064997fb FG |
6 | |
7 | pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String { | |
8 | path.syntax() | |
9 | .children() | |
10 | .filter_map(|node| { | |
11 | match_ast! { | |
12 | match node { | |
13 | ast::PathSegment(it) => { | |
14 | Some(it.name_ref()?.to_string()) | |
15 | }, | |
16 | ast::Path(it) => { | |
17 | Some(path_to_string_stripping_turbo_fish(&it)) | |
18 | }, | |
19 | _ => None, | |
20 | } | |
21 | } | |
22 | }) | |
23 | .join("::") | |
24 | } | |
25 | ||
9ffffee4 FG |
26 | pub fn is_raw_identifier(name: &str) -> bool { |
27 | let is_keyword = SyntaxKind::from_keyword(name).is_some(); | |
28 | is_keyword && !matches!(name, "self" | "crate" | "super" | "Self") | |
29 | } | |
30 | ||
064997fb FG |
31 | #[cfg(test)] |
32 | mod tests { | |
33 | use super::path_to_string_stripping_turbo_fish; | |
34 | use crate::ast::make; | |
35 | ||
36 | #[test] | |
37 | fn turbofishes_are_stripped() { | |
38 | assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),); | |
39 | assert_eq!( | |
40 | "Vec::new", | |
41 | path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")), | |
42 | ); | |
43 | assert_eq!( | |
44 | "Vec::new", | |
45 | path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")), | |
46 | ); | |
47 | } | |
48 | } |