Booleans now retain original value
This commit is contained in:
parent
c244975a0a
commit
19fc0ebaaf
2 changed files with 55 additions and 90 deletions
|
@ -8,7 +8,7 @@
|
||||||
//! additional methods for accessing leading comments or events by section.
|
//! additional methods for accessing leading comments or events by section.
|
||||||
|
|
||||||
use crate::values::{Boolean, TrueVariant, Value};
|
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::complete::{char, none_of, one_of};
|
||||||
use nom::character::{is_newline, is_space};
|
use nom::character::{is_newline, is_space};
|
||||||
use nom::combinator::{map, opt};
|
use nom::combinator::{map, opt};
|
||||||
|
|
143
src/values.rs
143
src/values.rs
|
@ -4,7 +4,7 @@ use serde::{Serialize, Serializer};
|
||||||
|
|
||||||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||||
pub enum Value<'a> {
|
pub enum Value<'a> {
|
||||||
Boolean(Boolean),
|
Boolean(Boolean<'a>),
|
||||||
Integer(Integer),
|
Integer(Integer),
|
||||||
Color(Color),
|
Color(Color),
|
||||||
Other(Cow<'a, str>),
|
Other(Cow<'a, str>),
|
||||||
|
@ -38,29 +38,13 @@ impl Serialize for Value<'_> {
|
||||||
// todo display for value
|
// todo display for value
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||||
pub enum Boolean {
|
pub enum Boolean<'a> {
|
||||||
True(TrueVariant),
|
True(TrueVariant<'a>),
|
||||||
False(FalseVariant),
|
False(FalseVariant<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: Display for boolean
|
impl<'a> Boolean<'a> {
|
||||||
|
pub fn from_str(value: &'a str) -> Result<Self, ()> {
|
||||||
impl Serialize for Boolean {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
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<Self, Self::Err> {
|
|
||||||
if let Ok(v) = TrueVariant::from_str(value) {
|
if let Ok(v) = TrueVariant::from_str(value) {
|
||||||
return Ok(Self::True(v));
|
return Ok(Self::True(v));
|
||||||
}
|
}
|
||||||
|
@ -73,12 +57,23 @@ impl FromStr for Boolean {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo: Display for boolean
|
||||||
|
|
||||||
|
impl Serialize for Boolean<'_> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
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)]
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||||
pub enum TrueVariant {
|
pub enum TrueVariant<'a> {
|
||||||
Yes,
|
Explicit(&'a str),
|
||||||
On,
|
|
||||||
True,
|
|
||||||
One,
|
|
||||||
/// For variables defined without a `= <value>`. This can never be created
|
/// For variables defined without a `= <value>`. This can never be created
|
||||||
/// from the [`FromStr`] trait, as an empty string is false without context.
|
/// from the [`FromStr`] trait, as an empty string is false without context.
|
||||||
/// If directly serializing this struct (instead of using a higher level
|
/// If directly serializing this struct (instead of using a higher level
|
||||||
|
@ -86,19 +81,30 @@ pub enum TrueVariant {
|
||||||
Implicit,
|
Implicit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for TrueVariant {
|
impl<'a> TrueVariant<'a> {
|
||||||
|
pub fn from_str(value: &'a str) -> Result<TrueVariant<'a>, ()> {
|
||||||
|
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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Yes => write!(f, "yes"),
|
Self::Explicit(v) => write!(f, "{}", v),
|
||||||
Self::On => write!(f, "on"),
|
|
||||||
Self::True => write!(f, "true"),
|
|
||||||
Self::One => write!(f, "one"),
|
|
||||||
Self::Implicit => write!(f, "(implicit)"),
|
Self::Implicit => write!(f, "(implicit)"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for TrueVariant {
|
impl Serialize for TrueVariant<'_> {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
|
@ -107,46 +113,31 @@ impl Serialize for TrueVariant {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for TrueVariant {
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||||
type Err = ();
|
pub struct FalseVariant<'a>(&'a str);
|
||||||
|
|
||||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
impl<'a> FalseVariant<'a> {
|
||||||
if value.eq_ignore_ascii_case("yes") {
|
pub fn from_str(value: &'a str) -> Result<FalseVariant<'a>, ()> {
|
||||||
Ok(Self::Yes)
|
if value.eq_ignore_ascii_case("no")
|
||||||
} else if value.eq_ignore_ascii_case("on") {
|
|| value.eq_ignore_ascii_case("off")
|
||||||
Ok(Self::On)
|
|| value.eq_ignore_ascii_case("false")
|
||||||
} else if value.eq_ignore_ascii_case("true") {
|
|| value.eq_ignore_ascii_case("zero")
|
||||||
Ok(Self::True)
|
|| value == "\"\""
|
||||||
} else if value.eq_ignore_ascii_case("one") {
|
{
|
||||||
Ok(Self::One)
|
Ok(Self(value))
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
Err(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
impl Display for FalseVariant<'_> {
|
||||||
pub enum FalseVariant {
|
|
||||||
No,
|
|
||||||
Off,
|
|
||||||
False,
|
|
||||||
Zero,
|
|
||||||
EmptyString,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for FalseVariant {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
write!(f, "{}", self.0)
|
||||||
Self::No => write!(f, "no"),
|
|
||||||
Self::Off => write!(f, "off"),
|
|
||||||
Self::False => write!(f, "false"),
|
|
||||||
Self::Zero => write!(f, "0"),
|
|
||||||
Self::EmptyString => write!(f, "\"\""),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for FalseVariant {
|
impl Serialize for FalseVariant<'_> {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
|
@ -155,26 +146,6 @@ impl Serialize for FalseVariant {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for FalseVariant {
|
|
||||||
type Err = ();
|
|
||||||
|
|
||||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
|
||||||
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)]
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||||
pub struct Integer {
|
pub struct Integer {
|
||||||
value: i64,
|
value: i64,
|
||||||
|
@ -297,13 +268,7 @@ impl Serialize for Color {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Color {
|
// impl fromstr for color
|
||||||
type Err = ();
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
|
||||||
enum ColorValue {
|
enum ColorValue {
|
||||||
|
|
Reference in a new issue