2020-06-29 15:36:01 +00:00
|
|
|
use crate::sql::common::escape;
|
|
|
|
use crate::sql::common::val_char;
|
|
|
|
use crate::sql::ident::ident_raw;
|
|
|
|
use nom::bytes::complete::tag;
|
|
|
|
use nom::IResult;
|
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;
|
|
|
|
|
2021-03-29 15:43:37 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Deserialize)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub struct Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
pub tb: String,
|
2020-06-29 15:36:01 +00:00
|
|
|
pub id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Thing {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-05-24 08:18:58 +00:00
|
|
|
let t = escape(&self.tb, &val_char, "`");
|
2021-03-29 15:43:37 +00:00
|
|
|
let i = escape(&self.id, &val_char, "`");
|
|
|
|
write!(f, "{}:{}", t, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Thing {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
|
|
|
if serializer.is_human_readable() {
|
2021-05-24 08:18:58 +00:00
|
|
|
let output = format!("{}:{}", self.tb, self.id);
|
2021-03-29 15:43:37 +00:00
|
|
|
serializer.serialize_some(&output)
|
|
|
|
} else {
|
|
|
|
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()
|
|
|
|
}
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn thing(i: &str) -> IResult<&str, Thing> {
|
|
|
|
let (i, t) = ident_raw(i)?;
|
|
|
|
let (i, _) = tag(":")(i)?;
|
|
|
|
let (i, v) = ident_raw(i)?;
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
Thing {
|
2021-05-24 08:18:58 +00:00
|
|
|
tb: t,
|
|
|
|
id: v,
|
2020-06-29 15:36:01 +00:00
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[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"),
|
2020-06-29 15:36:01 +00:00
|
|
|
id: String::from("id"),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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"),
|
2020-06-29 15:36:01 +00:00
|
|
|
id: String::from("id"),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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"),
|
2020-06-29 15:36:01 +00:00
|
|
|
id: String::from("id"),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|