Skip to main content

weirdnet_collab/
ids.rs

1//! Names the app chooses: projects, roles, permissions, streams and records.
2use serde::{Deserialize, Serialize};
3use std::fmt;
4
5macro_rules! name {
6    ($(#[$m:meta])* $t:ident) => {
7        $(#[$m])*
8        #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
9        #[serde(transparent)]
10        pub struct $t(pub String);
11
12        impl $t {
13            /// From text.
14            pub fn new(s: impl Into<String>) -> Self {
15                $t(s.into())
16            }
17
18            /// The text.
19            pub fn as_str(&self) -> &str {
20                &self.0
21            }
22        }
23
24        impl fmt::Display for $t {
25            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26                f.write_str(&self.0)
27            }
28        }
29
30        impl From<&str> for $t {
31            fn from(s: &str) -> Self {
32                $t(s.to_string())
33            }
34        }
35    };
36}
37
38name!(
39    /// A project: the one scope permissions stop at.
40    ProjectId
41);
42name!(
43    /// A role such as `host`, `editor` or `viewer`; the app defines the set.
44    RoleId
45);
46name!(
47    /// A permission such as `project.edit`; the app defines the catalog.
48    Permission
49);
50name!(
51    /// A stream: only owner consent looks at one.
52    StreamId
53);
54name!(
55    /// A rule, grant, invite or audit entry id.
56    RecordId
57);