surrealpatch/lib/src/sql/thing.rs

230 lines
4.6 KiB
Rust
Raw Normal View History

use crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Transaction;
use crate::err::Error;
2022-01-16 20:31:50 +00:00
use crate::sql::error::IResult;
use crate::sql::escape::escape_rid;
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;
use crate::sql::value::Value;
2022-04-09 09:09:01 +00:00
use derive::Store;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::char;
use nom::combinator::map;
2022-10-16 20:35:50 +00:00
use nom::sequence::delimited;
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;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Deserialize, Store, Hash)]
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((tb, id): (String, Id)) -> Self {
Self {
tb,
id,
}
}
2020-06-29 15:36:01 +00:00
}
2022-03-07 18:11:44 +00:00
impl From<(String, String)> for Thing {
fn from((tb, id): (String, String)) -> Self {
Self::from((tb, Id::from(id)))
}
}
impl From<(&str, &str)> for Thing {
fn from((tb, id): (&str, &str)) -> Self {
Self::from((tb.to_owned(), Id::from(id)))
}
}
impl Thing {
2022-10-19 09:55:19 +00:00
/// Convert the Thing to a raw String
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 {
write!(f, "{}:{}", escape_rid(&self.tb), self.id)
2021-03-29 15:43:37 +00:00
}
}
impl Thing {
pub(crate) async fn compute(
&self,
ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
doc: Option<&Value>,
) -> Result<Value, Error> {
Ok(Value::Thing(Thing {
tb: self.tb.clone(),
id: self.id.compute(ctx, opt, txn, doc).await?,
}))
}
}
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 = self.to_string();
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-10-16 20:35:50 +00:00
alt((thing_raw, thing_single, thing_double))(i)
}
2022-10-16 20:35:50 +00:00
fn thing_single(i: &str) -> IResult<&str, Thing> {
delimited(char('\''), thing_raw, char('\''))(i)
}
2022-10-16 20:35:50 +00:00
fn thing_double(i: &str) -> IResult<&str, Thing> {
delimited(char('\"'), thing_raw, char('\"'))(i)
}
fn thing_raw(i: &str) -> IResult<&str, Thing> {
2020-06-29 15:36:01 +00:00
let (i, t) = ident_raw(i)?;
let (i, _) = char(':')(i)?;
let (i, v) = alt((
map(tag("rand()"), |_| Id::rand()),
map(tag("ulid()"), |_| Id::ulid()),
map(tag("uuid()"), |_| Id::uuid()),
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::*;
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 {
tb: String::from("test"),
id: Id::from("id"),
2020-06-29 15:36:01 +00:00
}
);
}
#[test]
fn thing_integer() {
let sql = "test:001";
let res = thing(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!("test:1", format!("{}", out));
assert_eq!(
out,
Thing {
tb: String::from("test"),
id: Id::from(1),
}
);
}
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
}
);
}
#[test]
fn thing_object() {
let sql = "test:{ location: 'GBR', year: 2022 }";
let res = thing(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
2022-10-19 14:48:50 +00:00
assert_eq!("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;
2022-10-19 14:48:50 +00:00
assert_eq!("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
}