m10_sdk_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        let mut document = Vec::new();
12        self.encode(&mut document).unwrap();
13        document
14    }
15
16    fn set_id(&mut self, id: Vec<u8>);
17
18    fn id(&self) -> &[u8];
19}
20
21#[derive(Debug, Copy, Clone, PartialEq)]
22pub enum Collection {
23    Accounts,
24    AccountSets,
25    RoleBindings,
26    Roles,
27}
28
29impl Deref for Collection {
30    type Target = str;
31
32    fn deref(&self) -> &Self::Target {
33        match self {
34            Collection::Accounts => "accounts",
35            Collection::AccountSets => "account-sets",
36            Collection::RoleBindings => "role-bindings",
37            Collection::Roles => "roles",
38        }
39    }
40}
41
42impl fmt::Display for Collection {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        f.write_str(&**self)
45    }
46}
47
48impl From<Collection> for String {
49    fn from(collection: Collection) -> Self {
50        collection.to_string()
51    }
52}
53
54impl FromStr for Collection {
55    type Err = UnsupportedCollection;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        match s {
59            "accounts" => Ok(Collection::Accounts),
60            "account-sets" => Ok(Collection::AccountSets),
61            "role-bindings" => Ok(Collection::RoleBindings),
62            "roles" => Ok(Collection::Roles),
63            _unsupported => Err(UnsupportedCollection()),
64        }
65    }
66}
67
68#[derive(Debug)]
69pub struct UnsupportedCollection();
70
71impl fmt::Display for UnsupportedCollection {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        f.write_str("unsupported collection")
74    }
75}
76
77impl std::error::Error for UnsupportedCollection {}