From e6eb6168a1e53185679862917e879b180df29e91 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 16 Oct 2022 21:35:50 +0100 Subject: [PATCH] Simplify parsing of string based types --- lib/src/sql/datetime.rs | 7 ++----- lib/src/sql/thing.rs | 33 ++++++++++----------------------- lib/src/sql/uuid.rs | 16 +++++++++------- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/lib/src/sql/datetime.rs b/lib/src/sql/datetime.rs index 69ba6c8d..535c14ab 100644 --- a/lib/src/sql/datetime.rs +++ b/lib/src/sql/datetime.rs @@ -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); @@ -91,8 +88,8 @@ impl ops::Sub 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) } diff --git a/lib/src/sql/thing.rs b/lib/src/sql/thing.rs index 7fdcae7f..88690fd3 100644 --- a/lib/src/sql/thing.rs +++ b/lib/src/sql/thing.rs @@ -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 { diff --git a/lib/src/sql/uuid.rs b/lib/src/sql/uuid.rs index ce9beca5..bb260867 100644 --- a/lib/src/sql/uuid.rs +++ b/lib/src/sql/uuid.rs @@ -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> {