Skip to main content

egui_pop_api/bus/
journal.rs

1//! egui-pop's journal on the bus's (ADR 0030, slice 3): one journal per
2//! process.
3//!
4//! When the application's bus offers the contract's
5//! [`Journal`](bus_contract_journal::Journal) capability, egui-pop's journal
6//! (`AppHandle::publish`, `AppHandle::events_since`,
7//! `AppHandle::last_event`, so agents' `wait` and the debug bridge's
8//! journal readers) is that journal, unless the host keeps a journal of
9//! its own. egui-pop's events are entries of one journaled point,
10//! [`HostEvents`] (`pop.journal`), and read back as host events with
11//! their own names (`command.done`, `job.*`); the bus's other entries
12//! (the application's journaled facts) are read back as events named by
13//! their point. One numbered sequence carries both.
14
15use std::sync::Arc;
16
17use bus_contract::{AnyValue, Point, PointDecl, PointId, PointKey, PointKind};
18use bus_contract_journal::{JOURNALED, Journal, JournalEntry};
19use serde_json::Value as Json;
20
21use crate::host::{Event, Seq};
22
23/// An event egui-pop publishes, as the bus's journal keeps it: who did
24/// it, its name and its data (its number and cause are the entry's).
25#[derive(Clone, Debug, PartialEq)]
26#[non_exhaustive]
27pub struct HostEvent {
28    /// Who did it: `user`, `agent:<name>`, a plugin, the host.
29    pub source: String,
30    /// What happened: `command.done`, `job.started`...
31    pub name: String,
32    pub data: Json,
33}
34
35impl HostEvent {
36    pub fn new(source: impl Into<String>, name: impl Into<String>, data: Json) -> Self {
37        Self {
38            source: source.into(),
39            name: name.into(),
40            data,
41        }
42    }
43}
44
45/// egui-pop's events on the bus's journal (`pop.journal`): each one the
46/// host journal records when it is the bus's, delivered to this point's
47/// listeners by the journal's pump, after the publisher moved on.
48#[derive(Debug)]
49pub struct HostEvents;
50impl Point for HostEvents {
51    const KIND: PointKind = PointKind::Event;
52    const ID: PointId = PointId::new("pop.journal");
53    type Input = HostEvent;
54    type Value = ();
55}
56
57/// The declaration of [`HostEvents`]: journaled, so declared only on a
58/// bus offering the journal capability (it is not in
59/// [`CONTRACT`](super::CONTRACT), which every bus takes).
60pub const HOST_JOURNAL: PointDecl = PointDecl::of::<HostEvents>(
61    "egui-pop's journal on the bus's: each event egui-pop publishes (a command dispatched, done or failed, a job's progress, a link, an agent's session), recorded with a number shared with the application's journaled facts.",
62)
63.facets(&[JOURNALED]);
64
65/// Records an event on the bus's journal; its number, or why not.
66pub(crate) fn publish(
67    journal: &dyn Journal,
68    source: &str,
69    cause: Option<Seq>,
70    name: &str,
71    data: Json,
72) -> Result<Seq, bus_contract::Refused> {
73    journal.publish_any(
74        PointKey::of::<HostEvents>(),
75        Arc::new(HostEvent::new(source, name, data)),
76        fresh,
77        cause,
78    )
79}
80
81/// The value each listener of [`HostEvents`] fills: none.
82fn fresh() -> AnyValue {
83    Box::new(())
84}
85
86/// An entry of the bus's journal, as egui-pop's journal shows it: an
87/// event egui-pop published keeps its source, name and data; any other
88/// entry is named by its point (its kind), its source the process it was
89/// relayed from or `bus`, its data its text read as JSON (a string when
90/// it is not JSON, null when it has none).
91pub(crate) fn event_of(entry: &JournalEntry) -> Event {
92    if let Some(e) = entry.read::<HostEvents>() {
93        return Event {
94            seq: entry.seq,
95            source: e.source.clone(),
96            cause: entry.cause,
97            name: e.name.clone(),
98            data: e.data.clone(),
99        };
100    }
101    Event {
102        seq: entry.seq,
103        source: entry
104            .origin
105            .as_ref()
106            .map_or_else(|| "bus".into(), |o| o.process.clone()),
107        cause: entry.cause,
108        name: entry.kind.clone(),
109        data: entry.text.as_deref().map_or(Json::Null, |t| {
110            serde_json::from_str(t).unwrap_or_else(|_| Json::String(t.into()))
111        }),
112    }
113}