Skip to main content

codetether_agent/rlm/oracle/record/
convert.rs

1//! Conversion from [`OracleResult`] enum to the flat
2//! [`OracleTraceRecord`] struct used for persistence.
3//!
4//! Each enum variant is mapped to a `verdict` string with
5//! optional `reason` and `agreement_ratio` metadata.
6//!
7//! # Examples
8//!
9//! ```ignore
10//! let record = OracleResult::Golden(trace).to_record();
11//! assert_eq!(record.verdict, "golden");
12//! ```
13
14use super::super::trace_types::OracleResult;
15use super::OracleTraceRecord;
16
17impl OracleResult {
18    /// Flatten the verdict enum into a serialisable record.
19    ///
20    /// Copies the inner [`super::super::trace_types::ValidatedTrace`]
21    /// and maps the variant to a `verdict` string suitable for
22    /// JSON export and S3 key partitioning.
23    ///
24    /// # Examples
25    ///
26    /// ```ignore
27    /// let record = result.to_record();
28    /// serde_json::to_string(&record).unwrap();
29    /// ```
30    pub fn to_record(&self) -> OracleTraceRecord {
31        match self {
32            Self::Golden(trace) => OracleTraceRecord {
33                verdict: "golden".into(),
34                reason: None,
35                agreement_ratio: None,
36                trace: trace.clone(),
37            },
38            Self::Consensus {
39                trace,
40                agreement_ratio,
41            } => OracleTraceRecord {
42                verdict: "consensus".into(),
43                reason: None,
44                agreement_ratio: Some(*agreement_ratio),
45                trace: trace.clone(),
46            },
47            Self::Unverified { reason, trace } => OracleTraceRecord {
48                verdict: "unverified".into(),
49                reason: Some(reason.clone()),
50                agreement_ratio: None,
51                trace: trace.clone(),
52            },
53            Self::Failed { reason, trace, .. } => OracleTraceRecord {
54                verdict: "failed".into(),
55                reason: Some(reason.clone()),
56                agreement_ratio: None,
57                trace: trace.clone(),
58            },
59        }
60    }
61}