Skip to main content

zaun_community_protocol/signed/descriptor/
codec.rs

1//! Canonical encoding of the descriptor (fields 1–23 in table order).
2
3use super::CommunityDescriptor;
4use super::rules::{
5    CATEGORIES_MAX, CATEGORY_BYTES, CONTACT_BYTES, DESCRIPTION_BYTES, LANGUAGE_BYTES,
6    LANGUAGES_MAX, LINK_BYTES, LINKS_MAX, MODERATION_BYTES, NAME_BYTES, ROTATIONS_MAX, RULE_BYTES,
7    RULES_MAX,
8};
9use crate::encoding::{Decode, DecodeError, Encode, EncodeError, Reader, Writer};
10use crate::signed::fields::{
11    decode_checked, encode_checked, get_opt_string, get_strings, put_opt_string, put_strings,
12};
13use crate::signed::object::{FORMAT_V1, read_format};
14
15impl Encode for CommunityDescriptor {
16    fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
17        encode_checked(self.rules())?;
18        w.u8(FORMAT_V1);
19        w.put(&self.community_id)?;
20        w.u64(self.revision);
21        w.put(&self.root_public_key)?;
22        w.list(&self.rotations, ROTATIONS_MAX, Writer::put)?;
23        w.u8(self.min_suite);
24        w.string(&self.name, NAME_BYTES)?;
25        put_opt_string(w, self.description.as_ref(), DESCRIPTION_BYTES)?;
26        w.option(self.icon.as_ref(), Writer::put)?;
27        put_strings(w, &self.languages, LANGUAGES_MAX, LANGUAGE_BYTES)?;
28        put_strings(w, &self.categories, CATEGORIES_MAX, CATEGORY_BYTES)?;
29        put_strings(w, &self.rules, RULES_MAX, RULE_BYTES)?;
30        put_opt_string(w, self.moderation_policy.as_ref(), MODERATION_BYTES)?;
31        put_opt_string(w, self.operator_contact.as_ref(), CONTACT_BYTES)?;
32        put_opt_string(w, self.abuse_contact.as_ref(), CONTACT_BYTES)?;
33        w.put(&self.admission_summary)?;
34        w.put(&self.discovery)?;
35        w.u16(self.protocol_min);
36        w.u16(self.protocol_max);
37        w.put(&self.owner_member_key)?;
38        w.option(self.delegation.as_ref(), Writer::put)?;
39        put_strings(w, &self.public_channel_links, LINKS_MAX, LINK_BYTES)?;
40        w.u64(self.issued_at);
41        Ok(())
42    }
43}
44
45impl Decode for CommunityDescriptor {
46    fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
47        read_format(r)?;
48        let value = Self {
49            community_id: r.get()?,
50            revision: r.u64()?,
51            root_public_key: r.get()?,
52            rotations: r.list(ROTATIONS_MAX, Reader::get)?,
53            min_suite: r.u8()?,
54            name: r.string(NAME_BYTES)?.to_owned(),
55            description: get_opt_string(r, DESCRIPTION_BYTES)?,
56            icon: r.option(Reader::get)?,
57            languages: get_strings(r, LANGUAGES_MAX, LANGUAGE_BYTES)?,
58            categories: get_strings(r, CATEGORIES_MAX, CATEGORY_BYTES)?,
59            rules: get_strings(r, RULES_MAX, RULE_BYTES)?,
60            moderation_policy: get_opt_string(r, MODERATION_BYTES)?,
61            operator_contact: get_opt_string(r, CONTACT_BYTES)?,
62            abuse_contact: get_opt_string(r, CONTACT_BYTES)?,
63            admission_summary: r.get()?,
64            discovery: r.get()?,
65            protocol_min: r.u16()?,
66            protocol_max: r.u16()?,
67            owner_member_key: r.get()?,
68            delegation: r.option(Reader::get)?,
69            public_channel_links: get_strings(r, LINKS_MAX, LINK_BYTES)?,
70            issued_at: r.u64()?,
71        };
72        decode_checked(value, Self::rules)
73    }
74}