Skip to main content

tilo_actions/resource/
spec.rs

1//! A resource an app publishes: its identity, what it holds and how it nests.
2
3use crate::action::OperationKind;
4use crate::api::ActionAddress;
5
6use super::{
7    ActionSpec, ResourceId, ResourcePresentation, ResourceSemanticIdentity, ResourceType,
8    ResourceViewSpec, listed_for_people,
9};
10
11/// One open/presented resource and the actions it exports. Resources
12/// nest via [`children`](ResourceSpec::children).
13pub struct ResourceSpec {
14    pub id: ResourceId,
15    /// A stable kind tag (e.g. `"tube.video"`, `"ebook.annotation"`),
16    /// shared with the global-search type registry for OS-wide enumeration.
17    pub kind: String,
18    /// Human, localisable label shown in the palette.
19    pub label: String,
20    /// Optional shell-rendered metadata used by system search and resource
21    /// browsers. Invalid or oversized values are omitted at the boundary.
22    pub presentation: Option<ResourcePresentation>,
23    /// Exact original typed identity retained before host namespacing.
24    pub semantic_identity: Option<ResourceSemanticIdentity>,
25    /// The app that publishes it, as the [`ActionKey`](crate::ActionKey)s
26    /// of its actions name it: the one whose resource this is, not the
27    /// provider keeping it in a workspace. `None` for a resource published
28    /// outside an app.
29    pub app_id: Option<String>,
30    pub children: Vec<ResourceSpec>,
31    pub actions: Vec<ActionSpec>,
32    /// Explicit presentations of this resource. Actions remain invocation
33    /// mechanisms; only entries in this collection are resource views.
34    pub views: Vec<ResourceViewSpec>,
35    /// Whether the reading is the person's own rather than everyone's: their clipboard, their
36    /// notifications. Only a program the session runs itself reads it; an installed app reads
37    /// what its own records hold.
38    pub personal: bool,
39    /// The workspace that holds the record, when it is not the one the window publishing it is
40    /// open in: a person's profile record, held in their own workspace. That workspace decides
41    /// the record's changes; `None` is the window's.
42    pub workspace: Option<String>,
43    /// What it is, as people name it, when its type says ([`ResourceType`]).
44    pub resource_type: Option<ResourceType>,
45}
46
47/// Written out rather than derived: a derived `clone` is inlined into every
48/// app that copies a resource, this one is compiled once.
49impl Clone for ResourceSpec {
50    #[inline(never)]
51    fn clone(&self) -> Self {
52        Self {
53            id: self.id.clone(),
54            kind: self.kind.clone(),
55            label: self.label.clone(),
56            presentation: self.presentation.clone(),
57            semantic_identity: self.semantic_identity.clone(),
58            app_id: self.app_id.clone(),
59            children: self.children.clone(),
60            actions: self.actions.clone(),
61            views: self.views.clone(),
62            personal: self.personal,
63            workspace: self.workspace.clone(),
64            resource_type: self.resource_type.clone(),
65        }
66    }
67}
68
69impl ResourceSpec {
70    /// The address of `action` as this resource publishes it: what the API
71    /// takes, what a window's control bound to it says it does, and what the
72    /// survey lists — one address, which neither the window's composition
73    /// moving the control nor the person's language renaming it changes.
74    #[must_use]
75    pub fn address_of(&self, action: &ActionSpec) -> ActionAddress {
76        ActionAddress::new(self.id.as_str(), action.id.as_str())
77    }
78
79    /// A resource with a stable `id`, a `kind` and a human `label`.
80    /// Framework-internal: a declaration builds a resource's tree, never an
81    /// app.
82    #[doc(hidden)]
83    pub fn new(id: impl Into<String>, kind: impl Into<String>, label: impl Into<String>) -> Self {
84        Self {
85            id: ResourceId::new(id),
86            kind: kind.into(),
87            label: label.into(),
88            presentation: None,
89            resource_type: None,
90            semantic_identity: None,
91            app_id: None,
92            children: Vec::new(),
93            actions: Vec::new(),
94            views: Vec::new(),
95            personal: false,
96            workspace: None,
97        }
98    }
99
100    /// The app that publishes it, as an [`ActionKey`](crate::ActionKey) of
101    /// one of its actions names it ([`Self::app_id`]).
102    #[must_use]
103    pub fn app(&self) -> Option<&str> {
104        self.app_id.as_deref()
105    }
106
107    /// The resource's own id, not the transport alias a window published it
108    /// under ([`Self::semantic_identity`]): what its app and the directory
109    /// naming it offer it at, whatever alias the broker delivers it under.
110    #[must_use]
111    pub fn domain_name(&self) -> &str {
112        match &self.semantic_identity {
113            Some(identity) if identity.is_valid() => identity.key.as_str(),
114            _ => self.id.as_str(),
115        }
116    }
117
118    #[must_use]
119    pub fn with_presentation(mut self, presentation: ResourcePresentation) -> Self {
120        self.presentation = presentation.is_valid().then_some(presentation);
121        self
122    }
123
124    /// Saying what it is, as people name it.
125    #[must_use]
126    pub fn with_type(mut self, resource_type: ResourceType) -> Self {
127        self.resource_type = Some(resource_type);
128        self
129    }
130
131    #[must_use]
132    pub fn with_action(mut self, action: ActionSpec) -> Self {
133        self.actions.push(action);
134        self
135    }
136
137    #[must_use]
138    pub fn with_view(mut self, view: ResourceViewSpec) -> Self {
139        self.views.push(view);
140        self
141    }
142
143    /// Publish this resource's reading to the session's own programs alone: what the person's state
144    /// needs, when it is theirs and not an app's.
145    #[must_use]
146    pub fn with_personal_reading(mut self) -> Self {
147        self.personal = true;
148        self
149    }
150
151    /// The record is held in `workspace` rather than in the window's ([`Self::workspace`]).
152    #[must_use]
153    pub fn held_in(mut self, workspace: impl Into<String>) -> Self {
154        self.workspace = Some(workspace.into());
155        self
156    }
157
158    /// Keep the actions `keep` admits, and only the views still opened by
159    /// one of them: a view whose open action is not offered cannot be
160    /// offered either, and the action contract refuses the whole surface
161    /// that carries one.
162    pub fn retain_actions(&mut self, keep: impl FnMut(&ActionSpec) -> bool) {
163        self.actions.retain(keep);
164        let actions = &self.actions;
165        self.views
166            .retain(|view| actions.iter().any(|action| action.id == view.open_action));
167    }
168
169    #[must_use]
170    pub fn with_child(mut self, child: ResourceSpec) -> Self {
171        self.children.push(child);
172        self
173    }
174
175    /// Whether a palette may list it: a titled resource that offers a
176    /// person at least one action of its own. An untitled node, a control
177    /// mirror, and a node left with only agent actions are all addressing
178    /// machinery, and a list of what a person may act on never shows them.
179    #[must_use]
180    pub fn listed_for_people(&self) -> bool {
181        listed_for_people(&self.kind, &self.label)
182            && self
183                .actions
184                .iter()
185                .any(|action| action.contract.offered_to_people())
186    }
187
188    /// How many of its actions a person may take.
189    #[must_use]
190    pub fn person_action_count(&self) -> usize {
191        self.actions
192            .iter()
193            .filter(|action| action.contract.offered_to_people())
194            .count()
195    }
196
197    /// The action that presents it, when a person may take it: what a
198    /// palette lists the resource for.
199    #[must_use]
200    pub fn open_action(&self) -> Option<&ActionSpec> {
201        self.actions
202            .iter()
203            .filter(|action| action.contract.offered_to_people())
204            .find(|action| action.kind == OperationKind::View)
205            .or_else(|| {
206                self.actions
207                    .iter()
208                    .find(|action| action.contract.offered_to_people())
209            })
210    }
211
212    #[must_use]
213    pub fn with_children(mut self, children: impl IntoIterator<Item = ResourceSpec>) -> Self {
214        self.children.extend(children);
215        self
216    }
217
218    /// The address of the action `action` this resource publishes, or `None`
219    /// when it publishes none by that id. A caller holding a published
220    /// resource (a survey's, a search hit's, a bound tree's) addresses its
221    /// actions from it rather than writing an address of its own, so an
222    /// action the resource stopped publishing fails here, at the caller.
223    #[must_use]
224    pub fn action_address(&self, action: &str) -> Option<ActionAddress> {
225        self.actions
226            .iter()
227            .any(|spec| spec.id == action)
228            .then(|| ActionAddress::new(self.id.as_str(), action))
229    }
230}