zenith_tx/result.rs
1//! Transaction result types: [`TxResult`], [`TxStatus`], and [`TxError`].
2
3use std::fmt;
4
5use zenith_core::Diagnostic;
6
7/// The outcome status of a transaction run.
8#[derive(Debug, Clone, PartialEq)]
9pub enum TxStatus {
10 /// All ops applied and no diagnostics at Error or Warning severity.
11 Accepted,
12 /// All ops applied but at least one Warning-severity diagnostic was
13 /// produced (by op application or post-apply validation).
14 AcceptedWithWarnings,
15 /// At least one Error-severity diagnostic was produced; the document was
16 /// not changed (`source_after == source_before`).
17 Rejected,
18}
19
20/// The complete result of a transaction run, returned by both dry-run and apply.
21///
22/// Dry-run vs. apply is the *caller's* concern (whether to persist
23/// `source_after` to disk). Both paths through `run_transaction` return this
24/// identical shape.
25#[derive(Debug, Clone, PartialEq)]
26pub struct TxResult {
27 /// Overall outcome of the transaction.
28 pub status: TxStatus,
29 /// All diagnostics: from op-application and from post-apply validation.
30 pub diagnostics: Vec<Diagnostic>,
31 /// Canonical-formatted source of the *original* document (before any ops).
32 pub source_before: String,
33 /// Canonical-formatted source of the *result* document.
34 ///
35 /// Equal to `source_before` when `status == Rejected`.
36 pub source_after: String,
37 /// Node ids touched by successfully-applied ops, in first-seen application
38 /// order, de-duplicated (no HashMap — stable insertion-order `Vec` with a
39 /// linear membership check keeps the output deterministic).
40 pub affected_node_ids: Vec<String>,
41}
42
43// ── TxError ───────────────────────────────────────────────────────────────────
44
45/// An error that prevents a transaction result from being produced at all.
46///
47/// Distinct from a rejected transaction (which does produce a [`TxResult`]).
48/// Only returned for envelope-parse failures or formatter failures that make
49/// it impossible to produce `source_before` / `source_after`.
50#[derive(Debug, Clone, PartialEq)]
51pub struct TxError {
52 pub message: String,
53}
54
55impl fmt::Display for TxError {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "zenith-tx error: {}", self.message)
58 }
59}
60
61impl std::error::Error for TxError {}