Skip to main content

uts_core/codec/v1/timestamp/
encode.rs

1use super::*;
2use crate::{
3    codec::{Encode, Encoder},
4    error::EncodeError,
5};
6
7impl<A: Allocator> Encode for Timestamp<A> {
8    fn encode(&self, encoder: &mut impl Encoder) -> Result<(), EncodeError> {
9        match self {
10            Self::Attestation(attestation) => {
11                encoder.encode(self.op())?;
12                attestation.encode(encoder)?;
13            }
14            Self::Step(step) if step.op == OpCode::FORK => {
15                debug_assert!(step.next.len() >= 2, "FORK must have at least two children");
16                for child in step.next.iter().take(step.next.len() - 1) {
17                    encoder.encode(self.op())?;
18                    child.encode(encoder)?;
19                }
20                // Encode the last child
21                step.next.last().expect("infallible").encode(encoder)?;
22            }
23            Self::Step(step) => {
24                encoder.encode(self.op())?;
25                if !step.data.is_empty() {
26                    debug_assert!(step.op.has_immediate());
27                    encoder.encode_bytes(&step.data)?;
28                }
29                debug_assert_eq!(step.next.len(), 1);
30                step.next[0].encode(encoder)?;
31            }
32        }
33
34        Ok(())
35    }
36}