2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::common::val_char;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-05-15 08:34:29 +00:00
|
|
|
use crate::sql::escape::escape_ident;
|
2023-05-09 17:48:14 +00:00
|
|
|
use crate::sql::strand::no_nul_bytes;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::branch::alt;
|
2023-01-08 09:05:31 +00:00
|
|
|
use nom::bytes::complete::escaped_transform;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::is_not;
|
2023-04-06 08:33:52 +00:00
|
|
|
use nom::bytes::complete::tag;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::bytes::complete::take_while1;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2023-04-06 08:33:52 +00:00
|
|
|
use nom::combinator::recognize;
|
2023-01-08 09:05:31 +00:00
|
|
|
use nom::combinator::value;
|
2023-04-06 08:33:52 +00:00
|
|
|
use nom::multi::separated_list1;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::sequence::delimited;
|
2023-08-17 18:03:46 +00:00
|
|
|
use revision::revisioned;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-10-04 21:51:18 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2022-05-05 00:27:57 +00:00
|
|
|
use std::ops::Deref;
|
2020-06-29 15:36:01 +00:00
|
|
|
use std::str;
|
|
|
|
|
2023-09-08 11:28:36 +00:00
|
|
|
use super::error::expected;
|
|
|
|
|
2022-05-15 08:34:29 +00:00
|
|
|
const BRACKET_L: char = '⟨';
|
|
|
|
const BRACKET_R: char = '⟩';
|
2023-05-09 17:48:14 +00:00
|
|
|
const BRACKET_END_NUL: &str = "⟩\0";
|
2022-05-15 08:34:29 +00:00
|
|
|
|
2023-01-08 09:05:31 +00:00
|
|
|
const BACKTICK: char = '`';
|
2023-05-09 17:48:14 +00:00
|
|
|
const BACKTICK_ESC_NUL: &str = "`\\\0";
|
2022-05-15 08:34:29 +00:00
|
|
|
|
2022-10-27 12:23:24 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
|
2023-08-17 18:03:46 +00:00
|
|
|
#[revisioned(revision = 1)]
|
2023-05-09 17:48:14 +00:00
|
|
|
pub struct Ident(#[serde(with = "no_nul_bytes")] pub String);
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2021-05-24 08:18:58 +00:00
|
|
|
impl From<String> for Ident {
|
2023-03-31 15:42:29 +00:00
|
|
|
fn from(v: String) -> Self {
|
|
|
|
Self(v)
|
2021-05-24 08:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 09:42:10 +00:00
|
|
|
impl From<&str> for Ident {
|
2023-03-31 15:42:29 +00:00
|
|
|
fn from(v: &str) -> Self {
|
|
|
|
Self::from(String::from(v))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:27:57 +00:00
|
|
|
impl Deref for Ident {
|
|
|
|
type Target = String;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2022-04-07 10:12:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 19:44:27 +00:00
|
|
|
impl Ident {
|
2022-10-19 09:55:19 +00:00
|
|
|
/// Convert the Ident to a raw String
|
2022-05-31 19:44:27 +00:00
|
|
|
pub fn to_raw(&self) -> String {
|
|
|
|
self.0.to_string()
|
|
|
|
}
|
2023-07-01 21:04:24 +00:00
|
|
|
/// Checks if this field is the `id` field
|
2022-10-19 10:03:13 +00:00
|
|
|
pub(crate) fn is_id(&self) -> bool {
|
|
|
|
self.0.as_str() == "id"
|
|
|
|
}
|
2023-07-01 21:04:24 +00:00
|
|
|
/// 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"
|
|
|
|
}
|
2022-05-31 19:44:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 21:51:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ident(i: &str) -> IResult<&str, Ident> {
|
2023-09-08 11:28:36 +00:00
|
|
|
let (i, v) = expected("an identifier", ident_raw)(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((i, Ident::from(v)))
|
|
|
|
}
|
|
|
|
|
2023-03-25 19:44:03 +00:00
|
|
|
pub fn plain(i: &str) -> IResult<&str, Ident> {
|
2023-04-06 08:33:52 +00:00
|
|
|
let (i, v) = take_while1(val_char)(i)?;
|
|
|
|
Ok((i, Ident::from(v)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn multi(i: &str) -> IResult<&str, Ident> {
|
|
|
|
let (i, v) = recognize(separated_list1(tag("::"), take_while1(val_char)))(i)?;
|
2023-03-25 19:44:03 +00:00
|
|
|
Ok((i, Ident::from(v)))
|
|
|
|
}
|
|
|
|
|
2021-05-24 08:18:58 +00:00
|
|
|
pub fn ident_raw(i: &str) -> IResult<&str, String> {
|
|
|
|
let (i, v) = alt((ident_default, ident_backtick, ident_brackets))(i)?;
|
2022-03-04 16:01:32 +00:00
|
|
|
Ok((i, v))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 08:18:58 +00:00
|
|
|
fn ident_default(i: &str) -> IResult<&str, String> {
|
|
|
|
let (i, v) = take_while1(val_char)(i)?;
|
|
|
|
Ok((i, String::from(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 08:18:58 +00:00
|
|
|
fn ident_backtick(i: &str) -> IResult<&str, String> {
|
2023-01-08 09:05:31 +00:00
|
|
|
let (i, _) = char(BACKTICK)(i)?;
|
|
|
|
let (i, v) = escaped_transform(
|
2023-05-09 17:48:14 +00:00
|
|
|
is_not(BACKTICK_ESC_NUL),
|
2023-01-08 09:05:31 +00:00
|
|
|
'\\',
|
|
|
|
alt((
|
|
|
|
value('\u{5c}', char('\\')),
|
|
|
|
value('\u{60}', char('`')),
|
|
|
|
value('\u{2f}', char('/')),
|
|
|
|
value('\u{08}', char('b')),
|
|
|
|
value('\u{0c}', char('f')),
|
|
|
|
value('\u{0a}', char('n')),
|
|
|
|
value('\u{0d}', char('r')),
|
|
|
|
value('\u{09}', char('t')),
|
|
|
|
)),
|
|
|
|
)(i)?;
|
|
|
|
let (i, _) = char(BACKTICK)(i)?;
|
|
|
|
Ok((i, v))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 08:18:58 +00:00
|
|
|
fn ident_brackets(i: &str) -> IResult<&str, String> {
|
2023-05-09 17:48:14 +00:00
|
|
|
let (i, v) = delimited(char(BRACKET_L), is_not(BRACKET_END_NUL), char(BRACKET_R))(i)?;
|
2021-05-24 08:18:58 +00:00
|
|
|
Ok((i, String::from(v)))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ident_normal() {
|
|
|
|
let sql = "test";
|
|
|
|
let res = ident(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
|
|
|
assert_eq!(out, Ident::from("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ident_quoted_backtick() {
|
|
|
|
let sql = "`test`";
|
|
|
|
let res = ident(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
|
|
|
assert_eq!(out, Ident::from("test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ident_quoted_brackets() {
|
|
|
|
let sql = "⟨test⟩";
|
|
|
|
let res = ident(sql);
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
|
|
|
assert_eq!(out, Ident::from("test"));
|
|
|
|
}
|
|
|
|
}
|