surrealpatch/lib/src/sql/thing.rs

125 lines
2.3 KiB
Rust
Raw Normal View History

2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
use crate::sql::escape::escape_ident;
use crate::sql::id::{id, Id};
2020-06-29 15:36:01 +00:00
use crate::sql::ident::ident_raw;
use crate::sql::serde::is_internal_serialization;
2022-04-09 09:09:01 +00:00
use derive::Store;
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-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 {
pub tb: String,
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,
id: Id::from(v.1),
}
}
}
2020-06-29 15:36:01 +00:00
impl fmt::Display for Thing {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", escape_ident(&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,
{
if is_internal_serialization() {
2021-03-29 15:43:37 +00:00
let mut val = serializer.serialize_struct("Thing", 2)?;
val.serialize_field("tb", &self.tb)?;
2021-03-29 15:43:37 +00:00
val.serialize_field("id", &self.id)?;
val.end()
} else {
let output = format!("{}:{}", self.tb, self.id);
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> {
let (i, t) = ident_raw(i)?;
let (i, _) = char(':')(i)?;
let (i, v) = id(i)?;
2020-06-29 15:36:01 +00:00
Ok((
i,
Thing {
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 {
tb: String::from("test"),
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 {
tb: String::from("test"),
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 {
tb: String::from("test"),
id: Id::from("id"),
2020-06-29 15:36:01 +00:00
}
);
}
}