2022-01-16 20:31:50 +00:00
|
|
|
use crate::sql::error::IResult;
|
2022-07-06 09:08:11 +00:00
|
|
|
use crate::sql::escape::escape_id;
|
2022-03-18 07:24:36 +00:00
|
|
|
use crate::sql::id::{id, Id};
|
2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::ident::ident_raw;
|
2022-05-21 00:35:59 +00:00
|
|
|
use crate::sql::serde::is_internal_serialization;
|
2022-04-09 09:09:01 +00:00
|
|
|
use derive::Store;
|
2022-07-03 19:28:30 +00:00
|
|
|
use nom::branch::alt;
|
2022-03-16 23:52:25 +00:00
|
|
|
use nom::character::complete::char;
|
2021-03-29 15:43:37 +00:00
|
|
|
use serde::ser::SerializeStruct;
|
2020-06-29 15:36:01 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::fmt;
|
|
|
|
|
2022-07-03 19:28:30 +00:00
|
|
|
const SINGLE: char = '\'';
|
|
|
|
const DOUBLE: char = '"';
|
|
|
|
|
2022-04-09 09:09:01 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Deserialize, Store)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
pub tb: String,
|
2022-03-18 07:24:36 +00:00
|
|
|
pub id: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(String, Id)> for Thing {
|
|
|
|
fn from(v: (String, Id)) -> Self {
|
|
|
|
Thing {
|
|
|
|
tb: v.0,
|
|
|
|
id: v.1,
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 18:11:44 +00:00
|
|
|
impl From<(String, String)> for Thing {
|
|
|
|
fn from(v: (String, String)) -> Self {
|
|
|
|
Thing {
|
|
|
|
tb: v.0,
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from(v.1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 08:18:08 +00:00
|
|
|
impl From<(&str, &str)> for Thing {
|
|
|
|
fn from(v: (&str, &str)) -> Self {
|
|
|
|
Thing {
|
|
|
|
tb: v.0.to_owned(),
|
|
|
|
id: Id::from(v.1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Thing {
|
|
|
|
pub fn to_raw(&self) -> String {
|
|
|
|
self.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
impl fmt::Display for Thing {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-07-06 09:08:11 +00:00
|
|
|
write!(f, "{}:{}", escape_id(&self.tb), self.id)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Thing {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
2022-05-21 00:35:59 +00:00
|
|
|
if is_internal_serialization() {
|
2021-03-29 15:43:37 +00:00
|
|
|
let mut val = serializer.serialize_struct("Thing", 2)?;
|
2021-05-24 08:18:58 +00:00
|
|
|
val.serialize_field("tb", &self.tb)?;
|
2021-03-29 15:43:37 +00:00
|
|
|
val.serialize_field("id", &self.id)?;
|
|
|
|
val.end()
|
2022-05-21 00:35:59 +00:00
|
|
|
} else {
|
2022-07-06 09:09:21 +00:00
|
|
|
let output = self.to_string();
|
2022-05-21 00:35:59 +00:00
|
|
|
serializer.serialize_some(&output)
|
2021-03-29 15:43:37 +00:00
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn thing(i: &str) -> IResult<&str, Thing> {
|
2022-07-03 19:28:30 +00:00
|
|
|
let (i, v) = thing_raw(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thing_raw(i: &str) -> IResult<&str, Thing> {
|
|
|
|
alt((thing_normal, thing_single, thing_double))(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thing_normal(i: &str) -> IResult<&str, Thing> {
|
2020-06-29 15:36:01 +00:00
|
|
|
let (i, t) = ident_raw(i)?;
|
2022-03-16 23:52:25 +00:00
|
|
|
let (i, _) = char(':')(i)?;
|
2022-03-18 07:24:36 +00:00
|
|
|
let (i, v) = id(i)?;
|
2020-06-29 15:36:01 +00:00
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: t,
|
|
|
|
id: v,
|
2020-06-29 15:36:01 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-07-03 19:28:30 +00:00
|
|
|
fn thing_single(i: &str) -> IResult<&str, Thing> {
|
|
|
|
let (i, _) = char(SINGLE)(i)?;
|
|
|
|
let (i, v) = thing_normal(i)?;
|
|
|
|
let (i, _) = char(SINGLE)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thing_double(i: &str) -> IResult<&str, Thing> {
|
|
|
|
let (i, _) = char(DOUBLE)(i)?;
|
|
|
|
let (i, v) = thing_normal(i)?;
|
|
|
|
let (i, _) = char(DOUBLE)(i)?;
|
|
|
|
Ok((i, v))
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:36:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
2022-08-26 23:33:52 +00:00
|
|
|
use crate::sql::array::Array;
|
|
|
|
use crate::sql::object::Object;
|
|
|
|
use crate::sql::value::Value;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_normal() {
|
|
|
|
let sql = "test:id";
|
|
|
|
let res = thing(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test:id", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("id"),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_quoted_backtick() {
|
|
|
|
let sql = "`test`:`id`";
|
|
|
|
let res = thing(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test:id", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("id"),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_quoted_brackets() {
|
|
|
|
let sql = "⟨test⟩:⟨id⟩";
|
|
|
|
let res = thing(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!("test:id", format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: String::from("test"),
|
2022-03-18 07:24:36 +00:00
|
|
|
id: Id::from("id"),
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-08-26 23:33:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_object() {
|
|
|
|
let sql = "test:{ location: 'GBR', year: 2022 }";
|
|
|
|
let res = thing(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(r#"test:{ location: "GBR", year: 2022 }"#, format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
|
|
|
tb: String::from("test"),
|
|
|
|
id: Id::Object(Object::from(map! {
|
|
|
|
"location".to_string() => Value::from("GBR"),
|
|
|
|
"year".to_string() => Value::from(2022),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thing_array() {
|
|
|
|
let sql = "test:['GBR', 2022]";
|
|
|
|
let res = thing(sql);
|
|
|
|
assert!(res.is_ok());
|
|
|
|
let out = res.unwrap().1;
|
|
|
|
assert_eq!(r#"test:["GBR", 2022]"#, format!("{}", out));
|
|
|
|
assert_eq!(
|
|
|
|
out,
|
|
|
|
Thing {
|
|
|
|
tb: String::from("test"),
|
|
|
|
id: Id::Array(Array::from(vec![Value::from("GBR"), Value::from(2022)])),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|