Skip to main content

spacetimedb_schema/
table_name.rs

1use crate::identifier::Identifier;
2use core::fmt;
3use core::ops::Deref;
4use spacetimedb_sats::{impl_deserialize, impl_serialize, impl_st, raw_identifier::RawIdentifier};
5
6/// The name of a table.
7#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct TableName(Identifier);
9
10impl_st!([] TableName, ts => Identifier::make_type(ts));
11impl_serialize!([] TableName, (self, ser) => self.0.serialize(ser));
12impl_deserialize!([] TableName, de => Identifier::deserialize(de).map(Self));
13
14impl TableName {
15    pub fn new(id: Identifier) -> Self {
16        Self(id)
17    }
18
19    #[cfg(any(test, feature = "test"))]
20    pub fn for_test(name: &str) -> Self {
21        Self(Identifier::for_test(name))
22    }
23}
24
25impl Deref for TableName {
26    type Target = str;
27
28    fn deref(&self) -> &Self::Target {
29        &self.0
30    }
31}
32
33impl AsRef<str> for TableName {
34    fn as_ref(&self) -> &str {
35        &self.0
36    }
37}
38
39impl From<TableName> for Identifier {
40    fn from(id: TableName) -> Self {
41        id.0
42    }
43}
44
45impl From<TableName> for RawIdentifier {
46    fn from(id: TableName) -> Self {
47        Identifier::from(id).into()
48    }
49}
50
51impl fmt::Debug for TableName {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        fmt::Debug::fmt(&self.0, f)
54    }
55}
56
57impl fmt::Display for TableName {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        fmt::Display::fmt(&self.0, f)
60    }
61}