Skip to main content

spacetimedb_lib/db/
auth.rs

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