summaryrefslogtreecommitdiff
path: root/src/model/selector.rs
blob: c1528c0a33bc3049c318a233b2c7f5b4774ef451 (plain) (blame)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std::any::{Any, TypeId};
use std::fmt::{self, Debug, Formatter, Write};
use std::sync::Arc;

use ecow::{eco_format, EcoString, EcoVec};

use super::{Content, ElemFunc, Label, Location};
use crate::diag::StrResult;
use crate::eval::{
    cast, CastInfo, Dict, FromValue, Func, IntoValue, Reflect, Regex, Value,
};
use crate::model::Locatable;
use crate::util::pretty_array_like;

/// A selector in a show rule.
#[derive(Clone, PartialEq, Hash)]
pub enum Selector {
    /// Matches a specific type of element.
    ///
    /// If there is a dictionary, only elements with the fields from the
    /// dictionary match.
    Elem(ElemFunc, Option<Dict>),
    /// Matches the element at the specified location.
    Location(Location),
    /// Matches elements with a specific label.
    Label(Label),
    /// Matches text elements through a regular expression.
    Regex(Regex),
    /// Matches elements with a specific capability.
    Can(TypeId),
    /// Matches if any of the subselectors match.
    Or(EcoVec<Self>),
    /// Matches if all of the subselectors match.
    And(EcoVec<Self>),
    /// Matches all matches of `selector` before `end`.
    Before { selector: Arc<Self>, end: Arc<Self>, inclusive: bool },
    /// Matches all matches of `selector` after `start`.
    After { selector: Arc<Self>, start: Arc<Self>, inclusive: bool },
}

impl Selector {
    /// Define a simple text selector.
    pub fn text(text: &str) -> Self {
        Self::Regex(Regex::new(&regex::escape(text)).unwrap())
    }

    /// Define a simple [`Selector::Can`] selector.
    pub fn can<T: ?Sized + Any>() -> Self {
        Self::Can(TypeId::of::<T>())
    }

    /// Transforms this selector and an iterator of other selectors into a
    /// [`Selector::Or`] selector.
    pub fn and(self, others: impl IntoIterator<Item = Self>) -> Self {
        Self::And(others.into_iter().chain(Some(self)).collect())
    }

    /// Transforms this selector and an iterator of other selectors into a
    /// [`Selector::And`] selector.
    pub fn or(self, others: impl IntoIterator<Item = Self>) -> Self {
        Self::Or(others.into_iter().chain(Some(self)).collect())
    }

    /// Transforms this selector into a [`Selector::Before`] selector.
    pub fn before(self, location: impl Into<Self>, inclusive: bool) -> Self {
        Self::Before {
            selector: Arc::new(self),
            end: Arc::new(location.into()),
            inclusive,
        }
    }

    /// Transforms this selector into a [`Selector::After`] selector.
    pub fn after(self, location: impl Into<Self>, inclusive: bool) -> Self {
        Self::After {
            selector: Arc::new(self),
            start: Arc::new(location.into()),
            inclusive,
        }
    }

    /// Whether the selector matches for the target.
    pub fn matches(&self, target: &Content) -> bool {
        match self {
            Self::Elem(element, dict) => {
                target.func() == *element
                    && dict
                        .iter()
                        .flat_map(|dict| dict.iter())
                        .all(|(name, value)| target.field_ref(name) == Some(value))
            }
            Self::Label(label) => target.label() == Some(label),
            Self::Regex(regex) => {
                target.func() == item!(text_func)
                    && item!(text_str)(target).map_or(false, |text| regex.is_match(&text))
            }
            Self::Can(cap) => target.can_type_id(*cap),
            Self::Or(selectors) => selectors.iter().any(move |sel| sel.matches(target)),
            Self::And(selectors) => selectors.iter().all(move |sel| sel.matches(target)),
            Self::Location(location) => target.location() == Some(*location),
            // Not supported here.
            Self::Before { .. } | Self::After { .. } => false,
        }
    }
}

impl From<Location> for Selector {
    fn from(value: Location) -> Self {
        Self::Location(value)
    }
}

