surrealdb_sql/
ident.rs

1use crate::{escape::escape_ident, strand::no_nul_bytes};
2use revision::revisioned;
3use serde::{Deserialize, Serialize};
4use std::fmt::{self, Display, Formatter};
5use std::ops::Deref;
6use std::str;
7
8#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
9#[revisioned(revision = 1)]
10pub struct Ident(#[serde(with = "no_nul_bytes")] pub String);
11
12impl From<String> for Ident {
13	fn from(v: String) -> Self {
14		Self(v)
15	}
16}
17
18impl From<&str> for Ident {
19	fn from(v: &str) -> Self {
20		Self::from(String::from(v))
21	}
22}
23
24impl Deref for Ident {
25	type Target = String;
26	fn deref(&self) -> &Self::Target {
27		&self.0
28	}
29}
30
31impl Ident {
32	/// Convert the Ident to a raw String
33	pub fn to_raw(&self) -> String {
34		self.0.to_string()
35	}
36	/// Checks if this field is the `id` field
37	pub(crate) fn is_id(&self) -> bool {
38		self.0.as_str() == "id"
39	}
40	/// Checks if this field is the `type` field
41	pub(crate) fn is_type(&self) -> bool {
42		self.0.as_str() == "type"
43	}
44	/// Checks if this field is the `coordinates` field
45	pub(crate) fn is_coordinates(&self) -> bool {
46		self.0.as_str() == "coordinates"
47	}
48	/// Checks if this field is the `geometries` field
49	pub(crate) fn is_geometries(&self) -> bool {
50		self.0.as_str() == "geometries"
51	}
52}
53
54impl Display for Ident {
55	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
56		Display::fmt(&escape_ident(&self.0), f)
57	}
58}