Skip to main content

LedgerEvent

Struct LedgerEvent 

Source
pub struct LedgerEvent { /* private fields */ }
Expand description

One event on its way into the ledger: the name, the payload, and the envelope the writer computes.

The envelope — ts, event, head, branch, by, and session when an episode is open — is computed and never authored, exactly as share/events.yaml says. This type takes the computed values from the caller rather than reaching for git itself, because a domain that shells out to git while holding a lock on the ledger is a domain that holds the lock for a hundred milliseconds on a loaded machine.

use majordomus_cli::session::{Envelope, LedgerEvent, Vocabulary};

let v = Vocabulary::parse(
    "version: 1\nevents:\n  - id: session.closed\n    requires: [outcome, session_path]\n    public: true\n",
).unwrap();
let envelope = Envelope {
    ts: "2026-09-11T11:02:35Z".into(),
    head: "1a651e644".into(),
    branch: "feature/the-session-domain-is-typed".into(),
    by: "majordomus/0.5.0".into(),
    session: Some("s-20260910205542-e2a6".into()),
};

// the payload its declaration requires
let ok = LedgerEvent::new("session.closed")
    .with("outcome", "closed")
    .with("session_path", ".ai/repo/sessions/x.md");
let line = ok.render(&v, &envelope).expect("a line");
assert!(line.starts_with('{') && line.ends_with('}'));
assert!(line.contains("\"event\":\"session.closed\""));
assert!(line.contains("\"session\":\"s-20260910205542-e2a6\""));

// one key short, and it is refused before anything is written
let short = LedgerEvent::new("session.closed").with("outcome", "closed");
assert!(short.render(&v, &envelope).is_err());

// a name the vocabulary does not declare is refused too
assert!(LedgerEvent::new("session.clsoed").render(&v, &envelope).is_err());

Implementations§

Source§

impl LedgerEvent

Source

pub fn new(name: impl Into<String>) -> Self

An event with no payload yet. The name is not checked here: it is checked against the vocabulary at LedgerEvent::render, which is the last moment before a line could become durable.

use majordomus_cli::session::LedgerEvent;
assert_eq!(LedgerEvent::new("session.closed").name(), "session.closed");
Source

pub fn with(self, key: &str, value: impl Into<String>) -> Self

Add one payload key. Strings are the common case and have their own method so that the ordinary call site carries no json!.

use majordomus_cli::session::{Envelope, LedgerEvent, Vocabulary};
let v = Vocabulary::parse("version: 1\nevents:\n  - id: session.closed\n    requires: [outcome]\n").unwrap();
let envelope = Envelope {
    ts: "t".into(), head: "h".into(), branch: "b".into(), by: "m".into(), session: None,
};
let line = LedgerEvent::new("session.closed").with("outcome", "closed").render(&v, &envelope).unwrap();
assert!(line.contains("\"outcome\":\"closed\""));
Source

pub fn with_value(self, key: &str, value: Value) -> Self

Add one payload key whose value is not a string — a count, a flag, a list.

use majordomus_cli::session::{Envelope, LedgerEvent, Vocabulary};
let v = Vocabulary::parse("version: 1\nevents:\n  - id: ledger.rotated\n    requires: [kept]\n").unwrap();
let envelope = Envelope {
    ts: "t".into(), head: "h".into(), branch: "b".into(), by: "m".into(), session: None,
};
let line = LedgerEvent::new("ledger.rotated")
    .with_value("kept", serde_json::json!(120))
    .render(&v, &envelope)
    .unwrap();
assert!(line.contains("\"kept\":120"));
Source

pub fn name(&self) -> &str

The event’s name, as offered. It is a declared name only once the vocabulary has been asked, which LedgerEvent::render does.

use majordomus_cli::session::LedgerEvent;
assert_eq!(LedgerEvent::new("session.started").name(), "session.started");
Source

pub fn render( &self, vocabulary: &Vocabulary, envelope: &Envelope, ) -> Result<String, LedgerError>

Render the line, refusing an unregistered name or a missing required key. The refusal happens here — before the lock is taken and before anything is written — so that a malformed event never becomes a durable line.

use majordomus_cli::session::{Envelope, LedgerEvent, Vocabulary};

let v = Vocabulary::parse(
    "version: 1\nevents:\n  - id: session.closed\n    requires: [outcome, session_path]\n",
).unwrap();
let envelope = Envelope {
    ts: "2026-09-11T11:02:35Z".into(), head: "1a651e64".into(),
    branch: "master".into(), by: "majordomus/0.5.0".into(),
    session: Some("s-20260910205542-e2a6".into()),
};

let line = LedgerEvent::new("session.closed")
    .with("outcome", "closed")
    .with("session_path", ".ai/repo/sessions/x.md")
    .render(&v, &envelope)
    .expect("a line");
assert!(line.contains("\"event\":\"session.closed\""));
assert!(line.contains("\"session\":\"s-20260910205542-e2a6\""));

// one key short, and nothing is rendered at all
assert!(LedgerEvent::new("session.closed").with("outcome", "closed").render(&v, &envelope).is_err());

Trait Implementations§

Source§

impl Clone for LedgerEvent

Source§

fn clone(&self) -> LedgerEvent

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LedgerEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for LedgerEvent

Source§

fn eq(&self, other: &LedgerEvent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for LedgerEvent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more