Skip to main content

m10_protos/
pack.rs

1use core::fmt;
2use core::ops::Deref;
3use core::str::FromStr;
4
5use prost::Message;
6
7pub trait Pack: Default + Message + Sized {
8    const COLLECTION: Collection;
9
10    fn pack(&self) -> Vec<u8> {
11        self.encode_to_vec()
12    }
13
14    fn set_id(&mut self, id: Vec<u8>);
15
16    fn id(&self) -> &[u8];
17}
18
19#[derive(Debug, Copy, Clone, PartialEq)]
20pub enum Collection {
21    AccountMetadata,
22    AccountSets,
23    RoleBindings,
24    Roles,
25    Banks,
26    Locks,
27    SettlementCycles,
28}
29
30impl Deref for Collection {
31    type Target = str;
32
33    fn deref(&self) -> &Self::Target {
34        match self {
35            Collection::AccountMetadata => "account-metadata",
36            Collection::AccountSets => "account-sets",
37            Collection::RoleBindings => "role-bindings",
38            Collection::Roles => "roles",
39            Collection::Banks => "banks",
40            Collection::Locks => "locks",
41            Collection::SettlementCycles => "settlement-cycles",
42        }
43    }
44}
45
46impl fmt::Display for Collection {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(self)
49    }
50}
51
52impl From<Collection> for String {
53    fn from(collection: Collection) -> Self {
54        collection.to_string()
55    }
56}
57
58impl FromStr for Collection {
59    type Err = UnsupportedCollection;
60
61    fn from_str(s: &str) -> Result<Self, Self::Err> {
62        match s {
63            "account-metadata" => Ok(Collection::AccountMetadata),
64            "account-sets" => Ok(Collection::AccountSets),
65            "role-bindings" => Ok(Collection::RoleBindings),
66            "roles" => Ok(Collection::Roles),
67            "banks" => Ok(Collection::Banks),
68            "locks" => Ok(Collection::Locks),
69            "settlement-cycles" => Ok(Collection::SettlementCycles),
70            _unsupported => Err(UnsupportedCollection()),
71        }
72    }
73}
74
75#[derive(Debug)]
76pub struct UnsupportedCollection();
77
78impl fmt::Display for UnsupportedCollection {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        f.write_str("unsupported collection")
81    }
82}
83
84impl std::error::Error for UnsupportedCollection {}