use crate::remote::CatalogueRow;
use tilo_selector::Subject;
pub const KIND: &str = "kind";
pub const LABEL: &str = "label";
pub const BADGE: &str = "badge";
pub struct RowSubject<'a>(pub &'a CatalogueRow);
impl Subject for RowSubject<'_> {
fn kind(&self) -> &str {
self.0.resource.kind.as_str()
}
fn says(&self, field: &str) -> Vec<&str> {
match field {
LABEL => std::iter::once(self.0.resource.label.as_str()).collect(),
KIND => std::iter::once(self.0.resource.kind.as_str()).collect(),
BADGE => self
.0
.resource
.presentation
.as_ref()
.and_then(|presentation| presentation.badge.as_deref())
.into_iter()
.collect(),
_ => Vec::new(),
}
}
}
#[must_use]
pub fn subject(row: &CatalogueRow) -> RowSubject<'_> {
RowSubject(row)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::remote::{CatalogueResource, CatalogueRow};
use crate::resource::ResourcePresentation;
use tilo_selector::{Predicate, Selector};
fn row(kind: &str, label: &str, badge: Option<&str>) -> CatalogueRow {
CatalogueRow {
offered: None,
provider: "1".into(),
app_id: "so.tilo.files".into(),
parent_resource: String::new(),
resource: CatalogueResource {
id: "entry".into(),
kind: kind.into(),
label: label.into(),
presentation: badge.map(|badge| ResourcePresentation {
detail: None,
badge: Some(badge.into()),
preview: None,
}),
},
action: None,
has_children: false,
visibility: None,
surface: None,
}
}
tilo_test_env::scenario! {
fn a_selector_reads_published_resources_of_its_kind;
phase a_selector_reads_a_published_resource_by_its_kind_and_label;
{
let invoice = row("files.file", "Invoice 04.pdf", Some("PDF"));
let notes = row("files.file", "Notes.txt", Some("Text"));
let selector = Selector::parse(r#"files.file { label in ["Invoice 04.pdf"] }"#).unwrap();
assert!(selector.matches(&subject(&invoice)));
assert!(!selector.matches(&subject(¬es)));
let pdfs = Selector::kinds(["files.file".to_owned()])
.where_(Predicate::new("badge", ["PDF".to_owned()]));
assert!(pdfs.matches(&subject(&invoice)));
assert!(!pdfs.matches(&subject(¬es)));
assert!(
!Selector::kinds(["files.file".to_owned()])
.where_(Predicate::new("author", ["Ada".to_owned()]))
.matches(&subject(&invoice)),
"a field the resource does not publish is never met"
);
}
phase a_selector_of_another_kind_picks_out_nothing_here;
{
let invoices = row("files.file", "Invoice 04.pdf", None);
assert!(!Selector::kinds(["ebook.book".to_owned()]).matches(&subject(&invoices)));
}
}
}