Skip to main content

zaun_community_protocol/signed/descriptor/
parts.rs

1//! Descriptor sub-structures (specification §4.1).
2
3use crate::encoding::{Decode, DecodeError, Encode, EncodeError, Reader, Writer};
4
5macro_rules! byte_enum {
6    ($(#[$doc:meta])* $name:ident, $field:literal { $($(#[$vdoc:meta])* $variant:ident = $value:literal),* $(,)? }) => {
7        $(#[$doc])*
8        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9        #[repr(u8)]
10        pub enum $name {
11            $($(#[$vdoc])* $variant = $value),*
12        }
13
14        impl Encode for $name {
15            fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
16                w.u8(*self as u8);
17                Ok(())
18            }
19        }
20
21        impl Decode for $name {
22            fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
23                match r.u8()? {
24                    $($value => Ok(Self::$variant),)*
25                    _ => Err(DecodeError::Invalid($field)),
26                }
27            }
28        }
29    };
30}
31
32byte_enum!(
33    /// `discovery` (field 17).
34    Discovery, "discovery" {
35        /// `0x00`: invitation only.
36        Private = 0,
37        /// `0x01`: reachable by link.
38        Unlisted = 1,
39        /// `0x02`: may be submitted to directories.
40        Listed = 2,
41    }
42);
43
44byte_enum!(
45    /// `AdmissionSummary.mode`.
46    AdmissionMode, "admission_summary.mode" {
47        /// `0x00`: invitation only.
48        InvitationOnly = 0,
49        /// `0x01`: approval required.
50        ApprovalRequired = 1,
51        /// `0x02`: open.
52        Open = 2,
53    }
54);
55
56byte_enum!(
57    /// `AdmissionSummary.level`: verification level None … Highest (`governance.md`).
58    VerificationLevel, "admission_summary.level" {
59        /// `0x00`.
60        None = 0,
61        /// `0x01`.
62        Low = 1,
63        /// `0x02`.
64        Medium = 2,
65        /// `0x03`.
66        High = 3,
67        /// `0x04`.
68        Highest = 4,
69    }
70);
71
72/// `AdmissionSummary = mode u8 ‖ level u8`: the owner's public claim; the host's policy is
73/// authoritative for enforcement.
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
75pub struct AdmissionSummary {
76    /// Admission mode.
77    pub mode: AdmissionMode,
78    /// Verification level.
79    pub level: VerificationLevel,
80}
81
82impl Encode for AdmissionSummary {
83    fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
84        w.put(&self.mode)?;
85        w.put(&self.level)
86    }
87}
88
89impl Decode for AdmissionSummary {
90    fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
91        Ok(Self {
92            mode: r.get()?,
93            level: r.get()?,
94        })
95    }
96}
97
98/// `DelegationRef = epoch u64 ‖ delegation_hash bytes[32]`, where `delegation_hash` is
99/// `SHA3-256` of the delegation's canonical bytes (not its signature).
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
101pub struct DelegationRef {
102    /// The delegation's epoch.
103    pub epoch: u64,
104    /// `SHA3-256(delegation bytes)`.
105    pub delegation_hash: [u8; 32],
106}
107
108impl Encode for DelegationRef {
109    fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
110        w.u64(self.epoch);
111        w.put(&self.delegation_hash)
112    }
113}
114
115impl Decode for DelegationRef {
116    fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
117        Ok(Self {
118            epoch: r.u64()?,
119            delegation_hash: r.get()?,
120        })
121    }
122}