Skip to main content

tilo_actions/resource/
reading_row.rs

1//! One list item of a reading, as a person finds it in search.
2
3use std::collections::BTreeMap;
4
5/// One item a reading lists among its values — a container, a drive and its partitions, a
6/// printer and its jobs, a process, a journal record — as a person finds it in search: what it
7/// is called, what says what it is, and how choosing it opens it.
8///
9/// A reading publishes these for every list its resource keeps (`Service::search_rows`); the
10/// system's search lists each as a row of its own, so a device reading's items are found the
11/// way an app's are, without the app that shows them having to run.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13pub struct ReadingRow {
14    /// The item's stable id in its reading: a container's name, a drive's name in `/dev`, a
15    /// journal record's moment.
16    pub id: String,
17    /// What the row says.
18    pub title: String,
19    /// The line under it (a publisher, a state, a unit).
20    pub subtitle: Option<String>,
21    /// The group it is listed under ("Containers", "Printers").
22    pub group: Option<String>,
23    /// A named icon (a lucide kebab name).
24    pub icon: Option<String>,
25    /// Where it is (a container's image, a drive's mount point): further lines, not matched.
26    pub facts: Vec<String>,
27    /// More text it is found by: a publisher, a command, a record's unit.
28    pub fields: Vec<String>,
29    /// The action of its resource that opens it on the item, when the reading offers one: a
30    /// program's `launch`, a partition's `mount`.
31    pub action: Option<String>,
32    /// The values that action takes to name the item, by parameter name.
33    pub arguments: BTreeMap<String, serde_json::Value>,
34    /// The app whose window shows the item, opened when the reading offers no action on it.
35    pub app: Option<String>,
36}
37
38impl ReadingRow {
39    /// The item `id`, called `title`.
40    #[must_use]
41    pub fn new(id: impl Into<String>, title: impl Into<String>) -> Self {
42        Self {
43            id: id.into(),
44            title: title.into(),
45            ..Self::default()
46        }
47    }
48
49    /// The line under it.
50    #[must_use]
51    pub fn subtitled(mut self, subtitle: impl Into<String>) -> Self {
52        let subtitle = subtitle.into();
53        self.subtitle = (!subtitle.trim().is_empty()).then(|| subtitle.trim().to_owned());
54        self
55    }
56
57    /// The group it is listed under.
58    #[must_use]
59    pub fn grouped(mut self, group: impl Into<String>) -> Self {
60        self.group = Some(group.into());
61        self
62    }
63
64    /// A named icon.
65    #[must_use]
66    pub fn icon(mut self, icon: impl Into<String>) -> Self {
67        self.icon = Some(icon.into());
68        self
69    }
70
71    /// Where it is.
72    #[must_use]
73    pub fn fact(mut self, fact: impl Into<String>) -> Self {
74        let fact = fact.into();
75        if !fact.trim().is_empty() {
76            self.facts.push(fact);
77        }
78        self
79    }
80
81    /// More text it is found by.
82    #[must_use]
83    pub fn field(mut self, field: impl Into<String>) -> Self {
84        let field = field.into();
85        if !field.trim().is_empty() {
86            self.fields.push(field);
87        }
88        self
89    }
90
91    /// The action of its resource that opens it, given the item by `arguments`.
92    #[must_use]
93    pub fn opened_by<K, V>(
94        mut self,
95        action: impl Into<String>,
96        arguments: impl IntoIterator<Item = (K, V)>,
97    ) -> Self
98    where
99        K: Into<String>,
100        V: Into<serde_json::Value>,
101    {
102        self.action = Some(action.into());
103        self.arguments.extend(
104            arguments
105                .into_iter()
106                .map(|(name, value)| (name.into(), value.into())),
107        );
108        self
109    }
110
111    /// The app whose window shows it.
112    #[must_use]
113    pub fn shown_by(mut self, app: impl Into<String>) -> Self {
114        self.app = Some(app.into());
115        self
116    }
117}