Skip to main content

macroonz_compiler/origin/
encode.rs

1use super::{DecisionTrace, Nonclaim, OriginEdge, OriginTrail, TraceDecision, TraceEntry};
2use crate::identity::{encode_bytes, encode_length};
3
4impl OriginEdge {
5    /// Appends this edge's canonical bytes: the node it starts at, the relation slot, the node it produces.
6    pub fn encode_into(&self, into: &mut Vec<u8>) {
7        encode_bytes(self.from.as_bytes(), into);
8        into.push(self.relation.slot());
9        encode_bytes(self.to.as_bytes(), into);
10    }
11}
12
13impl OriginTrail {
14    /// Appends this trail's canonical bytes: the edge count, then every edge in walk order.
15    pub fn encode_into(&self, into: &mut Vec<u8>) {
16        encode_length(self.edges().count(), into);
17        for edge in self.edges() {
18            edge.encode_into(into);
19        }
20    }
21}
22
23impl TraceDecision {
24    /// Appends this decision's canonical bytes: the discriminant, then the cited fact where one was cited.
25    /// [`TraceDecision::NotRun`] writes its discriminant followed by an empty framed citation, so every decision keeps one unambiguous citation seat.
26    pub fn encode_into(&self, into: &mut Vec<u8>) {
27        into.push(self.slot());
28        match self {
29            Self::SelectedBecause(cited) | Self::OmittedBecause(cited) => {
30                encode_bytes(&cited.citation_bytes(), into);
31            }
32            Self::NotRun => encode_bytes(&[], into),
33        }
34    }
35}
36
37impl TraceEntry {
38    /// Appends this entry's canonical bytes: the subject, then the decision.
39    pub fn encode_into(&self, into: &mut Vec<u8>) {
40        encode_bytes(self.subject.as_bytes(), into);
41        self.decision.encode_into(into);
42    }
43}
44
45impl DecisionTrace {
46    /// Appends this trace's canonical bytes: the entry count, then every entry in selection order.
47    pub fn encode_into(&self, into: &mut Vec<u8>) {
48        encode_length(self.entries().count(), into);
49        for entry in self.entries() {
50            entry.encode_into(into);
51        }
52    }
53}
54
55impl Nonclaim {
56    /// Appends this nonclaim's canonical bytes: the unclaimed subject, then the fact that leaves it unclaimed.
57    pub fn encode_into(&self, into: &mut Vec<u8>) {
58        encode_bytes(self.unclaimed.as_bytes(), into);
59        encode_bytes(&self.because.citation_bytes(), into);
60    }
61}