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
//! The selector for a given search, with its trait implementations.

use crate::search::Search;
use crate::Error;
use std::fmt;
use std::str::FromStr;
use syn::{Ident, Item};

/// The path provided by the user to search for.
///
/// Not all Rust paths are valid selectors; UFCS and generics are not supported.
#[derive(Debug, Clone)]
pub struct Selector {
    segments: Vec<SelectorSegment>,
}

impl Selector {
    /// Create a new `Selector` by parsing the passed-in string.
    ///
    /// # Usage
    /// ```rust,edition2018
    /// use syn_select::Selector;
    /// let selector = Selector::try_from("hello::world").unwrap();
    /// assert_eq!(format!("{}", selector), "hello::world".to_owned());
    /// ```
    pub fn try_from(s: impl AsRef<str>) -> Result<Self, Error> {
        s.as_ref().parse()
    }

    /// Use this selector to search a file, returning the list of items that match the selector.
    pub fn apply_to(&self, file: &syn::File) -> Vec<Item> {
        let mut search = Search::new(self);
        search.search_file(file);
        search.results
    }

    pub(crate) fn part(&self, index: usize) -> &SelectorSegment {
        &self.segments[index]
    }

    pub(crate) fn len(&self) -> usize {
        self.segments.len()
    }
}

impl fmt::Display for Selector {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.segments[0])?;
        for segment in self.segments.iter().skip(1) {
            write!(f, "::{}", segment)?;
        }

        Ok(())
    }
}

impl FromStr for Selector {
    type Err = Error;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        let mut segments = Vec::new();

        if input.trim() == "" {
            return Err(Error::empty_path());
        }

        for segment in input.split("::") {
            match segment.parse() {
                Ok(seg) => segments.push(seg),
                Err(_) => return Err(Error::invalid_segment(segment.into())),
            }
        }

        Ok(Selector { segments })
    }
}

/// One segment of a selector path
#[derive(Debug, Clone)]
pub(crate) enum SelectorSegment {
    /// A specific ident that must be exactly equal to match.
    Ident(String),
    /// A wildcard that matches any ident.
    Wildcard,
}

impl FromStr for SelectorSegment {
    type Err = Error;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        if input == "_" {
            return Ok(SelectorSegment::Wildcard);
        }

        syn::parse_str::<Ident>(input)
            .map(|ident| SelectorSegment::Ident(ident.to_string()))
            .map_err(|_| Error::invalid_segment(input.into()))
    }
}

impl PartialEq<Ident> for SelectorSegment {
    fn eq(&self, other: &Ident) -> bool {
        match self {
            SelectorSegment::Wildcard => true,
            SelectorSegment::Ident(ident) => other == ident,
        }
    }
}

impl fmt::Display for SelectorSegment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            SelectorSegment::Wildcard => "_".fmt(f),
            SelectorSegment::Ident(ident) => ident.fmt(f),
        }
    }
}