surrealpatch/core/src/sql/ident.rs

61 lines
1.4 KiB
Rust
Raw Normal View History

use crate::sql::{escape::escape_ident, strand::no_nul_bytes};
use revision::revisioned;
2020-06-29 15:36:01 +00:00
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use std::ops::Deref;
2020-06-29 15:36:01 +00:00
use std::str;
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[revisioned(revision = 1)]
2024-01-09 15:34:52 +00:00
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct Ident(#[serde(with = "no_nul_bytes")] pub String);
2020-06-29 15:36:01 +00:00
impl From<String> for Ident {
fn from(v: String) -> Self {
Self(v)
}
}
2022-05-25 09:42:10 +00:00
impl From<&str> for Ident {
fn from(v: &str) -> Self {
Self::from(String::from(v))
2020-06-29 15:36:01 +00:00
}
}
impl Deref for Ident {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Ident {
2022-10-19 09:55:19 +00:00
/// Convert the Ident to a raw String
pub fn to_raw(&self) -> String {
self.0.to_string()
}
/// Checks if this field is the `id` field
pub(crate) fn is_id(&self) -> bool {
self.0.as_str() == "id"
}
/// Checks if this field is the `type` field
pub(crate) fn is_type(&self) -> bool {
self.0.as_str() == "type"
}
/// Checks if this field is the `coordinates` field
pub(crate) fn is_coordinates(&self) -> bool {
self.0.as_str() == "coordinates"
}
/// Checks if this field is the `geometries` field
pub(crate) fn is_geometries(&self) -> bool {
self.0.as_str() == "geometries"
}
}
impl Display for Ident {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(&escape_ident(&self.0), f)
2020-06-29 15:36:01 +00:00
}
}