From 19fc0ebaaf9990565c023d04ba6c0d8c7d05e3ec Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 19 Feb 2021 19:41:58 -0500 Subject: [PATCH] Booleans now retain original value --- src/parser.rs | 2 +- src/values.rs | 143 +++++++++++++++++++------------------------------- 2 files changed, 55 insertions(+), 90 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 07cfc55..064ac59 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,7 +8,7 @@ //! additional methods for accessing leading comments or events by section. use crate::values::{Boolean, TrueVariant, Value}; -use nom::bytes::complete::{escaped, tag, take_till, take_until, take_while}; +use nom::bytes::complete::{escaped, tag, take_till, take_while}; use nom::character::complete::{char, none_of, one_of}; use nom::character::{is_newline, is_space}; use nom::combinator::{map, opt}; diff --git a/src/values.rs b/src/values.rs index c7ada1c..686da89 100644 --- a/src/values.rs +++ b/src/values.rs @@ -4,7 +4,7 @@ use serde::{Serialize, Serializer}; #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub enum Value<'a> { - Boolean(Boolean), + Boolean(Boolean<'a>), Integer(Integer), Color(Color), Other(Cow<'a, str>), @@ -38,29 +38,13 @@ impl Serialize for Value<'_> { // todo display for value #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] -pub enum Boolean { - True(TrueVariant), - False(FalseVariant), +pub enum Boolean<'a> { + True(TrueVariant<'a>), + False(FalseVariant<'a>), } -// todo: Display for boolean - -impl Serialize for Boolean { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - match self { - Boolean::True(_) => serializer.serialize_bool(true), - Boolean::False(_) => serializer.serialize_bool(false), - } - } -} - -impl FromStr for Boolean { - type Err = (); - - fn from_str(value: &str) -> Result { +impl<'a> Boolean<'a> { + pub fn from_str(value: &'a str) -> Result { if let Ok(v) = TrueVariant::from_str(value) { return Ok(Self::True(v)); } @@ -73,12 +57,23 @@ impl FromStr for Boolean { } } +// todo: Display for boolean + +impl Serialize for Boolean<'_> { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + Boolean::True(_) => serializer.serialize_bool(true), + Boolean::False(_) => serializer.serialize_bool(false), + } + } +} + #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] -pub enum TrueVariant { - Yes, - On, - True, - One, +pub enum TrueVariant<'a> { + Explicit(&'a str), /// For variables defined without a `= `. This can never be created /// from the [`FromStr`] trait, as an empty string is false without context. /// If directly serializing this struct (instead of using a higher level @@ -86,19 +81,30 @@ pub enum TrueVariant { Implicit, } -impl Display for TrueVariant { +impl<'a> TrueVariant<'a> { + pub fn from_str(value: &'a str) -> Result, ()> { + if value.eq_ignore_ascii_case("yes") + || value.eq_ignore_ascii_case("on") + || value.eq_ignore_ascii_case("true") + || value.eq_ignore_ascii_case("one") + { + Ok(Self::Explicit(value)) + } else { + Err(()) + } + } +} + +impl Display for TrueVariant<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::Yes => write!(f, "yes"), - Self::On => write!(f, "on"), - Self::True => write!(f, "true"), - Self::One => write!(f, "one"), + Self::Explicit(v) => write!(f, "{}", v), Self::Implicit => write!(f, "(implicit)"), } } } -impl Serialize for TrueVariant { +impl Serialize for TrueVariant<'_> { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, @@ -107,46 +113,31 @@ impl Serialize for TrueVariant { } } -impl FromStr for TrueVariant { - type Err = (); +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] +pub struct FalseVariant<'a>(&'a str); - fn from_str(value: &str) -> Result { - if value.eq_ignore_ascii_case("yes") { - Ok(Self::Yes) - } else if value.eq_ignore_ascii_case("on") { - Ok(Self::On) - } else if value.eq_ignore_ascii_case("true") { - Ok(Self::True) - } else if value.eq_ignore_ascii_case("one") { - Ok(Self::One) +impl<'a> FalseVariant<'a> { + pub fn from_str(value: &'a str) -> Result, ()> { + if value.eq_ignore_ascii_case("no") + || value.eq_ignore_ascii_case("off") + || value.eq_ignore_ascii_case("false") + || value.eq_ignore_ascii_case("zero") + || value == "\"\"" + { + Ok(Self(value)) } else { Err(()) } } } -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] -pub enum FalseVariant { - No, - Off, - False, - Zero, - EmptyString, -} - -impl Display for FalseVariant { +impl Display for FalseVariant<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::No => write!(f, "no"), - Self::Off => write!(f, "off"), - Self::False => write!(f, "false"), - Self::Zero => write!(f, "0"), - Self::EmptyString => write!(f, "\"\""), - } + write!(f, "{}", self.0) } } -impl Serialize for FalseVariant { +impl Serialize for FalseVariant<'_> { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, @@ -155,26 +146,6 @@ impl Serialize for FalseVariant { } } -impl FromStr for FalseVariant { - type Err = (); - - fn from_str(value: &str) -> Result { - if value.eq_ignore_ascii_case("no") { - Ok(Self::No) - } else if value.eq_ignore_ascii_case("off") { - Ok(Self::Off) - } else if value.eq_ignore_ascii_case("false") { - Ok(Self::False) - } else if value.eq_ignore_ascii_case("zero") { - Ok(Self::Zero) - } else if value.is_empty() { - Ok(Self::EmptyString) - } else { - Err(()) - } - } -} - #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub struct Integer { value: i64, @@ -297,13 +268,7 @@ impl Serialize for Color { } } -impl FromStr for Color { - type Err = (); - - fn from_str(s: &str) -> Result { - todo!() - } -} +// impl fromstr for color #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] enum ColorValue {