2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::common::escape;
|
|
|
|
use crate::sql::common::val_char;
|
2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::is_not;
|
|
|
|
use nom::bytes::complete::take_while1;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2020-06-29 15:36:01 +00:00
|
|
|
use nom::sequence::delimited;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
use std::str;
|
|
|
|
|
2022-01-13 17:36:41 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Ident {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2021-05-24 08:18:58 +00:00
|
|
|
impl From<String> for Ident {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Ident {
|
|
|
|
name: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
impl<'a> From<&'a str> for Ident {
|
|
|
|
fn from(i: &str) -> Ident {
|
|
|
|
Ident {
|
|
|
|
name: String::from(i),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:12:48 +00:00
|
|
|
impl<'a> From<&'a String> for Ident {
|
|
|
|
fn from(i: &String) -> Ident {
|
|
|
|
Ident {
|
|
|
|
name: String::from(i),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
impl fmt::Display for Ident {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", escape(&self.name, &val_char, "`"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ident(i: &str) -> IResult<&str, Ident> {
|
|
|
|
let (i, v) = ident_raw(i)?;
|
|
|
|
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> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, v) = delimited(char('`'), is_not("`"), char('`'))(i)?;
|
2021-05-24 08:18:58 +00:00
|
|
|
Ok((i, String::from(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> {
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, v) = delimited(char('⟨'), is_not("⟩"), char('⟩'))(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);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
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);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
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);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test", format!("{}", out));
|
|
|
|
assert_eq!(out, Ident::from("test"));
|
|
|
|
}
|
|
|
|
}
|