Skip to main content

bus_contract/
types.rs

1//! The contract's types, shared by every implementation.
2
3use std::fmt;
4use std::time::Duration;
5
6/// The deadline of an in-process listener when neither the listener nor
7/// its point sets one.
8pub const DEFAULT_DEADLINE: Duration = Duration::from_secs(1);
9
10/// The deadline meant for listeners that answer from another process (a
11/// plugin over RPC): give it with [`ListenOpts::deadline`] or
12/// [`PointSpec::deadline`].
13pub const PLUGIN_DEADLINE: Duration = Duration::from_secs(5);
14
15/// The `after` target that places a listener after every other listener
16/// of its point ([`ListenOpts::last`]): one per point, a second is
17/// refused as a [`Refused::Cycle`] (each would run after the other).
18pub const LAST: &str = "*";
19
20/// The name a host gives the fact it publishes for each [`Failure`] its
21/// sinks receive ([`Bus::add_failure_sink`](crate::Bus::add_failure_sink)).
22pub const FAILED_FACT: &str = "hook.failed";
23
24/// A hook point: one type per point, so its input and value are checked
25/// at compile time. Declared by the crate that owns the result.
26///
27/// Every listener of a point has the same shape: it reads the emit's
28/// [`Input`](Point::Input) and edits a [`Value`](Point::Value). An
29/// [`alter`](crate::Bus::alter_any) emit passes one value through the listeners
30/// in order; a [`collect`](crate::Bus::collect_any) emit gives each listener a
31/// fresh value and gathers them in a list.
32pub trait Point: 'static {
33    /// The point's id, `<crate>.<subject>.<moment>`, checked when made:
34    /// `const ID: PointId = PointId::new("health.report");` does not
35    /// compile with a bad id ([`PointId`](crate::PointId)).
36    const ID: crate::PointId;
37    /// What every listener reads: shared, never changed by a listener.
38    type Input: Send + Sync + 'static;
39    /// What a listener edits: the value altered in turn, or one
40    /// listener's contribution to a collect.
41    type Value: Send + 'static;
42    /// What the point is for ([`PointKind`]); [`PointKind::Alter`] unless
43    /// the point says otherwise.
44    const KIND: PointKind = PointKind::Alter;
45}
46
47/// What a point is for, declared for introspection and review. A request
48/// is an alter emit over the request's input; only a request can be
49/// refused by a listener ([`HookError::Refuse`]) or declared fail-closed
50/// ([`PointSpec::fail_closed`]).
51#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
52#[non_exhaustive]
53pub enum PointKind {
54    /// A request (`<noun>.<verb>`): its listeners do the work in their
55    /// declared order; the value is what each did.
56    Request,
57    /// An event's listeners: each event delivered once, in journal order.
58    Event,
59    /// One value passed through the listeners in turn.
60    #[default]
61    Alter,
62    /// Each listener adds its own part.
63    Collect,
64}
65
66impl PointKind {
67    /// The kind as introspection writes it (`request`).
68    pub fn as_str(self) -> &'static str {
69        match self {
70            PointKind::Request => "request",
71            PointKind::Event => "event",
72            PointKind::Alter => "alter",
73            PointKind::Collect => "collect",
74        }
75    }
76}
77
78/// A point's declaration: its description and limits, for introspection.
79///
80/// New fields may be added in a minor release: build one with
81/// [`PointSpec::new`] and the builder methods.
82#[derive(Clone, Debug, Default, PartialEq, Eq)]
83#[non_exhaustive]
84pub struct PointSpec {
85    /// What the point builds, for people and agents reading
86    /// [`Bus::points`](crate::Bus::points).
87    pub description: String,
88    /// The deadline of the point's listeners that set none themselves;
89    /// `None` takes the kernel's default.
90    pub deadline: Option<Duration>,
91    /// On a [`PointKind::Request`] point, a listener that fails (an
92    /// error, a panic, a missed deadline) refuses the request instead of
93    /// having its contribution dropped: the emit stops there
94    /// ([`Refusal::failed`]). Ignored on the other kinds.
95    pub fail_closed: bool,
96    /// The time one pass of the point's listeners should take on a
97    /// thread (the timings contract): a pass over it is reported
98    /// (the timings contract's `OverBudget`), never failed. `None`: no budget.
99    pub budget: Option<Duration>,
100    /// What the declaration says for capability contracts
101    /// ([`Facet`](crate::Facet)): a journaled mark, a codec. Each facet's
102    /// contract is needed ([`declare_all`](crate::declare_all)), and the
103    /// implementation of that contract reads it; the core does not.
104    pub facets: Vec<crate::Facet>,
105    /// The point's version, from 1: raised when its payload changes in a
106    /// way an older reader cannot use (a field it may ignore is not such
107    /// a change). Two processes cross a point only at the same version
108    /// (the crossing contract).
109    pub version: u32,
110}
111
112impl PointSpec {
113    /// A point described by `description`, with the kernel's deadline.
114    pub fn new(description: impl Into<String>) -> PointSpec {
115        PointSpec {
116            description: description.into(),
117            deadline: None,
118            fail_closed: false,
119            budget: None,
120            facets: Vec::new(),
121            version: 1,
122        }
123    }
124
125    /// The deadline of the point's listeners that set none themselves.
126    pub fn deadline(mut self, deadline: Duration) -> PointSpec {
127        self.deadline = Some(deadline);
128        self
129    }
130
131    /// A listener failure refuses the request ([`PointSpec::fail_closed`]).
132    pub fn fail_closed(mut self) -> PointSpec {
133        self.fail_closed = true;
134        self
135    }
136
137    /// The time one pass of the point's listeners should take
138    /// ([`PointSpec::budget`]).
139    pub fn budget(mut self, budget: Duration) -> PointSpec {
140        self.budget = Some(budget);
141        self
142    }
143
144    /// Adds a facet of a capability contract ([`PointSpec::facets`]).
145    pub fn facet(mut self, facet: crate::Facet) -> PointSpec {
146        self.facets.push(facet);
147        self
148    }
149
150    /// Whether the declaration has a facet of `contract`.
151    pub fn has(&self, contract: &crate::Contract) -> bool {
152        self.facets.iter().any(|f| f.contract == contract)
153    }
154
155    /// The point's version ([`PointSpec::version`]).
156    pub fn version(mut self, version: u32) -> PointSpec {
157        self.version = version;
158        self
159    }
160}
161
162/// How a listener is placed and limited.
163#[derive(Clone, Debug, Default, PartialEq, Eq)]
164pub struct ListenOpts {
165    /// Listeners this one runs after.
166    pub after: Vec<String>,
167    /// Listeners this one runs before.
168    pub before: Vec<String>,
169    /// Among listeners `after`/`before` leave free, higher runs first;
170    /// ties run in registration order.
171    pub priority: i32,
172    /// Overrides the point's deadline.
173    pub deadline: Option<Duration>,
174    /// Who registered it (a module, a plugin), for [`Bus::forget`](crate::Bus::forget) and
175    /// introspection.
176    pub owner: Option<String>,
177    /// Runs on a thread of its own, so the emit stops waiting at the
178    /// deadline. An inline listener (the default) runs on the emitting
179    /// thread: it cannot be stopped, and only counts as late once it
180    /// returns. A local listener runs inline on its own thread: it is
181    /// refused with this set ([`Refused::Threaded`]).
182    pub threaded: bool,
183}
184
185impl ListenOpts {
186    /// No constraint, priority 0, the point's deadline, inline.
187    pub fn new() -> ListenOpts {
188        ListenOpts::default()
189    }
190
191    /// Runs after the listener `id` (a missing one is reported and
192    /// ignored).
193    pub fn after(mut self, id: impl Into<String>) -> ListenOpts {
194        self.after.push(id.into());
195        self
196    }
197
198    /// Runs before the listener `id` (a missing one is reported and
199    /// ignored).
200    pub fn before(mut self, id: impl Into<String>) -> ListenOpts {
201        self.before.push(id.into());
202        self
203    }
204
205    /// Runs after every other listener of its point, whatever they
206    /// declare: an invariants check, a listener that reads what all the
207    /// others left. One per point: a second one closes a cycle and is
208    /// refused ([`Refused::Cycle`]). Kept in [`ListenOpts::after`] as
209    /// [`LAST`].
210    pub fn last(mut self) -> ListenOpts {
211        if !self.after.iter().any(|a| a == LAST) {
212            self.after.push(LAST.to_owned());
213        }
214        self
215    }
216
217    /// Whether the listener runs after every other one of its point
218    /// ([`ListenOpts::last`]).
219    pub fn is_last(&self) -> bool {
220        self.after.iter().any(|a| a == LAST)
221    }
222
223    /// Higher runs first among listeners left free by `after`/`before`.
224    pub fn priority(mut self, priority: i32) -> ListenOpts {
225        self.priority = priority;
226        self
227    }
228
229    /// Overrides the point's deadline.
230    pub fn deadline(mut self, deadline: Duration) -> ListenOpts {
231        self.deadline = Some(deadline);
232        self
233    }
234
235    /// Who registered the listener.
236    pub fn owner(mut self, owner: impl Into<String>) -> ListenOpts {
237        self.owner = Some(owner.into());
238        self
239    }
240
241    /// Runs on a thread of its own, so a slow listener cannot hold the
242    /// emit past its deadline.
243    pub fn threaded(mut self) -> ListenOpts {
244        self.threaded = true;
245        self
246    }
247}
248
249/// What a listener returns instead of `Ok(())`: it failed, or it refuses
250/// the request.
251#[derive(Clone, Debug, PartialEq, Eq)]
252#[non_exhaustive]
253pub enum HookError {
254    /// The listener failed: its contribution is dropped and the other
255    /// listeners run, unless its point is fail-closed
256    /// ([`PointSpec::fail_closed`]).
257    Failed(String),
258    /// The listener refuses the request, with a reason. On a
259    /// [`PointKind::Request`] point the emit stops: no later listener
260    /// runs, and the result names the refusal ([`Refusal`]). On another
261    /// kind it counts as [`HookError::Failed`].
262    Refuse(String),
263}
264
265impl HookError {
266    /// A failure with `message`.
267    pub fn failed(message: impl Into<String>) -> HookError {
268        HookError::Failed(message.into())
269    }
270
271    /// A refusal of the request, with `reason`.
272    pub fn refuse(reason: impl Into<String>) -> HookError {
273        HookError::Refuse(reason.into())
274    }
275
276    /// The failure's message or the refusal's reason.
277    pub fn message(&self) -> &str {
278        match self {
279            HookError::Failed(m) | HookError::Refuse(m) => m,
280        }
281    }
282
283    /// Whether the listener refuses the request.
284    pub fn is_refusal(&self) -> bool {
285        matches!(self, HookError::Refuse(_))
286    }
287}
288
289impl fmt::Display for HookError {
290    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291        match self {
292            HookError::Failed(m) => f.write_str(m),
293            HookError::Refuse(r) => write!(f, "refused: {r}"),
294        }
295    }
296}
297
298impl std::error::Error for HookError {}
299
300/// A failure with this message.
301impl From<String> for HookError {
302    fn from(message: String) -> HookError {
303        HookError::Failed(message)
304    }
305}
306
307/// A failure with this message.
308impl From<&str> for HookError {
309    fn from(message: &str) -> HookError {
310        HookError::Failed(message.to_owned())
311    }
312}
313
314/// What went wrong with one listener during an emit.
315#[derive(Clone, Debug, PartialEq, Eq)]
316#[non_exhaustive]
317pub enum FailureKind {
318    /// The listener returned an error.
319    Error(String),
320    /// The listener panicked; the panic's message.
321    Panic(String),
322    /// The listener missed its deadline. `took` is its time when it
323    /// returned late (inline); `None` when the emit stopped waiting
324    /// (threaded).
325    Late {
326        /// The deadline it had.
327        deadline: Duration,
328        /// How long it ran, when it returned.
329        took: Option<Duration>,
330    },
331    /// An `after` or `before` constraint names a listener that is not
332    /// registered. The listener still ran and its contribution counts;
333    /// only the constraint was ignored. Not sent to the failure sinks.
334    MissingTarget(String),
335    /// The point id is registered with another [`Point`] type; no
336    /// listener ran. `listener` is `*`.
337    TypeMismatch,
338}
339
340/// One listener's failure: its contribution was dropped (except for a
341/// [`FailureKind::MissingTarget`]); the other listeners ran, unless the
342/// point is fail-closed ([`PointSpec::fail_closed`]).
343#[derive(Clone, Debug, PartialEq, Eq)]
344#[non_exhaustive]
345pub struct Failure {
346    /// The point emitted.
347    pub point: &'static str,
348    /// The listener's id.
349    pub listener: String,
350    /// The listener's owner.
351    pub owner: Option<String>,
352    /// What went wrong.
353    pub kind: FailureKind,
354}
355
356impl Failure {
357    /// The failure of the listener `listener` of `point`.
358    pub fn new(
359        point: &'static str,
360        listener: impl Into<String>,
361        owner: Option<String>,
362        kind: FailureKind,
363    ) -> Failure {
364        Failure {
365            point,
366            listener: listener.into(),
367            owner,
368            kind,
369        }
370    }
371
372    /// Whether the failure dropped a contribution: everything but a
373    /// missing ordering target. Only these reach the failure sinks.
374    pub fn dropped(&self) -> bool {
375        !matches!(self.kind, FailureKind::MissingTarget(_))
376    }
377}
378
379impl fmt::Display for FailureKind {
380    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
381        match self {
382            FailureKind::Error(m) => write!(f, "failed: {m}"),
383            FailureKind::Panic(m) => write!(f, "panicked: {m}"),
384            FailureKind::Late {
385                deadline,
386                took: Some(t),
387            } => write!(f, "took {t:?}, past its deadline of {deadline:?}"),
388            FailureKind::Late { deadline, .. } => {
389                write!(f, "no answer within its deadline of {deadline:?}")
390            }
391            FailureKind::MissingTarget(t) => {
392                write!(f, "orders itself against `{t}`, which is not there")
393            }
394            FailureKind::TypeMismatch => write!(f, "the point is registered with another type"),
395        }
396    }
397}
398
399impl fmt::Display for Failure {
400    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
401        write!(
402            f,
403            "hook `{}`, listener `{}`: {}",
404            self.point, self.listener, self.kind
405        )
406    }
407}
408
409/// Why a declaration or a listener was refused. Nothing was changed.
410#[derive(Clone, Debug, PartialEq, Eq)]
411#[non_exhaustive]
412pub enum Refused {
413    /// The point already has a listener with this id.
414    Duplicate {
415        /// The point.
416        point: &'static str,
417        /// The id taken.
418        listener: String,
419    },
420    /// The listener's `after`/`before` would close a cycle: these
421    /// listeners, each to run before the next, the first again at the
422    /// end.
423    Cycle {
424        /// The point.
425        point: &'static str,
426        /// The listeners in the cycle.
427        path: Vec<String>,
428    },
429    /// The point id is already registered with another [`Point`] type.
430    TypeMismatch {
431        /// The point.
432        point: &'static str,
433    },
434    /// The point was already declared.
435    AlreadyDeclared {
436        /// The point.
437        point: &'static str,
438    },
439    /// A local listener asked for a thread of its own
440    /// ([`ListenOpts::threaded`]); it runs on the thread that registered
441    /// it.
442    Threaded {
443        /// The point.
444        point: &'static str,
445        /// The listener.
446        listener: String,
447    },
448    /// The point needs a capability contract the bus does not offer
449    /// ([`PointDecl::needs`](crate::PointDecl::needs),
450    /// [`PointDecl::facets`](crate::PointDecl::facets)); it was not
451    /// declared.
452    Unsupported {
453        /// The point.
454        point: &'static str,
455        /// The contract missing ([`Contract::name`](crate::Contract::name)).
456        contract: &'static str,
457    },
458    /// A capability contract refused what the point's declaration or a
459    /// call says for it: a journaled mark on a point that is not an
460    /// event, a codec of another point, a publish on a point not
461    /// journaled. Nothing was declared or recorded.
462    Contract {
463        /// The point.
464        point: &'static str,
465        /// The contract that refused it.
466        contract: &'static str,
467        /// Why.
468        reason: String,
469    },
470    /// An id breaks the naming rule ([`check_id`](crate::check_id)): a
471    /// point key's id at declaration, a listener's id when it
472    /// registers. Nothing was changed.
473    BadId {
474        /// The id refused.
475        id: String,
476        /// Why.
477        error: crate::IdError,
478    },
479}
480
481impl fmt::Display for Refused {
482    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
483        match self {
484            Refused::Duplicate { point, listener } => {
485                write!(f, "hook `{point}` already has a listener `{listener}`")
486            }
487            Refused::Cycle { point, path } => {
488                write!(
489                    f,
490                    "hook `{point}`: listeners order each other in a cycle: {}",
491                    path.join(" -> ")
492                )
493            }
494            Refused::TypeMismatch { point } => {
495                write!(f, "hook `{point}` is registered with another type")
496            }
497            Refused::AlreadyDeclared { point } => write!(f, "hook `{point}` is already declared"),
498            Refused::Threaded { point, listener } => write!(
499                f,
500                "hook `{point}`: the local listener `{listener}` runs on its own thread, not a new one"
501            ),
502            Refused::Unsupported { point, contract } => write!(
503                f,
504                "hook `{point}` needs the `{contract}` contract, which this bus does not offer"
505            ),
506            Refused::Contract {
507                point,
508                contract,
509                reason,
510            } => write!(
511                f,
512                "hook `{point}`: refused by the `{contract}` contract: {reason}"
513            ),
514            Refused::BadId { id, error } => write!(f, "id `{id}` refused: {error}"),
515        }
516    }
517}
518
519impl std::error::Error for Refused {}
520
521/// A request stopped by one of its listeners: the listener refused it
522/// ([`HookError::Refuse`]), or failed on a fail-closed point
523/// ([`PointSpec::fail_closed`]). The listeners after it did not run.
524#[derive(Clone, Debug, PartialEq, Eq)]
525#[non_exhaustive]
526pub struct Refusal {
527    /// The point emitted.
528    pub point: &'static str,
529    /// The listener that stopped the request.
530    pub listener: String,
531    /// Its owner.
532    pub owner: Option<String>,
533    /// The reason it gave, or its failure as text.
534    pub reason: String,
535    /// Whether the listener failed (its [`Failure`] is in the result's
536    /// failures) rather than refused.
537    pub failed: bool,
538}
539
540impl Refusal {
541    /// The refusal of `point` by the listener `listener`, for `reason`.
542    pub fn new(
543        point: &'static str,
544        listener: impl Into<String>,
545        owner: Option<String>,
546        reason: impl Into<String>,
547    ) -> Refusal {
548        Refusal {
549            point,
550            listener: listener.into(),
551            owner,
552            reason: reason.into(),
553            failed: false,
554        }
555    }
556
557    /// The refusal a listener's failure makes on a fail-closed point.
558    pub fn from_failure(failure: &Failure) -> Refusal {
559        let reason = match &failure.kind {
560            FailureKind::Error(m) | FailureKind::Panic(m) => m.clone(),
561            kind => kind.to_string(),
562        };
563        Refusal {
564            point: failure.point,
565            listener: failure.listener.clone(),
566            owner: failure.owner.clone(),
567            reason,
568            failed: true,
569        }
570    }
571}
572
573impl fmt::Display for Refusal {
574    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
575        if self.failed {
576            write!(
577                f,
578                "hook `{}`: refused, `{}` failed: {}",
579                self.point, self.listener, self.reason
580            )
581        } else {
582            write!(
583                f,
584                "hook `{}`: refused by `{}`: {}",
585                self.point, self.listener, self.reason
586            )
587        }
588    }
589}
590
591/// The result of an [`alter`](crate::Bus::alter_any) emit.
592#[derive(Debug)]
593#[non_exhaustive]
594pub struct Altered<V> {
595    /// The value after every listener that succeeded; when the request
596    /// was refused, as the listeners before the refusal left it.
597    pub value: V,
598    /// Every failure, in listener order.
599    pub failures: Vec<Failure>,
600    /// The local listeners bound to another thread, in order: queued for
601    /// that thread's [`run_pending`](crate::Bus::run_pending), not run
602    /// by this emit. Empty when the request was refused.
603    pub deferred: Vec<String>,
604    /// The listener that stopped the request, if one did.
605    pub refused: Option<Refusal>,
606}
607
608impl<V> Altered<V> {
609    /// An emit that left `value`, with no failure, nothing deferred and
610    /// no refusal.
611    pub fn new(value: V) -> Altered<V> {
612        Altered {
613            value,
614            failures: Vec::new(),
615            deferred: Vec::new(),
616            refused: None,
617        }
618    }
619}
620
621/// The result of a [`collect`](crate::Bus::collect_any) emit.
622#[derive(Debug)]
623#[non_exhaustive]
624pub struct Collected<V> {
625    /// Each successful listener's value, in listener order, with its id.
626    pub parts: Vec<(String, V)>,
627    /// Every failure, in listener order.
628    pub failures: Vec<Failure>,
629    /// The local listeners bound to another thread, in order: queued for
630    /// that thread's [`run_pending`](crate::Bus::run_pending), not run
631    /// by this emit. Empty when the request was refused.
632    pub deferred: Vec<String>,
633    /// The listener that stopped the request, if one did.
634    pub refused: Option<Refusal>,
635}
636
637impl<V> Collected<V> {
638    /// An emit with no part, no failure, nothing deferred and no
639    /// refusal.
640    pub fn new() -> Collected<V> {
641        Collected {
642            parts: Vec::new(),
643            failures: Vec::new(),
644            deferred: Vec::new(),
645            refused: None,
646        }
647    }
648}
649
650impl<V> Default for Collected<V> {
651    fn default() -> Collected<V> {
652        Collected::new()
653    }
654}
655
656/// What listeners run later did: one emit's local listeners queued for
657/// their thread ([`Bus::run_pending`](crate::Bus::run_pending)), or one
658/// deferred request (the defer contract's drain).
659#[derive(Clone, Debug, PartialEq, Eq)]
660#[non_exhaustive]
661pub struct PendingRun {
662    /// The point emitted.
663    pub point: &'static str,
664    /// The listeners that ran, in order (a listener removed since the
665    /// emit is not run).
666    pub ran: Vec<String>,
667    /// Their failures, in listener order.
668    pub failures: Vec<Failure>,
669    /// The listener that stopped the request, if one did.
670    pub refused: Option<Refusal>,
671    /// For a deferred request, the number the defer contract's ask
672    /// returned when it was asked; `None` for local listeners run by
673    /// [`Bus::run_pending`](crate::Bus::run_pending).
674    pub ask: Option<u64>,
675}
676
677impl PendingRun {
678    /// A run of `point` where nothing ran yet.
679    pub fn new(point: &'static str) -> PendingRun {
680        PendingRun {
681            point,
682            ran: Vec::new(),
683            failures: Vec::new(),
684            refused: None,
685            ask: None,
686        }
687    }
688}
689
690/// A listener as introspection shows it.
691#[derive(Clone, Debug, PartialEq, Eq)]
692#[non_exhaustive]
693pub struct ListenerInfo {
694    /// Its id.
695    pub id: String,
696    /// Its owner.
697    pub owner: Option<String>,
698    /// The listeners it runs after.
699    pub after: Vec<String>,
700    /// The listeners it runs before.
701    pub before: Vec<String>,
702    /// Its priority.
703    pub priority: i32,
704    /// The deadline it gets: its own, else its point's, else the kernel's.
705    pub deadline: Duration,
706    /// Whether it runs on a thread of its own.
707    pub threaded: bool,
708    /// Whether it is a local listener, bound to the thread that
709    /// registered it.
710    pub local: bool,
711    /// The `after`/`before` targets that are not registered.
712    pub missing: Vec<String>,
713    /// How many times it ran.
714    pub runs: u64,
715    /// How many of those runs failed (contribution dropped).
716    pub failures: u64,
717    /// The last failure, as text.
718    pub last_failure: Option<String>,
719}
720
721impl ListenerInfo {
722    /// The listener `id` with no owner, no constraint, priority 0, the
723    /// default deadline, inline, never run.
724    pub fn new(id: impl Into<String>) -> ListenerInfo {
725        ListenerInfo {
726            id: id.into(),
727            owner: None,
728            after: Vec::new(),
729            before: Vec::new(),
730            priority: 0,
731            deadline: DEFAULT_DEADLINE,
732            threaded: false,
733            local: false,
734            missing: Vec::new(),
735            runs: 0,
736            failures: 0,
737            last_failure: None,
738        }
739    }
740}
741
742/// A point as introspection shows it.
743#[derive(Clone, Debug, PartialEq, Eq)]
744#[non_exhaustive]
745pub struct PointInfo {
746    /// Its id.
747    pub id: &'static str,
748    /// What it is for.
749    pub kind: PointKind,
750    /// Its description; empty until declared.
751    pub description: String,
752    /// Whether its owner declared it ([`Bus::declare_point`](crate::Bus::declare_point)); a point
753    /// listened to before that is not.
754    pub declared: bool,
755    /// The deadline of its listeners that set none.
756    pub deadline: Duration,
757    /// Whether a listener failure refuses the request
758    /// ([`PointSpec::fail_closed`]).
759    pub fail_closed: bool,
760    /// The time one pass of its listeners should take
761    /// ([`PointSpec::budget`]).
762    pub budget: Option<Duration>,
763    /// What its declaration says for capability contracts
764    /// ([`PointSpec::facets`]): read by their crates
765    /// (`bus_contract_journal::is_journaled(&info.facets)`).
766    pub facets: Vec<crate::Facet>,
767    /// Its version ([`PointSpec::version`]).
768    pub version: u32,
769    /// Its listeners, in the order they run.
770    pub listeners: Vec<ListenerInfo>,
771}
772
773impl PointInfo {
774    /// The point `id` of `kind`, not declared, with the default deadline
775    /// and no listener.
776    pub fn new(id: &'static str, kind: PointKind) -> PointInfo {
777        PointInfo {
778            id,
779            kind,
780            description: String::new(),
781            declared: false,
782            deadline: DEFAULT_DEADLINE,
783            fail_closed: false,
784            budget: None,
785            facets: Vec::new(),
786            version: 1,
787            listeners: Vec::new(),
788        }
789    }
790}