tilo_resource/row.rs
1//! A live row: one entity a surface publishes beneath its resource that lives
2//! outside the resource's document — a process, a device, a container, a
3//! journal entry, a fetched catalogue row, a running job — for the palette,
4//! agents and scripts.
5//!
6//! A row is not document state: it has no history, no undo and nothing to
7//! sync, so it can never carry a document mutation. A change to the document
8//! is the resource's `Action`, bound to a row through the framework's
9//! `Publishes::row_edits` or the resource's `children`.
10
11use std::collections::BTreeMap;
12
13use tilo_actions::{
14 ActionSpec, Exports, ResourcePresentation, ResourceSemanticIdentity, ResourceSpec,
15};
16
17use crate::Effect;
18
19/// The arguments a row sets for the effects it offers, by effect id.
20///
21/// A row of a declared type offers the effects its type declares on rows
22/// (`#[effect(on = Row)]`), and knows from its own state what those effects
23/// act on: a saved network knows the profile it keeps and whether forging it
24/// once more means going through its proxy. Each effect the row names is
25/// offered on it with exactly the arguments it gives bound; an effect it
26/// does not name is not offered (`RowBindings::new()` offers none, and
27/// [`LiveRow::typed`] offers every declared one with just the id bound).
28#[derive(Clone, Default)]
29pub struct RowBindings {
30 arguments: BTreeMap<&'static str, Vec<serde_json::Map<String, serde_json::Value>>>,
31 /// The parameters of each effect a caller may give another value than the
32 /// row's own: offered as asked, with the row's value as their default.
33 defaults: BTreeMap<&'static str, Vec<&'static str>>,
34 choices: BTreeMap<(&'static str, &'static str), Vec<String>>,
35 specs: Vec<ActionSpec>,
36}
37
38impl std::fmt::Debug for RowBindings {
39 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 formatter
41 .debug_struct("RowBindings")
42 .field("arguments", &self.arguments)
43 .field("defaults", &self.defaults)
44 .field("choices", &self.choices)
45 .field(
46 "specs",
47 &self
48 .specs
49 .iter()
50 .map(|spec| spec.id.as_str())
51 .collect::<Vec<_>>(),
52 )
53 .finish()
54 }
55}
56
57impl RowBindings {
58 #[must_use]
59 pub fn new() -> Self {
60 Self::default()
61 }
62
63 /// Offer the effect `effect` on the row, with `arguments` bound: the
64 /// effect's declaration's parameters, each by the name it declares.
65 /// Names the effect does not take are dropped.
66 #[must_use]
67 pub fn bind<R, A, I, K>(self, effect: crate::ActionRef<R, A>, arguments: I) -> Self
68 where
69 I: IntoIterator<Item = (K, serde_json::Value)>,
70 K: Into<String>,
71 {
72 self.bind_id(effect.id, arguments)
73 }
74
75 /// [`Self::bind_id`] of an effect known only when it invokes, by the id it
76 /// answers to: the arguments its declaration takes, named as it names
77 /// them. Names the effect does not take are dropped.
78 #[must_use]
79 pub fn bind_id<I, K>(self, effect: &'static str, arguments: I) -> Self
80 where
81 I: IntoIterator<Item = (K, serde_json::Value)>,
82 K: Into<String>,
83 {
84 let mut arguments = arguments
85 .into_iter()
86 .map(|(name, value)| (name.into(), value));
87 self.offer_arguments(effect, &mut arguments)
88 }
89
90 /// [`Self::bind_id`] once the arguments are names and values. Not generic
91 /// over the effect or the iterator a caller passes, so an app builds it
92 /// once instead of once per effect and argument type it binds.
93 #[inline(never)]
94 fn offer_arguments(
95 mut self,
96 effect: &'static str,
97 arguments: &mut dyn Iterator<Item = (String, serde_json::Value)>,
98 ) -> Self {
99 self.arguments
100 .entry(effect)
101 .or_default()
102 .push(arguments.collect());
103 self
104 }
105
106 /// Offer `effect` on the row with `arguments` as the values it would act
107 /// with, still asking the parameters `asking` names: what the row's own
108 /// state knows the effect would act with unless its caller gives another
109 /// (the model agents use now, the protocol this provider speaks). The
110 /// arguments `asking` does not name are bound and not asked.
111 #[must_use]
112 pub fn bind_asking<R, A, I, K>(
113 self,
114 effect: crate::ActionRef<R, A>,
115 arguments: I,
116 asking: &[&'static str],
117 ) -> Self
118 where
119 I: IntoIterator<Item = (K, serde_json::Value)>,
120 K: Into<String>,
121 {
122 self.bind_asking_id(effect.id, arguments, asking)
123 }
124
125 /// [`Self::bind_asking`] of an effect known only when it invokes, by its
126 /// id.
127 #[must_use]
128 pub fn bind_asking_id<I, K>(
129 self,
130 effect: &'static str,
131 arguments: I,
132 asking: &[&'static str],
133 ) -> Self
134 where
135 I: IntoIterator<Item = (K, serde_json::Value)>,
136 K: Into<String>,
137 {
138 self.bind_id(effect, arguments).ask_defaults(effect, asking)
139 }
140
141 /// Offer `effect` with the parameters `asking` as their bound value's default,
142 /// still asked. Not generic, so every effect that asks one shares it.
143 #[inline(never)]
144 fn ask_defaults(mut self, effect: &'static str, asking: &[&'static str]) -> Self {
145 let asked = self.defaults.entry(effect).or_default();
146 for name in asking {
147 if !asked.contains(name) {
148 asked.push(name);
149 }
150 }
151 self
152 }
153
154 /// Offer `effect` on the row with nothing bound: every argument of it is
155 /// asked from whoever invokes it.
156 #[must_use]
157 pub fn offer<R, A>(self, effect: crate::ActionRef<R, A>) -> Self {
158 self.offer_id(effect.id)
159 }
160
161 /// [`Self::offer`] of an effect known only when it invokes, by its id.
162 #[must_use]
163 pub fn offer_id(mut self, effect: &'static str) -> Self {
164 self.arguments
165 .entry(effect)
166 .or_default()
167 .push(serde_json::Map::default());
168 self
169 }
170
171 /// Offer beside what the row already offers, `other`'s: two effects a
172 /// row's own state knows separately are one row's bindings.
173 #[must_use]
174 pub fn extended(mut self, other: &Self) -> Self {
175 for (effect, arguments) in &other.arguments {
176 self.arguments
177 .entry(effect)
178 .or_default()
179 .extend(arguments.iter().cloned());
180 }
181 for (effect, asking) in &other.defaults {
182 self = self.ask_defaults(effect, asking);
183 }
184 self.choices.extend(other.choices.clone());
185 self.specs.extend(other.specs.iter().cloned());
186 self
187 }
188
189 /// Offer the parameter `param` of `effect`, which the row offers, with
190 /// the values the row's state knows it takes (a display's modes, the
191 /// scales it supports): what a control or the palette lists.
192 #[must_use]
193 pub fn choices<R, A>(
194 self,
195 effect: crate::ActionRef<R, A>,
196 param: &'static str,
197 choices: impl IntoIterator<Item = String>,
198 ) -> Self {
199 self.chosen_by(effect.id, param, choices.into_iter().collect())
200 }
201
202 /// [`Self::choices`] once the values are read. Not generic, so every
203 /// effect a row offers values for shares it.
204 #[inline(never)]
205 fn chosen_by(
206 mut self,
207 effect: &'static str,
208 param: &'static str,
209 choices: Vec<String>,
210 ) -> Self {
211 self.choices.insert((effect, param), choices);
212 self
213 }
214
215 /// Offer `spec` on the row as it is: an action another resource declares,
216 /// which the row was given rather than declaring it itself (a settings
217 /// section's edit, the open of an installed app), or one the row's window
218 /// shapes from its own state for a single entity it names (a form the page
219 /// asks, the departure it holds).
220 #[must_use]
221 pub fn spec(mut self, spec: ActionSpec) -> Self {
222 self.specs.push(spec);
223 self
224 }
225
226 /// The arguments the row gives the effect `effect`, once for each time it
227 /// offers it: an effect offered twice with different arguments (each
228 /// reviewed sign-in method) is two actions on the row.
229 #[must_use]
230 pub fn arguments(&self, effect: &str) -> Option<&[serde_json::Map<String, serde_json::Value>]> {
231 self.arguments.get(effect).map(Vec::as_slice)
232 }
233
234 /// The values the row says `param` of `effect` takes, when it says.
235 #[must_use]
236 pub(crate) fn chosen(&self, effect: &str, param: &str) -> Option<&[String]> {
237 self.choices
238 .iter()
239 .find(|((id, name), _)| *id == effect && *name == param)
240 .map(|(_, values)| values.as_slice())
241 }
242
243 /// The parameters `effect` is offered with the row's value as their
244 /// default, still asked ([`Self::bind_asking`]).
245 #[must_use]
246 pub(crate) fn asks(&self, effect: &str) -> &[&'static str] {
247 self.defaults.get(effect).map_or(&[], Vec::as_slice)
248 }
249
250 /// The actions of another resource the row offers
251 /// ([`Self::spec`]), as published.
252 #[must_use]
253 pub(crate) fn declared(&self) -> &[ActionSpec] {
254 &self.specs
255 }
256
257 /// Whether the row offers no effect of its type's.
258 #[must_use]
259 pub fn is_empty(&self) -> bool {
260 self.arguments.is_empty() && self.specs.is_empty()
261 }
262}
263
264/// The resource `id` of the declared type `R`, published where no document
265/// holds it: a session service's reading (Wi-Fi's networks, the power
266/// policy), a question a service asks, a report a crash left. Its words are
267/// the type's, so search and the palette name it by what it is; the service
268/// publishes the type's vocabulary on it itself
269/// ([`offer`](crate::effect::offer)).
270#[must_use]
271pub fn typed_resource<R: crate::words::RowType>(
272 id: impl Into<String>,
273 label: impl Into<String>,
274) -> ResourceSpec {
275 ResourceSpec::new(id, R::KIND, label).with_type(R::words())
276}
277
278/// One live external entity a surface publishes beneath its resource, with
279/// the view and effect operations it offers (inspect a process, stop a
280/// container).
281///
282/// Rows are merged by id into the resource's published children: a row with
283/// the id of a document child adds its operations to that child instead of
284/// publishing a second resource under the same id.
285///
286/// A row is shared, not copied: a surface that keeps the rows it made while
287/// what they show holds hands the same rows to every publication, and the
288/// publication reuses what it built from each one instead of building it
289/// again.
290///
291/// [`LiveRow::typed`] is the one way to publish a row: it is an item of a
292/// declared [`RowType`](crate::words::RowType), saying what it is to search
293/// and offering the type's effects, so a press on it is one of the type's
294/// actions or `open`.
295#[derive(Clone)]
296pub struct LiveRow {
297 resource: std::rc::Rc<ResourceSpec>,
298}
299
300impl LiveRow {
301 /// A row with a stable `id` (unique within the resource, and naming the
302 /// same entity for as long as it lives: a process id with its start
303 /// time, a container id), a `kind` tag and a human `label`.
304 ///
305 /// The untyped builder is framework-internal: it names no declared type,
306 /// so search cannot say what the row is and a press on it offers only the
307 /// operations the caller adds. Production code publishes
308 /// [`Self::typed`]; the repository's live-row check refuses this one in
309 /// an app or a session surface.
310 #[doc(hidden)]
311 pub fn new(id: impl Into<String>, kind: impl Into<String>, label: impl Into<String>) -> Self {
312 Self {
313 resource: std::rc::Rc::new(ResourceSpec::new(id, kind, label)),
314 }
315 }
316
317 /// A row of the declared type `R` (a resource type of `R`'s kind), whose
318 /// noun its type publishes, of the app the type is declared in: its
319 /// `id`, its `label`, the [`ResourceType`](tilo_actions::ResourceType)
320 /// search matches "videos of …" against, and the effects the type offers
321 /// on it ([`RowType::effects`](crate::words::RowType::effects), each
322 /// bound to `id`). A row an app declares as an item of a `#[resource]`
323 /// type publishes (`Video`, `Channel`) is one of these, so what it is is
324 /// the type's and the search's, nothing the app writes.
325 pub fn typed<R: crate::words::RowType>(
326 id: impl Into<String>,
327 label: impl Into<String>,
328 ) -> Self {
329 typed_row(&RowTypeTable::of::<R>(), id.into(), label.into(), None)
330 }
331
332 /// A row of the declared type `R`, offering the effects `bindings`
333 /// names among the type's ([`RowType::effects_with`](crate::words::RowType::effects_with)),
334 /// each bound to what the row's own state knows: unlike [`Self::typed`],
335 /// which offers every effect the type declares on its rows with only the
336 /// id bound, this one leaves out the effects that do not apply (a network
337 /// with no proxy) and sets the arguments the row holds (the profile, the
338 /// direction of a toggle).
339 pub fn typed_with<R: crate::words::RowType>(
340 id: impl Into<String>,
341 label: impl Into<String>,
342 bindings: &RowBindings,
343 ) -> Self {
344 typed_row(
345 &RowTypeTable::of::<R>(),
346 id.into(),
347 label.into(),
348 Some(bindings),
349 )
350 }
351
352 /// Attach the stable source-domain identity used to find this row across
353 /// live dispatch aliases. It is descriptive metadata only and grants no
354 /// permission or authority. Invalid identities are omitted at this
355 /// publication boundary.
356 #[must_use]
357 pub fn semantic_identity(mut self, identity: ResourceSemanticIdentity) -> Self {
358 std::rc::Rc::make_mut(&mut self.resource).semantic_identity =
359 identity.is_valid().then_some(identity);
360 self
361 }
362
363 /// `resource`, an entity a declaration shaped — a document's child, a
364 /// search hit's place, a tree a declared type's verbs built — as a row:
365 /// its presentation, views, operations and children, its document
366 /// actions dropped at any depth (the resource's own publication carries
367 /// them). The entity says what it is (its type's words), so search names
368 /// it by that and its type's effects are the ones it offers.
369 ///
370 /// # Panics
371 /// When `resource` names no declared type: an entity no declaration
372 /// shaped has no words to be searched by and no type whose effects it
373 /// offers, so it is not a row. Publish it as one with
374 /// [`Self::typed`](Self::typed) instead.
375 #[must_use]
376 pub fn declared(resource: ResourceSpec) -> Self {
377 assert!(
378 resource.resource_type.is_some(),
379 "a row is an entity of a declared type, and `{}` names none",
380 resource.id.as_str()
381 );
382 Self {
383 resource: std::rc::Rc::new(crate::share::resolved_outside_document(resource)),
384 }
385 }
386
387 /// Offer a view or effect operation on the row.
388 ///
389 /// Offer every effect of the vocabulary `E` the resource itself offers
390 /// ([`Effect::specs`](crate::Effect::specs)), as it publishes them: what a
391 /// window's own row offers (navigate, show settings), beside the effects
392 /// its row types declare.
393 #[must_use]
394 pub fn vocabulary<E: crate::Effect>(mut self) -> Self {
395 for spec in E::specs() {
396 self = self.operation(spec);
397 }
398 self
399 }
400
401 /// # Panics
402 /// In a debug build, when `operation` is a document mutation; a release
403 /// build drops it.
404 ///
405 /// Framework-internal, beside [`Self::new`]: a row offers its effects
406 /// through its type's declaration.
407 #[doc(hidden)]
408 #[must_use]
409 pub fn operation(mut self, operation: ActionSpec) -> Self {
410 if crate::share::admits_outside_document(&operation, self.id()) {
411 std::rc::Rc::make_mut(&mut self.resource)
412 .actions
413 .push(operation);
414 }
415 self
416 }
417
418 /// How the entity stands right now (a process's CPU use, what a timer
419 /// has left), shown beside its label. A row merged into a document child
420 /// that has no presentation of its own lends it this one.
421 #[must_use]
422 pub fn presentation(mut self, presentation: ResourcePresentation) -> Self {
423 std::rc::Rc::make_mut(&mut self.resource).presentation = Some(presentation);
424 self
425 }
426
427 /// Offer `effect`, bound to this row's entity (stop *this* process):
428 /// invoked here, it reaches the surface's or view's handler of `E` as
429 /// that value, under the effect's declared contract.
430 ///
431 /// Framework-internal: a row's type declares its effects
432 /// (`#[effect(on = Row)]`) and [`Self::typed`] publishes them.
433 #[doc(hidden)]
434 #[must_use]
435 pub fn effect<E: Effect>(self, effect: &E) -> Self {
436 self.operation(effect.bound())
437 }
438
439 /// Offer the row's effect `effect` as `label` here, where the row can say
440 /// what it is more exactly than its type can (a container's row opens
441 /// *this* container by name). The effect's contract is its declaration's;
442 /// only what the row reads it by is the row's.
443 #[must_use]
444 pub fn labelled<E, A>(
445 mut self,
446 effect: crate::ActionRef<E, A>,
447 label: impl Into<String>,
448 ) -> Self {
449 let label = label.into();
450 let actions = &mut std::rc::Rc::make_mut(&mut self.resource).actions;
451 if let Some(spec) = actions.iter_mut().find(|spec| spec.id == effect.id) {
452 spec.type_mut().label = label;
453 }
454 self
455 }
456
457 /// Offer `effect` with `values` already in its parameters' fields, each
458 /// still asked: what the row's state knows the effect would act with
459 /// unless its caller says otherwise (a calendar's current colour, a
460 /// switch's next state). Published as the parameters' defaults, so a
461 /// surface fills the field and an agent reads it, and the invocation
462 /// binds whatever is finally given. The contract stays the declaration's.
463 #[must_use]
464 pub fn prefilled<R, A, I, K>(mut self, effect: crate::ActionRef<R, A>, values: I) -> Self
465 where
466 I: IntoIterator<Item = (K, serde_json::Value)>,
467 K: Into<String>,
468 {
469 let values: Vec<(String, serde_json::Value)> = values
470 .into_iter()
471 .map(|(name, value)| (name.into(), value))
472 .collect();
473 for spec in std::rc::Rc::make_mut(&mut self.resource)
474 .actions
475 .iter_mut()
476 .filter(|spec| spec.id == effect.id)
477 {
478 for (name, value) in &values {
479 spec.prefilled(name, value.clone());
480 }
481 }
482 self
483 }
484
485 /// Offer `view` of the row's entity: a way to show it (a message in
486 /// its conversation, a range of its text) that a link or an agent
487 /// navigates to, opened by one of the row's own operations.
488 #[must_use]
489 pub fn view(mut self, view: tilo_actions::ResourceViewSpec) -> Self {
490 std::rc::Rc::make_mut(&mut self.resource).views.push(view);
491 self
492 }
493
494 /// Offer the row's operation `effect` as unavailable, with `code` a
495 /// program reads and `reason` a person reads.
496 #[must_use]
497 pub fn unavailable(
498 self,
499 effect: &str,
500 code: impl Into<String>,
501 reason: impl Into<String>,
502 ) -> Self {
503 self.amend(effect, move |spec| spec.unavailable(code, reason))
504 }
505
506 /// Say what the row's operation `effect` does on this row, where only
507 /// the row can say it.
508 #[must_use]
509 pub fn describe(self, effect: &str, description: impl Into<String>) -> Self {
510 self.amend(effect, move |spec| spec.with_description(description))
511 }
512
513 /// Change the operation `effect` this row offers, when it offers it.
514 fn amend(mut self, effect: &str, change: impl FnOnce(ActionSpec) -> ActionSpec) -> Self {
515 let actions = &mut std::rc::Rc::make_mut(&mut self.resource).actions;
516 if let Some(at) = actions.iter().position(|action| action.id == effect) {
517 let spec = actions.remove(at);
518 actions.insert(at, change(spec));
519 }
520 self
521 }
522
523 /// Nest `row` beneath this one (a container's logs).
524 #[must_use]
525 pub fn row(mut self, row: LiveRow) -> Self {
526 std::rc::Rc::make_mut(&mut self.resource)
527 .children
528 .push(std::rc::Rc::unwrap_or_clone(row.resource));
529 self
530 }
531
532 pub fn id(&self) -> &str {
533 self.resource.id.as_str()
534 }
535
536 /// The row `id` names, this one or one nested beneath it, as a row of its own: what a
537 /// control showing that entity binds to.
538 #[must_use]
539 pub fn find(&self, id: &str) -> Option<Self> {
540 fn walk<'a>(resource: &'a ResourceSpec, id: &str) -> Option<&'a ResourceSpec> {
541 if resource.id.0 == id {
542 return Some(resource);
543 }
544 resource.children.iter().find_map(|child| walk(child, id))
545 }
546 if self.resource.id.0 == id {
547 return Some(self.clone());
548 }
549 walk(&self.resource, id).map(|found| Self {
550 resource: std::rc::Rc::new(found.clone()),
551 })
552 }
553
554 /// The operations the row offers, as it publishes them.
555 #[must_use]
556 pub fn operations(&self) -> &[ActionSpec] {
557 &self.resource.actions
558 }
559
560 /// The row as the published tree carries it.
561 pub fn into_resource(self) -> ResourceSpec {
562 let mut resource = std::rc::Rc::unwrap_or_clone(self.resource);
563 crate::share::offer_tree(&mut resource);
564 resource
565 }
566
567 /// Whether `self` and `other` are the same row, handed out again: a
568 /// clone of one row, not two rows that read alike.
569 pub fn same_row(&self, other: &Self) -> bool {
570 std::rc::Rc::ptr_eq(&self.resource, &other.resource)
571 }
572}
573
574/// What a declared row type answers about its rows, as plain data: the
575/// compiled-once [`typed_row`] takes it so the row builders are one function
576/// rather than one per type every app publishes.
577struct RowTypeTable {
578 kind: &'static str,
579 words: fn() -> tilo_actions::ResourceType,
580 effects: fn(&crate::Id) -> Vec<ActionSpec>,
581 effects_with: fn(&crate::Id, &RowBindings) -> Vec<ActionSpec>,
582}
583
584impl RowTypeTable {
585 fn of<R: crate::words::RowType>() -> Self {
586 Self {
587 kind: R::KIND,
588 words: R::words,
589 effects: R::effects,
590 effects_with: R::effects_with,
591 }
592 }
593}
594
595/// The row of the type `kind` is, built by one function for every type: the
596/// type answers what it is and which effects its rows offer, and nothing here
597/// is compiled per type.
598#[inline(never)]
599fn typed_row(
600 kind: &RowTypeTable,
601 id: String,
602 label: String,
603 bindings: Option<&RowBindings>,
604) -> LiveRow {
605 let named = crate::Id::from_raw(id.clone());
606 let operations = match bindings {
607 Some(bindings) => {
608 let mut operations = (kind.effects_with)(&named, bindings);
609 operations.extend(bindings.declared().iter().cloned());
610 operations
611 }
612 None => (kind.effects)(&named),
613 };
614 let resource = ResourceSpec::new(id, kind.kind, label).with_type((kind.words)());
615 let mut row = LiveRow {
616 resource: std::rc::Rc::new(resource),
617 };
618 for operation in operations {
619 row = row.operation(operation);
620 }
621 row
622}
623
624/// A control binds to the row it shows: `ActionBinding::unit_for(&row, effect.id())`.
625impl Exports for LiveRow {
626 fn exported(&self) -> &ResourceSpec {
627 &self.resource
628 }
629}
630
631/// A row of a reading as the resource it is: its address, typed by its row
632/// type `R`. A record of a reading that is a resource of its own (a Wi-Fi
633/// network in the network's reading) holds one under
634/// [`ADDRESS_KEY`](tilo_actions::surface::ADDRESS_KEY), so a caller of any
635/// language calls the row's own actions on the address it carries, the way
636/// every caller does: never a root action taking the row's name.
637///
638/// It is written as the address alone, and published as a resource of `R`'s
639/// kind ([`FieldType::Resource`](tilo_actions::surface::FieldType::Resource)).
640pub struct Handle<R> {
641 address: String,
642 row: std::marker::PhantomData<fn() -> R>,
643}
644
645impl<R: crate::words::RowType> Handle<R> {
646 /// The row at `address`, as the resource publishing it names its child.
647 #[must_use]
648 pub fn new(address: impl Into<String>) -> Self {
649 Self {
650 address: address.into(),
651 row: std::marker::PhantomData,
652 }
653 }
654
655 /// The address the row's actions are called on (`wifi-network:Home`).
656 #[must_use]
657 pub fn address(&self) -> &str {
658 &self.address
659 }
660
661 /// The address of `action`, one of the row type's own, on this row.
662 #[must_use]
663 pub fn action(&self, action: &str) -> tilo_actions::ActionAddress {
664 tilo_actions::ActionAddress::new(self.address.clone(), action)
665 }
666}
667
668impl<R> Clone for Handle<R> {
669 fn clone(&self) -> Self {
670 Self {
671 address: self.address.clone(),
672 row: std::marker::PhantomData,
673 }
674 }
675}
676
677impl<R> Default for Handle<R> {
678 fn default() -> Self {
679 Self {
680 address: String::new(),
681 row: std::marker::PhantomData,
682 }
683 }
684}
685
686impl<R> PartialEq for Handle<R> {
687 fn eq(&self, other: &Self) -> bool {
688 self.address == other.address
689 }
690}
691
692impl<R> Eq for Handle<R> {}
693
694impl<R> std::fmt::Debug for Handle<R> {
695 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
696 f.debug_tuple("Handle").field(&self.address).finish()
697 }
698}
699
700impl<R> serde::Serialize for Handle<R> {
701 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
702 serializer.serialize_str(&self.address)
703 }
704}
705
706impl<'de, R> serde::Deserialize<'de> for Handle<R> {
707 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
708 Ok(Self {
709 address: String::deserialize(deserializer)?,
710 row: std::marker::PhantomData,
711 })
712 }
713}
714
715impl<R: crate::words::RowType> crate::JsonSchema for Handle<R> {
716 fn json_schema() -> serde_json::Value {
717 tilo_actions::params::picked_address_schema(R::KIND)
718 }
719
720 fn field_type(_: &mut crate::TypeSet) -> crate::FieldType {
721 crate::FieldType::Resource(R::KIND.to_owned())
722 }
723}