Skip to main content

macroonz_compiler/codec/
encode.rs

1//! The canonical bytes one refused issue is, for the related identity a diagnostic derives over it.
2//!
3//! The row's position rides ahead of the material it governs, and every variable-length member is framed through the identity home's one framing, so two issues differing in any typed member never encode alike.
4
5use super::CodecIssue;
6use crate::identity::encode_bytes;
7
8impl CodecIssue {
9    /// This issue's canonical bytes on their own.
10    #[must_use]
11    pub fn canonical_bytes(&self) -> Vec<u8> {
12        let mut bytes = Vec::new();
13        self.encode_into(&mut bytes);
14        bytes
15    }
16
17    /// Appends this issue's canonical bytes: the row's position in the declared roster, then the typed material that row carries, framed.
18    pub fn encode_into(&self, into: &mut Vec<u8>) {
19        into.push(self.slot());
20        let mut material = Vec::new();
21        self.material_into(&mut material);
22        encode_bytes(&material, into);
23    }
24
25    /// The typed material one issue carries, through each value's own spelling.
26    ///
27    /// Exhaustive over the roster on purpose: an issue added to [`CodecIssue`] stops compiling HERE until somebody says what of it a preimage commits to, so no issue can be admitted and left out of every identity derived over a refusal that carries it.
28    fn material_into(&self, into: &mut Vec<u8>) {
29        match self {
30            Self::PathSegmentsAbsent
31            | Self::MemberSpellingAbsent
32            | Self::AssemblyRoadAbsent
33            | Self::MembersAbsent => {}
34            Self::SegmentNotAnIdentifier { segment } => encode_bytes(segment.as_bytes(), into),
35            Self::MemberSpellingNotAnIdentifier { spelling }
36            | Self::MemberSpellingDoubled { spelling }
37            | Self::AssemblyRoadNotAnIdentifier { spelling }
38            | Self::RefusalSpellingNotAnIdentifier { spelling }
39            | Self::ModuleSpellingNotAnIdentifier { spelling } => {
40                encode_bytes(spelling.as_bytes(), into);
41            }
42            Self::MemberShadowsBinding { spelling, binding } => {
43                encode_bytes(spelling.as_bytes(), into);
44                encode_bytes(binding.as_bytes(), into);
45            }
46            Self::PathSegmentsUnbounded { bound, observed }
47            | Self::MembersUnbounded { bound, observed } => {
48                into.extend_from_slice(&bound.to_be_bytes());
49                into.extend_from_slice(&observed.to_be_bytes());
50            }
51        }
52    }
53}