Skip to main content

surrealdb_expr/val/
table_name_public.rs

1//! Conversions between the public `Table` value and the engine's table name.
2//!
3//! Neither type is local here, so these cannot be `From` impls: the orphan rule
4//! needs one side to belong to the defining crate. They are not on the types
5//! themselves either — `surrealdb-strand` is a string primitive and does not
6//! know the public contract, and `surrealdb-types` is the public contract and
7//! does not take dependencies on internal crates. Core is where the two meet,
8//! so core owns the conversion.
9
10use surrealdb_types::Table as PublicTable;
11
12use crate::val::TableName;
13
14/// Convert a public table value into the engine's table name.
15pub trait IntoTableName {
16	fn into_table_name(self) -> TableName;
17}
18
19impl IntoTableName for PublicTable {
20	fn into_table_name(self) -> TableName {
21		TableName::new(self.into_string())
22	}
23}
24
25/// Convert the engine's table name into a public table value.
26pub trait IntoPublicTable {
27	fn into_public_table(self) -> PublicTable;
28}
29
30impl IntoPublicTable for TableName {
31	fn into_public_table(self) -> PublicTable {
32		PublicTable::new(self.into_string())
33	}
34}
35
36#[cfg(test)]
37mod tests {
38	use super::*;
39
40	#[test]
41	fn round_trips_without_changing_the_name() {
42		let public = PublicTable::new("users".to_owned());
43		let name = public.clone().into_table_name();
44		assert_eq!(name.as_str(), "users");
45		assert_eq!(name.into_public_table(), public);
46	}
47
48	/// The public form is not escaped, so a name that would need quoting in
49	/// SurrealQL crosses unchanged. Escaping belongs to the language layer.
50	#[test]
51	fn does_not_escape() {
52		assert_eq!(PublicTable::new("select".to_owned()).into_table_name().as_str(), "select");
53	}
54}