surrealdb_core/val/
table.rs

1use std::fmt;
2use std::ops::Deref;
3
4use revision::revisioned;
5use serde::{Deserialize, Serialize};
6
7use crate::expr::Ident;
8use crate::expr::escape::EscapeIdent;
9use crate::val::Strand;
10
11/// A value type referencing a specific table.
12#[revisioned(revision = 1)]
13#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Hash)]
14#[serde(rename = "$surrealdb::private::sql::Table")]
15#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
16pub struct Table(String);
17
18impl Table {
19	/// Create a new strand, returns None if the string contains a null byte.
20	pub fn new(s: String) -> Option<Table> {
21		if s.contains('\0') {
22			None
23		} else {
24			Some(Table(s))
25		}
26	}
27
28	/// Create a new strand, without checking the string.
29	///
30	/// # Safety
31	/// Caller must ensure that string handed as an argument does not contain
32	/// any null bytes.
33	pub unsafe fn new_unchecked(s: String) -> Table {
34		// Check in debug mode if the variants
35		debug_assert!(!s.contains('\0'));
36		Table(s)
37	}
38
39	pub fn from_strand(s: Strand) -> Table {
40		Table(s.into_string())
41	}
42
43	pub fn into_strand(self) -> Strand {
44		unsafe { Strand::new_unchecked(self.0) }
45	}
46
47	pub fn into_string(self) -> String {
48		self.0
49	}
50
51	pub fn as_str(&self) -> &str {
52		&self.0
53	}
54}
55
56impl Deref for Table {
57	type Target = str;
58	fn deref(&self) -> &Self::Target {
59		&self.0
60	}
61}
62
63impl From<Ident> for Table {
64	fn from(value: Ident) -> Self {
65		Table(value.into_string())
66	}
67}
68
69impl fmt::Display for Table {
70	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71		EscapeIdent(&self.0).fmt(f)
72	}
73}