impl Debug for Selector {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Elem(elem, dict) => {
                f.write_str(elem.name())?;
                if let Some(dict) = dict {
                    f.write_str(".where")?;
                    dict.fmt(f)?;
                }
                Ok(())
            }
            Self::Label(label) => label.fmt(f),
            Self::Regex(regex) => regex.fmt(f),
            Self::Can(cap) => cap.fmt(f),
            Self::Or(selectors) | Self::And(selectors) => {
                f.write_str(if matches!(self, Self::Or(_)) { "or" } else { "and" })?;
                let pieces: Vec<_> =
                    selectors.iter().map(|sel| eco_format!("{sel:?}")).collect();
                f.write_str(&pretty_array_like(&pieces, false))
            }
            Self::Location(loc) => loc.fmt(f),
            Self::Before { selector, end: split, inclusive }
            | Self::After { selector, start: split, inclusive } => {
                selector.fmt(f)?;

                if matches!(self, Self::Before { .. }) {
                    f.write_str(".before(")?;
                } else {
                    f.write_str(".after(")?;
                }

                split.fmt(f)?;
                if !*inclusive {
                    f.write_str(", inclusive: false")?;
                }
                f.write_char(')')
            }
        }
    }
}

cast! {
    type Selector: "selector",
    func: Func => func
        .element()
        .ok_or("only element functions can be used as selectors")?
        .select(),
    label: Label => Self::Label(label),
    text: EcoString => Self::text(&text),
    regex: Regex => Self::Regex(regex),
    location: Location => Self::Location(location),
}

/// A selector that can be used with `query`.
///
/// Hopefully, this is made obsolete by a more powerful query mechanism in the
/// future.
#[derive(Clone, PartialEq, Hash)]
pub struct LocatableSelector(pub Selector);

impl Reflect for LocatableSelector {
    fn describe() -> CastInfo {
        CastInfo::Union(vec![
            CastInfo::Type("function"),
            CastInfo::Type("label"),
            CastInfo::Type("selector"),
        ])
    }

    fn castable(value: &Value) -> bool {
        matches!(value.type_name(), "function" | "label" | "selector")
    }
}

impl IntoValue for LocatableSelector {
    fn into_value(self) -> Value {
        self.0.into_value()
    }
}

impl FromValue for LocatableSelector {
    fn from_value(value: Value) -> StrResult<Self> {
        fn validate(selector: &Selector) -> StrResult<()> {
            match selector {
                Selector::Elem(elem, _) => {
                    if !elem.can::<dyn Locatable>() {
                        Err(eco_format!("{} is not locatable", elem.name()))?
                    }
                }
                Selector::Location(_) => {}
                Selector::Label(_) => {}
                Selector::Regex(_) => Err("text is not locatable")?,
                Selector::Can(_) => Err("capability is not locatable")?,
                Selector::Or(list) | Selector::And(list) => {
                    for selector in list {
                        validate(selector)?;
                    }
                }
                Selector::Before { selector, end: split, .. }
                | Selector::After { selector, start: split, .. } => {
                    for selector in [selector, split] {
                        validate(selector)?;
                    }
                }
            }
            Ok(())
        }

        if !Self::castable(&value) {
            return Err(Self::error(&value));
        }

        let selector = Selector::from_value(value)?;
        validate(&selector)?;
        Ok(Self(selector))
    }
}

/// A selector that can be used with show rules.
///
/// Hopefully, this is made obsolete by a more powerful showing mechanism in the
/// future.
#[derive(Clone, PartialEq, Hash)]
pub struct ShowableSelector(pub Selector);

impl Reflect for ShowableSelector {
    fn describe() -> CastInfo {
        CastInfo::Union(vec![
            CastInfo::Type("function"),
            CastInfo::Type("label"),
            CastInfo::Type("string"),
            CastInfo::Type("regex"),
            CastInfo::Type("symbol"),
            CastInfo::Type("selector"),
        ])
    }

    fn castable(value: &Value) -> bool {
        matches!(
            value.type_name(),
            "symbol" | "string" | "label" | "function" | "regex" | "selector"
        )
    }
}

impl IntoValue for ShowableSelector {
    fn into_value(self) -> Value {
        self.0.into_value()
    }
}

impl FromValue for ShowableSelector {
    fn from_value(value: Value) -> StrResult<Self> {
        fn validate(selector: &Selector) -> StrResult<()> {
            match selector {
                Selector::Elem(_, _) => {}
                Selector::Label(_) => {}
                Selector::Regex(_) => {}
                Selector::Or(_)
                | Selector::And(_)
                | Selector::Location(_)
                | Selector::Can(_)
                | Selector::Before { .. }
                | Selector::After { .. } => {
                    Err("this selector cannot be used with show")?
                }
            }
            Ok(())
        }

        if !Self::castable(&value) {
            return Err(Self::error(&value));
        }

        let selector = Selector::from_value(value)?;
        validate(&selector)?;
        Ok(Self(selector))
    }
}