spacetimedb_lib/db/
auth.rs

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