aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/utils.rs
blob: f4c02518b4c76a2ed708bac285872018bc2cc507 (plain)
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
//! A set of utils methods to reuse on other abstraction levels

use itertools::Itertools;

use crate::{ast, match_ast, AstNode};

pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
    path.syntax()
        .children()
        .filter_map(|node| {
            match_ast! {
                match node {
                    ast::PathSegment(it) => {
                        Some(it.name_ref()?.to_string())
                    },
                    ast::Path(it) => {
                        Some(path_to_string_stripping_turbo_fish(&it))
                    },
                    _ => None,
                }
            }
        })
        .join("::")
}

#[cfg(test)]
mod tests {
    use super::path_to_string_stripping_turbo_fish;
    use crate::ast::make;

    #[test]
    fn turbofishes_are_stripped() {
        assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),);
        assert_eq!(
            "Vec::new",
            path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")),
        );
        assert_eq!(
            "Vec::new",
            path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")),
        );
    }
}