surrealpatch/src/sql/table.rs

84 lines
1.8 KiB
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use crate::sql::common::commas;
use crate::sql::common::escape;
use crate::sql::common::val_char;
use crate::sql::ident::ident_raw;
use nom::multi::separated_nonempty_list;
use nom::IResult;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str;
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Tables(Vec<Table>);
impl fmt::Display for Tables {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.iter().map(|ref v| format!("{}", v)).collect::<Vec<_>>().join(", "))
}
}
pub fn tables(i: &str) -> IResult<&str, Tables> {
let (i, v) = separated_nonempty_list(commas, table)(i)?;
Ok((i, Tables(v)))
}
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Table {
pub name: String,
}
impl<'a> From<&'a str> for Table {
fn from(t: &str) -> Table {
Table {
name: String::from(t),
}
}
}
impl fmt::Display for Table {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", escape(&self.name, &val_char, "`"))
}
}
pub fn table(i: &str) -> IResult<&str, Table> {
let (i, v) = ident_raw(i)?;
Ok((i, Table::from(v)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table_normal() {
let sql = "test";
let res = table(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("test", format!("{}", out));
assert_eq!(out, Table::from("test"));
}
#[test]
fn table_quoted_backtick() {
let sql = "`test`";
let res = table(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("test", format!("{}", out));
assert_eq!(out, Table::from("test"));
}
#[test]
fn table_quoted_brackets() {
let sql = "⟨test⟩";
let res = table(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("test", format!("{}", out));
assert_eq!(out, Table::from("test"));
}
}