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}
27
28impl Deref for Collection {
29 type Target = str;
30
31 fn deref(&self) -> &Self::Target {
32 match self {
33 Collection::AccountMetadata => "account-metadata",
34 Collection::AccountSets => "account-sets",
35 Collection::RoleBindings => "role-bindings",
36 Collection::Roles => "roles",
37 Collection::Banks => "banks",
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 "account-metadata" => Ok(Collection::AccountMetadata),
60 "account-sets" => Ok(Collection::AccountSets),
61 "role-bindings" => Ok(Collection::RoleBindings),
62 "roles" => Ok(Collection::Roles),
63 "banks" => Ok(Collection::Banks),
64 _unsupported => Err(UnsupportedCollection()),
65 }
66 }
67}
68
69#[derive(Debug)]
70pub struct UnsupportedCollection();
71
72impl fmt::Display for UnsupportedCollection {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 f.write_str("unsupported collection")
75 }
76}
77
78impl std::error::Error for UnsupportedCollection {}