surrealpatch/src/sql/strand.rs

69 lines
1.4 KiB
Rust
Raw Normal View History

2020-06-29 15:36:01 +00:00
use nom::branch::alt;
use nom::bytes::complete::is_not;
use nom::bytes::complete::tag;
use nom::sequence::delimited;
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;
use std::str;
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 Strand {
pub value: String,
}
2021-03-29 15:43:37 +00:00
impl From<String> for Strand {
fn from(s: String) -> Self {
Strand {
value: s,
}
}
}
2020-06-29 15:36:01 +00:00
impl<'a> From<&'a str> for Strand {
fn from(s: &str) -> Self {
Strand {
value: String::from(s),
}
}
}
impl fmt::Display for Strand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\"{}\"", self.value)
}
}
2021-03-29 15:43:37 +00:00
impl Serialize for Strand {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if serializer.is_human_readable() {
serializer.serialize_some(&self.value)
} else {
let mut val = serializer.serialize_struct("Strand", 1)?;
val.serialize_field("value", &self.value)?;
val.end()
}
}
}
2020-06-29 15:36:01 +00:00
pub fn strand(i: &str) -> IResult<&str, Strand> {
let (i, v) = strand_raw(i)?;
Ok((i, Strand::from(v)))
}
pub fn strand_raw(i: &str) -> IResult<&str, &str> {
alt((strand_single, strand_double))(i)
}
fn strand_single(i: &str) -> IResult<&str, &str> {
delimited(tag("\'"), is_not("\'"), tag("\'"))(i)
}
fn strand_double(i: &str) -> IResult<&str, &str> {
delimited(tag("\""), is_not("\""), tag("\""))(i)
}