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