spacetimedb_lib/db/
auth.rs

1use spacetimedb_sats::{impl_deserialize, impl_serialize, impl_st, AlgebraicType};
2
3use crate::de::Error;
4
5/// Describe the visibility of the table
6#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum StAccess {
8    /// Visible to all
9    Public,
10    /// Visible only to the owner
11    Private,
12}
13
14impl StAccess {
15    pub fn as_str(&self) -> &'static str {
16        match self {
17            Self::Public => "public",
18            Self::Private => "private",
19        }
20    }
21}
22
23impl<'a> TryFrom<&'a str> for StAccess {
24    type Error = &'a str;
25
26    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
27        Ok(match value {
28            "public" => Self::Public,
29            "private" => Self::Private,
30            x => return Err(x),
31        })
32    }
33}
34
35impl_serialize!([] StAccess, (self, ser) => ser.serialize_str(self.as_str()));
36impl_deserialize!([] StAccess, de => {
37    let value = de.deserialize_str_slice()?;
38    StAccess::try_from(value).map_err(|x| {
39        Error::custom(format!(
40            "DecodeError for StAccess: `{x}`. Expected `public` | 'private'"
41        ))
42    })
43});
44impl_st!([] StAccess, AlgebraicType::String);
45
46/// Describe is the table is a `system table` or not.
47#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
48pub enum StTableType {
49    /// Created by the system
50    ///
51    /// System tables are `StAccess::Public` by default
52    System,
53    /// Created by the User
54    User,
55}
56
57impl StTableType {
58    pub fn as_str(&self) -> &'static str {
59        match self {
60            Self::System => "system",
61            Self::User => "user",
62        }
63    }
64}
65
66impl<'a> TryFrom<&'a str> for StTableType {
67    type Error = &'a str;
68
69    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
70        Ok(match value {
71            "system" => Self::System,
72            "user" => Self::User,
73            x => return Err(x),
74        })
75    }
76}
77
78impl_serialize!([] StTableType, (self, ser) => ser.serialize_str(self.as_str()));
79impl_deserialize!([] StTableType, de => {
80    let value = de.deserialize_str_slice()?;
81    StTableType::try_from(value).map_err(|x| {
82        Error::custom(format!(
83            "DecodeError for StTableType: `{x}`. Expected 'system' | 'user'"
84        ))
85    })
86});
87impl_st!([] StTableType, AlgebraicType::String);