Skip to main content

surrealdb_types/value/
table.rs

1use std::fmt::Display;
2use std::ops::Deref;
3
4use serde::{Deserialize, Serialize};
5use surrealdb_types_derive::write_sql;
6
7use crate as surrealdb_types;
8use crate::sql::{SqlFormat, ToSql};
9
10/// A value type referencing a specific table.
11#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
12#[repr(transparent)]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14pub struct Table(pub(crate) String);
15
16impl Table {
17	/// Create a new table.
18	pub fn new(s: impl Into<String>) -> Self {
19		Table(s.into())
20	}
21
22	/// Convert the table to a string.
23	pub fn into_string(self) -> String {
24		self.0
25	}
26
27	/// Convert into the inner String
28	pub fn into_inner(self) -> String {
29		self.0
30	}
31
32	/// Get the table as a string.
33	pub fn as_str(&self) -> &str {
34		&self.0
35	}
36}
37
38impl Deref for Table {
39	type Target = String;
40	fn deref(&self) -> &Self::Target {
41		&self.0
42	}
43}
44
45impl Display for Table {
46	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47		write!(f, "{}", self.0)
48	}
49}
50
51impl ToSql for Table {
52	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
53		use crate::utils::escape::EscapeSqonIdent;
54		write_sql!(f, fmt, "{}", EscapeSqonIdent(&self.0));
55	}
56}
57
58impl From<&str> for Table {
59	fn from(s: &str) -> Self {
60		Table::new(s.to_string())
61	}
62}
63
64impl From<String> for Table {
65	fn from(s: String) -> Self {
66		Table::new(s)
67	}
68}
69
70impl From<Table> for String {
71	fn from(value: Table) -> Self {
72		value.0
73	}
74}