tilo_actions/
selectors.rs1use crate::remote::CatalogueRow;
11use tilo_selector::Subject;
12
13pub const KIND: &str = "kind";
16
17pub const LABEL: &str = "label";
20
21pub const BADGE: &str = "badge";
23
24pub 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#[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(¬es)));
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(¬es)));
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}