Simplify parsing of string based types
This commit is contained in:
parent
1f6847be85
commit
e6eb6168a1
3 changed files with 21 additions and 35 deletions
|
@ -14,9 +14,6 @@ use std::ops;
|
|||
use std::ops::Deref;
|
||||
use std::str;
|
||||
|
||||
const SINGLE: char = '\'';
|
||||
const DOUBLE: char = '"';
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Deserialize)]
|
||||
pub struct Datetime(pub DateTime<Utc>);
|
||||
|
||||
|
@ -91,8 +88,8 @@ impl ops::Sub<Self> for Datetime {
|
|||
|
||||
pub fn datetime(i: &str) -> IResult<&str, Datetime> {
|
||||
alt((
|
||||
delimited(char(DOUBLE), datetime_raw, char(DOUBLE)),
|
||||
delimited(char(SINGLE), datetime_raw, char(SINGLE)),
|
||||
delimited(char('\''), datetime_raw, char('\'')),
|
||||
delimited(char('\"'), datetime_raw, char('\"')),
|
||||
))(i)
|
||||
}
|
||||
|
||||
|
|
|
@ -11,13 +11,11 @@ use crate::sql::value::Value;
|
|||
use derive::Store;
|
||||
use nom::branch::alt;
|
||||
use nom::character::complete::char;
|
||||
use nom::sequence::delimited;
|
||||
use serde::ser::SerializeStruct;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
const SINGLE: char = '\'';
|
||||
const DOUBLE: char = '"';
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Deserialize, Store)]
|
||||
pub struct Thing {
|
||||
pub tb: String,
|
||||
|
@ -101,15 +99,18 @@ impl Serialize for Thing {
|
|||
}
|
||||
|
||||
pub fn thing(i: &str) -> IResult<&str, Thing> {
|
||||
let (i, v) = thing_raw(i)?;
|
||||
Ok((i, v))
|
||||
alt((thing_raw, thing_single, thing_double))(i)
|
||||
}
|
||||
|
||||
fn thing_single(i: &str) -> IResult<&str, Thing> {
|
||||
delimited(char('\''), thing_raw, char('\''))(i)
|
||||
}
|
||||
|
||||
fn thing_double(i: &str) -> IResult<&str, Thing> {
|
||||
delimited(char('\"'), thing_raw, char('\"'))(i)
|
||||
}
|
||||
|
||||
fn thing_raw(i: &str) -> IResult<&str, Thing> {
|
||||
alt((thing_normal, thing_single, thing_double))(i)
|
||||
}
|
||||
|
||||
fn thing_normal(i: &str) -> IResult<&str, Thing> {
|
||||
let (i, t) = ident_raw(i)?;
|
||||
let (i, _) = char(':')(i)?;
|
||||
let (i, v) = id(i)?;
|
||||
|
@ -122,20 +123,6 @@ fn thing_normal(i: &str) -> IResult<&str, Thing> {
|
|||
))
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
|
|
|
@ -12,9 +12,6 @@ use std::fmt;
|
|||
use std::ops::Deref;
|
||||
use std::str;
|
||||
|
||||
const SINGLE: char = '\'';
|
||||
const DOUBLE: char = '"';
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
|
||||
pub struct Uuid(pub uuid::Uuid);
|
||||
|
||||
|
@ -66,10 +63,15 @@ impl Serialize for Uuid {
|
|||
}
|
||||
|
||||
pub fn uuid(i: &str) -> IResult<&str, Uuid> {
|
||||
alt((
|
||||
delimited(char(DOUBLE), uuid_raw, char(DOUBLE)),
|
||||
delimited(char(SINGLE), uuid_raw, char(SINGLE)),
|
||||
))(i)
|
||||
alt((uuid_single, uuid_double))(i)
|
||||
}
|
||||
|
||||
fn uuid_single(i: &str) -> IResult<&str, Uuid> {
|
||||
delimited(char('\''), uuid_raw, char('\''))(i)
|
||||
}
|
||||
|
||||
fn uuid_double(i: &str) -> IResult<&str, Uuid> {
|
||||
delimited(char('\"'), uuid_raw, char('\"'))(i)
|
||||
}
|
||||
|
||||
fn uuid_raw(i: &str) -> IResult<&str, Uuid> {
|
||||
|
|
Loading…
Reference in a new issue