Skip to main content

tilo_actions/
selectors.rs

1//! Reading a published resource the way a skill's selector reads it.
2//!
3//! Every resource a running app publishes says what it is: its kind, its
4//! label, and the words it shows beside it. A skill's
5//! [`Selector`](tilo_selector::Selector) reads exactly that, so which skills
6//! a resource may be asked with is answered from the live catalogue an agent,
7//! a script and a screen reader see — never from anything a window draws and
8//! never from a second index of the apps.
9
10use crate::remote::CatalogueRow;
11use tilo_selector::Subject;
12
13/// The catalogue's field naming the resource kind a row is of, as
14/// `Selector::to_syntax` writes it: `kind in [ebook.book]`.
15pub const KIND: &str = "kind";
16
17/// The catalogue's field naming a resource's label: the words it presents
18/// itself with.
19pub const LABEL: &str = "label";
20
21/// The catalogue's field naming what a resource shows beside its label.
22pub const BADGE: &str = "badge";
23
24/// A catalogue row's resource, as a selector reads it.
25pub struct RowSubject<'a>(pub &'a CatalogueRow);
26
27impl Subject for RowSubject<'_> {
28    fn kind(&self) -> &str {
29        self.0.resource.kind.as_str()
30    }
31
32    fn says(&self, field: &str) -> Vec<&str> {
33        match field {
34            LABEL => std::iter::once(self.0.resource.label.as_str()).collect(),
35            KIND => std::iter::once(self.0.resource.kind.as_str()).collect(),
36            BADGE => self
37                .0
38                .resource
39                .presentation
40                .as_ref()
41                .and_then(|presentation| presentation.badge.as_deref())
42                .into_iter()
43                .collect(),
44            _ => Vec::new(),
45        }
46    }
47}
48
49/// `row`'s resource, read the way a skill's selector reads it.
50#[must_use]
51pub fn subject(row: &CatalogueRow) -> RowSubject<'_> {
52    RowSubject(row)
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use crate::remote::{CatalogueResource, CatalogueRow};
59    use crate::resource::ResourcePresentation;
60    use tilo_selector::{Predicate, Selector};
61
62    fn row(kind: &str, label: &str, badge: Option<&str>) -> CatalogueRow {
63        CatalogueRow {
64            offered: None,
65            provider: "1".into(),
66            app_id: "so.tilo.files".into(),
67            parent_resource: String::new(),
68            resource: CatalogueResource {
69                id: "entry".into(),
70                kind: kind.into(),
71                label: label.into(),
72                presentation: badge.map(|badge| ResourcePresentation {
73                    detail: None,
74                    badge: Some(badge.into()),
75                    preview: None,
76                }),
77            },
78            action: None,
79            has_children: false,
80            visibility: None,
81            surface: None,
82        }
83    }
84
85    tilo_test_env::scenario! {
86        fn a_selector_reads_published_resources_of_its_kind;
87
88        phase a_selector_reads_a_published_resource_by_its_kind_and_label;
89        {
90            let invoice = row("files.file", "Invoice 04.pdf", Some("PDF"));
91            let notes = row("files.file", "Notes.txt", Some("Text"));
92            let selector = Selector::parse(r#"files.file { label in ["Invoice 04.pdf"] }"#).unwrap();
93            assert!(selector.matches(&subject(&invoice)));
94            assert!(!selector.matches(&subject(&notes)));
95
96            let pdfs = Selector::kinds(["files.file".to_owned()])
97                .where_(Predicate::new("badge", ["PDF".to_owned()]));
98            assert!(pdfs.matches(&subject(&invoice)));
99            assert!(!pdfs.matches(&subject(&notes)));
100            assert!(
101                !Selector::kinds(["files.file".to_owned()])
102                    .where_(Predicate::new("author", ["Ada".to_owned()]))
103                    .matches(&subject(&invoice)),
104                "a field the resource does not publish is never met"
105            );
106        }
107
108        phase a_selector_of_another_kind_picks_out_nothing_here;
109        {
110            let invoices = row("files.file", "Invoice 04.pdf", None);
111            assert!(!Selector::kinds(["ebook.book".to_owned()]).matches(&subject(&invoices)));
112        }
113    }
114}