surrealdb_core/sql/
ident.rs1use crate::sql::statements::info::InfoStructure;
2use crate::sql::{escape::escape_ident, strand::no_nul_bytes, Value};
3use revision::revisioned;
4use serde::{Deserialize, Serialize};
5use std::fmt::{self, Display, Formatter};
6use std::ops::Deref;
7use std::str;
8
9#[revisioned(revision = 1)]
10#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
11#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
12#[non_exhaustive]
13pub struct Ident(#[serde(with = "no_nul_bytes")] pub String);
14
15impl From<String> for Ident {
16 fn from(v: String) -> Self {
17 Self(v)
18 }
19}
20
21impl From<&str> for Ident {
22 fn from(v: &str) -> Self {
23 Self::from(String::from(v))
24 }
25}
26
27impl Deref for Ident {
28 type Target = String;
29 fn deref(&self) -> &Self::Target {
30 &self.0
31 }
32}
33
34impl Ident {
35 pub fn to_raw(&self) -> String {
37 self.0.to_string()
38 }
39 pub(crate) fn is_dash(&self) -> bool {
41 self.0.as_str() == "-"
42 }
43 pub(crate) fn is_id(&self) -> bool {
45 self.0.as_str() == "id"
46 }
47 pub(crate) fn is_type(&self) -> bool {
49 self.0.as_str() == "type"
50 }
51 pub(crate) fn is_coordinates(&self) -> bool {
53 self.0.as_str() == "coordinates"
54 }
55 pub(crate) fn is_geometries(&self) -> bool {
57 self.0.as_str() == "geometries"
58 }
59}
60
61impl Display for Ident {
62 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
63 Display::fmt(&escape_ident(&self.0), f)
64 }
65}
66
67impl InfoStructure for Ident {
68 fn structure(self) -> Value {
69 self.to_raw().into()
70 }
71}