From 1f49b4ffe10781d4499193d92b1fdac6aeae5bf5 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:52:04 +1300 Subject: [PATCH 1/8] refactor: `format/toml.rs` Explicit `ValueKind` mapping instead of implicitly inferred `Value` type. Matches the `format/json5.rs` logic. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/toml.rs | 53 ++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index 19b78044..a92b02f9 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -2,44 +2,43 @@ use std::error::Error; use crate::format; use crate::map::Map; -use crate::value::Value; +use crate::value::{Value, ValueKind}; pub fn parse( uri: Option<&String>, text: &str, ) -> Result, Box> { - // Parse a TOML value from the provided text - let value = from_toml_value(uri, &toml::from_str(text)?); + // Parse a TOML input from the provided text + let value = from_toml_value(uri, toml::from_str(text)?); format::extract_root_table(uri, value) } -fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value { - match *value { - toml::Value::String(ref value) => Value::new(uri, value.to_string()), - toml::Value::Float(value) => Value::new(uri, value), - toml::Value::Integer(value) => Value::new(uri, value), - toml::Value::Boolean(value) => Value::new(uri, value), - - toml::Value::Table(ref table) => { - let mut m = Map::new(); - - for (key, value) in table { - m.insert(key.clone(), from_toml_value(uri, value)); - } - - Value::new(uri, m) +fn from_toml_value(uri: Option<&String>, value: toml::Value) -> Value { + let vk = match value { + toml::Value::Datetime(v) => ValueKind::String(v.to_string()), + toml::Value::String(v) => ValueKind::String(v), + toml::Value::Float(v) => ValueKind::Float(v), + toml::Value::Integer(v) => ValueKind::I64(v), + toml::Value::Boolean(v) => ValueKind::Boolean(v), + + toml::Value::Table(table) => { + let m = table + .into_iter() + .map(|(k, v)| (k, from_toml_value(uri, v))) + .collect(); + + ValueKind::Table(m) } - toml::Value::Array(ref array) => { - let mut l = Vec::new(); - - for value in array { - l.push(from_toml_value(uri, value)); - } + toml::Value::Array(array) => { + let l = array + .into_iter() + .map(|v| from_toml_value(uri, v)) + .collect(); - Value::new(uri, l) + ValueKind::Array(l) } + }; - toml::Value::Datetime(ref datetime) => Value::new(uri, datetime.to_string()), - } + Value::new(uri, vk) } From c9575a79d31b62288cd22a2d44232328d2b4e73f Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:11:25 +1300 Subject: [PATCH 2/8] refactor: Use a common `from_value` method DRY: The `json`, `json5` and `toml` parsers all leverage `serde` and can share a common enum to deserialize data into, instead of individual methods performing roughly the same transformations. - While `ron` improves their support for serde untagged enums with `v0.9`, it is still not compatible with this approach (_Their README details why_). - The `yaml` support doesn't leverage `serde`, thus is not compatible. `from_parsed_value()` is based on the approached used by `format/json5.rs`: - It has been adjusted to reflect the `ValueKind` enum, which could not directly be used due to the `Table` and `Array` types using `Value` as their value storage type instead of self-referencing the enum. - Very similar to a `impl From`, but supports the complimentary `uri` parameter for each `Value` derived. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/json.rs | 46 +++--------------------------------- src/file/format/json5.rs | 46 +++--------------------------------- src/file/format/toml.rs | 34 ++------------------------- src/format.rs | 50 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 118 deletions(-) diff --git a/src/file/format/json.rs b/src/file/format/json.rs index bd506f0d..025219a0 100644 --- a/src/file/format/json.rs +++ b/src/file/format/json.rs @@ -2,53 +2,13 @@ use std::error::Error; use crate::format; use crate::map::Map; -use crate::value::{Value, ValueKind}; +use crate::value::Value; pub fn parse( uri: Option<&String>, text: &str, ) -> Result, Box> { - // Parse a JSON object value from the text - let value = from_json_value(uri, &serde_json::from_str(text)?); + // Parse a JSON input from the provided text + let value = format::from_parsed_value(uri, serde_json::from_str(text)?); format::extract_root_table(uri, value) } - -fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value { - match *value { - serde_json::Value::String(ref value) => Value::new(uri, ValueKind::String(value.clone())), - - serde_json::Value::Number(ref value) => { - if let Some(value) = value.as_i64() { - Value::new(uri, ValueKind::I64(value)) - } else if let Some(value) = value.as_f64() { - Value::new(uri, ValueKind::Float(value)) - } else { - unreachable!(); - } - } - - serde_json::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), - - serde_json::Value::Object(ref table) => { - let mut m = Map::new(); - - for (key, value) in table { - m.insert(key.clone(), from_json_value(uri, value)); - } - - Value::new(uri, ValueKind::Table(m)) - } - - serde_json::Value::Array(ref array) => { - let mut l = Vec::new(); - - for value in array { - l.push(from_json_value(uri, value)); - } - - Value::new(uri, ValueKind::Array(l)) - } - - serde_json::Value::Null => Value::new(uri, ValueKind::Nil), - } -} diff --git a/src/file/format/json5.rs b/src/file/format/json5.rs index 99003bd0..242a4725 100644 --- a/src/file/format/json5.rs +++ b/src/file/format/json5.rs @@ -2,53 +2,13 @@ use std::error::Error; use crate::format; use crate::map::Map; -use crate::value::{Value, ValueKind}; - -#[derive(serde::Deserialize, Debug)] -#[serde(untagged)] -pub enum Val { - Null, - Boolean(bool), - Integer(i64), - Float(f64), - String(String), - Array(Vec), - Object(Map), -} +use crate::value::Value; pub fn parse( uri: Option<&String>, text: &str, ) -> Result, Box> { - let value = from_json5_value(uri, json5_rs::from_str::(text)?); + // Parse a JSON5 input from the provided text + let value = format::from_parsed_value(uri, json5_rs::from_str(text)?); format::extract_root_table(uri, value) } - -fn from_json5_value(uri: Option<&String>, value: Val) -> Value { - let vk = match value { - Val::Null => ValueKind::Nil, - Val::String(v) => ValueKind::String(v), - Val::Integer(v) => ValueKind::I64(v), - Val::Float(v) => ValueKind::Float(v), - Val::Boolean(v) => ValueKind::Boolean(v), - Val::Object(table) => { - let m = table - .into_iter() - .map(|(k, v)| (k, from_json5_value(uri, v))) - .collect(); - - ValueKind::Table(m) - } - - Val::Array(array) => { - let l = array - .into_iter() - .map(|v| from_json5_value(uri, v)) - .collect(); - - ValueKind::Array(l) - } - }; - - Value::new(uri, vk) -} diff --git a/src/file/format/toml.rs b/src/file/format/toml.rs index a92b02f9..55931698 100644 --- a/src/file/format/toml.rs +++ b/src/file/format/toml.rs @@ -2,43 +2,13 @@ use std::error::Error; use crate::format; use crate::map::Map; -use crate::value::{Value, ValueKind}; +use crate::value::Value; pub fn parse( uri: Option<&String>, text: &str, ) -> Result, Box> { // Parse a TOML input from the provided text - let value = from_toml_value(uri, toml::from_str(text)?); + let value = format::from_parsed_value(uri, toml::from_str(text)?); format::extract_root_table(uri, value) } - -fn from_toml_value(uri: Option<&String>, value: toml::Value) -> Value { - let vk = match value { - toml::Value::Datetime(v) => ValueKind::String(v.to_string()), - toml::Value::String(v) => ValueKind::String(v), - toml::Value::Float(v) => ValueKind::Float(v), - toml::Value::Integer(v) => ValueKind::I64(v), - toml::Value::Boolean(v) => ValueKind::Boolean(v), - - toml::Value::Table(table) => { - let m = table - .into_iter() - .map(|(k, v)| (k, from_toml_value(uri, v))) - .collect(); - - ValueKind::Table(m) - } - - toml::Value::Array(array) => { - let l = array - .into_iter() - .map(|v| from_toml_value(uri, v)) - .collect(); - - ValueKind::Array(l) - } - }; - - Value::new(uri, vk) -} diff --git a/src/format.rs b/src/format.rs index 3d1ca335..1556645b 100644 --- a/src/format.rs +++ b/src/format.rs @@ -44,3 +44,53 @@ pub fn extract_root_table( .map_err(|err| ConfigError::invalid_root(uri, err)) .map_err(|err| Box::new(err) as Box) } + +// Equivalent to ValueKind, except Table + Array store the same enum +// Useful for serde to serialize values into, then convert to Value +#[derive(serde::Deserialize, Debug)] +#[serde(untagged)] +pub enum ParsedValue { + Nil, + Boolean(bool), + I64(i64), + I128(i128), + U64(u64), + U128(u128), + Float(f64), + String(String), + Table(Map), + Array(Vec), +} + +// Value wrap ValueKind values, with optional uri (origin) +pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value { + let vk = match value { + ParsedValue::Nil => ValueKind::Nil, + ParsedValue::String(v) => ValueKind::String(v), + ParsedValue::I64(v) => ValueKind::I64(v), + ParsedValue::I128(v) => ValueKind::I128(v), + ParsedValue::U64(v) => ValueKind::U64(v), + ParsedValue::U128(v) => ValueKind::U128(v), + ParsedValue::Float(v) => ValueKind::Float(v), + ParsedValue::Boolean(v) => ValueKind::Boolean(v), + ParsedValue::Table(table) => { + let m = table + .into_iter() + .map(|(k, v)| (k, from_parsed_value(uri, v))) + .collect(); + + ValueKind::Table(m) + } + + ParsedValue::Array(array) => { + let l = array + .into_iter() + .map(|v| from_parsed_value(uri, v)) + .collect(); + + ValueKind::Array(l) + } + }; + + Value::new(uri, vk) +} From 6f63595aa4dd07fbd1bd8ac29c68fedd0c8aa68f Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:02:21 +1300 Subject: [PATCH 3/8] fix: Support TOML `Datetime` value - The enum did not properly handle the `Datetime` TOML value which needed to be converted into a `String` type. - This workaround approach avoids duplicating `from_parsed_value()` logic to support one enum variant. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/format.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/format.rs b/src/format.rs index 1556645b..fb0a2fca 100644 --- a/src/format.rs +++ b/src/format.rs @@ -57,11 +57,40 @@ pub enum ParsedValue { U64(u64), U128(u128), Float(f64), + #[serde(deserialize_with = "deserialize_parsed_string")] String(String), Table(Map), Array(Vec), } +// Deserialization support for TOML `Datetime` value type into `String` +#[derive(serde::Deserialize, Debug)] +#[serde(untagged)] +enum ParsedString { + String(String), + #[cfg(feature = "toml")] + DateTime(toml::value::Datetime), +} + +fn deserialize_parsed_string<'de, D>(deserializer: D) -> Result +where + D: serde::de::Deserializer<'de>, +{ + let s: ParsedString = serde::Deserialize::deserialize(deserializer)?; + Ok(s.to_string()) +} + +impl std::fmt::Display for ParsedString { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let s = match self { + ParsedString::String(s) => s.to_string(), + #[cfg(feature = "toml")] + ParsedString::DateTime(dt) => dt.to_string() + }; + write!(f, "{}", s) + } +} + // Value wrap ValueKind values, with optional uri (origin) pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value { let vk = match value { From a4f8c86853f0d401aabad66a621feb895b0f947d Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:01:55 +1300 Subject: [PATCH 4/8] refactor: Simplify `ParsedString` deserializer The enum is only intended as a helper for this deserializer into `String` type, it can be bundled inside. Likewise, no need to `impl Display`. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/format.rs | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/format.rs b/src/format.rs index fb0a2fca..63cd7cf1 100644 --- a/src/format.rs +++ b/src/format.rs @@ -3,6 +3,7 @@ use std::error::Error; use crate::error::{ConfigError, Unexpected}; use crate::map::Map; use crate::value::{Value, ValueKind}; +use serde::Deserialize; /// Describes a format of configuration source data /// @@ -63,34 +64,6 @@ pub enum ParsedValue { Array(Vec), } -// Deserialization support for TOML `Datetime` value type into `String` -#[derive(serde::Deserialize, Debug)] -#[serde(untagged)] -enum ParsedString { - String(String), - #[cfg(feature = "toml")] - DateTime(toml::value::Datetime), -} - -fn deserialize_parsed_string<'de, D>(deserializer: D) -> Result -where - D: serde::de::Deserializer<'de>, -{ - let s: ParsedString = serde::Deserialize::deserialize(deserializer)?; - Ok(s.to_string()) -} - -impl std::fmt::Display for ParsedString { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let s = match self { - ParsedString::String(s) => s.to_string(), - #[cfg(feature = "toml")] - ParsedString::DateTime(dt) => dt.to_string() - }; - write!(f, "{}", s) - } -} - // Value wrap ValueKind values, with optional uri (origin) pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value { let vk = match value { @@ -123,3 +96,25 @@ pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value { Value::new(uri, vk) } + +// Deserialization support for TOML `Datetime` value type into `String` +fn deserialize_parsed_string<'de, D>(deserializer: D) -> Result +where + D: serde::de::Deserializer<'de>, +{ + #[derive(serde::Deserialize)] + #[serde(untagged)] + enum ParsedString { + // Anything that can deserialize into a string successfully: + String(String), + // Config specific support for types that need string conversion: + #[cfg(feature = "toml")] + TomlDateTime(toml::value::Datetime), + } + + Ok(match ParsedString::deserialize(deserializer)? { + ParsedString::String(v) => v, + #[cfg(feature = "toml")] + ParsedString::TomlDateTime(v) => v.to_string(), + }) +} From fe9ad9a7b90eb9dcb85891b80295b608443bdf99 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:14:26 +1300 Subject: [PATCH 5/8] chore: Have `ParsedValue` deserialize to `Nil` as fallback If no other variant could be deserialized into successfully, the type is not supported and treated as the `Nil` type. This better communicates failures within tests when a type is compared and is not expected to be `Nil`. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- Cargo.toml | 1 + src/format.rs | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8b08f5be..0277d2ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ async = ["async-trait"] [dependencies] lazy_static = "1.0" serde = "1.0.8" +serde_with = "3" nom = "7" async-trait = { version = "0.1.50", optional = true } diff --git a/src/format.rs b/src/format.rs index 63cd7cf1..a691ec92 100644 --- a/src/format.rs +++ b/src/format.rs @@ -4,6 +4,7 @@ use crate::error::{ConfigError, Unexpected}; use crate::map::Map; use crate::value::{Value, ValueKind}; use serde::Deserialize; +use serde_with::rust::deserialize_ignore_any; /// Describes a format of configuration source data /// @@ -47,11 +48,12 @@ pub fn extract_root_table( } // Equivalent to ValueKind, except Table + Array store the same enum -// Useful for serde to serialize values into, then convert to Value +// Useful for serde to serialize values into, then convert to Value. +// NOTE: Order of variants is important. Serde will use whichever +// the input successfully deserializes into first. #[derive(serde::Deserialize, Debug)] #[serde(untagged)] pub enum ParsedValue { - Nil, Boolean(bool), I64(i64), I128(i128), @@ -62,6 +64,9 @@ pub enum ParsedValue { String(String), Table(Map), Array(Vec), + // If nothing else above matched, use Nil: + #[serde(deserialize_with = "deserialize_ignore_any")] + Nil, } // Value wrap ValueKind values, with optional uri (origin) From e597f6aaef1205d508480735bf887bb8301408d9 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Wed, 11 Oct 2023 01:31:19 +1300 Subject: [PATCH 6/8] fix: Support Ron values - `Char` variant needed to be converted to `String`. - `Option` variant could be introduced generically. NOTE: With `v0.9` of Ron, more types are introduced. Without tests, if these do not deserialize into a supported type they will be treated as `Nil` type. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/ron.rs | 54 ++---------------------------------------- src/format.rs | 26 ++++++++++++++++---- 2 files changed, 23 insertions(+), 57 deletions(-) diff --git a/src/file/format/ron.rs b/src/file/format/ron.rs index 9ac81a9d..67b19f57 100644 --- a/src/file/format/ron.rs +++ b/src/file/format/ron.rs @@ -2,62 +2,12 @@ use std::error::Error; use crate::format; use crate::map::Map; -use crate::value::{Value, ValueKind}; +use crate::value::Value; pub fn parse( uri: Option<&String>, text: &str, ) -> Result, Box> { - let value = from_ron_value(uri, ron::from_str(text)?)?; + let value = format::from_parsed_value(uri, ron::from_str(text)?); format::extract_root_table(uri, value) } - -fn from_ron_value( - uri: Option<&String>, - value: ron::Value, -) -> Result> { - let kind = match value { - ron::Value::Option(value) => match value { - Some(value) => from_ron_value(uri, *value)?.kind, - None => ValueKind::Nil, - }, - - ron::Value::Unit => ValueKind::Nil, - - ron::Value::Bool(value) => ValueKind::Boolean(value), - - ron::Value::Number(value) => match value { - ron::Number::Float(value) => ValueKind::Float(value.get()), - ron::Number::Integer(value) => ValueKind::I64(value), - }, - - ron::Value::Char(value) => ValueKind::String(value.to_string()), - - ron::Value::String(value) => ValueKind::String(value), - - ron::Value::Seq(values) => { - let array = values - .into_iter() - .map(|value| from_ron_value(uri, value)) - .collect::, _>>()?; - - ValueKind::Array(array) - } - - ron::Value::Map(values) => { - let map = values - .iter() - .map(|(key, value)| -> Result<_, Box> { - let key = key.clone().into_rust::()?; - let value = from_ron_value(uri, value.clone())?; - - Ok((key, value)) - }) - .collect::, _>>()?; - - ValueKind::Table(map) - } - }; - - Ok(Value::new(uri, kind)) -} diff --git a/src/format.rs b/src/format.rs index a691ec92..cdf3c81a 100644 --- a/src/format.rs +++ b/src/format.rs @@ -64,6 +64,7 @@ pub enum ParsedValue { String(String), Table(Map), Array(Vec), + Option(Option>), // If nothing else above matched, use Nil: #[serde(deserialize_with = "deserialize_ignore_any")] Nil, @@ -80,6 +81,7 @@ pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value { ParsedValue::U128(v) => ValueKind::U128(v), ParsedValue::Float(v) => ValueKind::Float(v), ParsedValue::Boolean(v) => ValueKind::Boolean(v), + ParsedValue::Table(table) => { let m = table .into_iter() @@ -97,12 +99,17 @@ pub fn from_parsed_value(uri: Option<&String>, value: ParsedValue) -> Value { ValueKind::Array(l) } + + // Boxed value must be dereferenced: + ParsedValue::Option(v) => match v { + Some(boxed) => from_parsed_value(uri, *boxed).kind, + None => ValueKind::Nil, + }, }; Value::new(uri, vk) } -// Deserialization support for TOML `Datetime` value type into `String` fn deserialize_parsed_string<'de, D>(deserializer: D) -> Result where D: serde::de::Deserializer<'de>, @@ -115,11 +122,20 @@ where // Config specific support for types that need string conversion: #[cfg(feature = "toml")] TomlDateTime(toml::value::Datetime), + #[cfg(feature = "ron")] + RonChar(ron::Value), } - Ok(match ParsedString::deserialize(deserializer)? { - ParsedString::String(v) => v, + match ParsedString::deserialize(deserializer)? { + ParsedString::String(v) => Ok(v), #[cfg(feature = "toml")] - ParsedString::TomlDateTime(v) => v.to_string(), - }) + ParsedString::TomlDateTime(v) => Ok(v.to_string()), + #[cfg(feature = "ron")] + ParsedString::RonChar(variant) => match variant { + ron::Value::Char(v) => Ok(v.to_string()), + _ => Err(serde::de::Error::custom( + "should not be serialized to string", + )), + }, + } } From d4f2f354fc0a78eaf7b77e5ad528d7c11b475961 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:53:48 +1300 Subject: [PATCH 7/8] fix: Support Yaml values - Required switching to `serde_yaml`. - Like with `yaml-rust`, requires special handling for table keys. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- Cargo.toml | 4 +- README.md | 2 +- src/file/format/mod.rs | 2 +- src/file/format/yaml.rs | 97 ++------------------------------------- src/format.rs | 40 ++++++++++++++++ tests/file_yaml.rs | 5 +- tests/legacy/file_yaml.rs | 5 +- 7 files changed, 53 insertions(+), 102 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0277d2ab..0cc0b0b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ maintenance = { status = "actively-developed" } [features] default = ["toml", "json", "yaml", "ini", "ron", "json5", "convert-case", "async"] json = ["serde_json"] -yaml = ["yaml-rust"] +yaml = ["serde_yaml"] ini = ["rust-ini"] json5 = ["json5_rs", "serde/derive"] convert-case = ["convert_case"] @@ -33,7 +33,7 @@ nom = "7" async-trait = { version = "0.1.50", optional = true } toml = { version = "0.8", optional = true } serde_json = { version = "1.0.2", optional = true } -yaml-rust = { version = "0.4", optional = true } +serde_yaml = { version = "0.9", optional = true } rust-ini = { version = "0.19", optional = true } ron = { version = "0.8", optional = true } json5_rs = { version = "0.4", optional = true, package = "json5" } diff --git a/README.md b/README.md index badab891..3130ef69 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ [JSON]: https://github.com/serde-rs/json [TOML]: https://github.com/toml-lang/toml -[YAML]: https://github.com/chyh1990/yaml-rust +[YAML]: https://github.com/dtolnay/serde-yaml [INI]: https://github.com/zonyitoo/rust-ini [RON]: https://github.com/ron-rs/ron [JSON5]: https://github.com/callum-oakley/json5-rs diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 025e98a9..b292f68c 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -40,7 +40,7 @@ pub enum FileFormat { #[cfg(feature = "json")] Json, - /// YAML (parsed with yaml_rust) + /// YAML (parsed with serde_yaml) #[cfg(feature = "yaml")] Yaml, diff --git a/src/file/format/yaml.rs b/src/file/format/yaml.rs index b35ba4d9..e04c5dfd 100644 --- a/src/file/format/yaml.rs +++ b/src/file/format/yaml.rs @@ -1,105 +1,14 @@ use std::error::Error; -use std::fmt; -use std::mem; - -use yaml_rust as yaml; use crate::format; use crate::map::Map; -use crate::value::{Value, ValueKind}; +use crate::value::Value; pub fn parse( uri: Option<&String>, text: &str, ) -> Result, Box> { - // Parse a YAML object from file - let mut docs = yaml::YamlLoader::load_from_str(text)?; - let root = match docs.len() { - 0 => yaml::Yaml::Hash(yaml::yaml::Hash::new()), - 1 => mem::replace(&mut docs[0], yaml::Yaml::Null), - n => { - return Err(Box::new(MultipleDocumentsError(n))); - } - }; - - let value = from_yaml_value(uri, &root)?; + // Parse a YAML input from the provided text + let value = format::from_parsed_value(uri, serde_yaml::from_str(text)?); format::extract_root_table(uri, value) } - -fn from_yaml_value( - uri: Option<&String>, - value: &yaml::Yaml, -) -> Result> { - match *value { - yaml::Yaml::String(ref value) => Ok(Value::new(uri, ValueKind::String(value.clone()))), - yaml::Yaml::Real(ref value) => { - // TODO: Figure out in what cases this can panic? - value - .parse::() - .map_err(|_| { - Box::new(FloatParsingError(value.to_string())) as Box<(dyn Error + Send + Sync)> - }) - .map(ValueKind::Float) - .map(|f| Value::new(uri, f)) - } - yaml::Yaml::Integer(value) => Ok(Value::new(uri, ValueKind::I64(value))), - yaml::Yaml::Boolean(value) => Ok(Value::new(uri, ValueKind::Boolean(value))), - yaml::Yaml::Hash(ref table) => { - let mut m = Map::new(); - for (key, value) in table { - match key { - yaml::Yaml::String(k) => m.insert(k.to_owned(), from_yaml_value(uri, value)?), - yaml::Yaml::Integer(k) => m.insert(k.to_string(), from_yaml_value(uri, value)?), - _ => unreachable!(), - }; - } - Ok(Value::new(uri, ValueKind::Table(m))) - } - yaml::Yaml::Array(ref array) => { - let mut l = Vec::new(); - - for value in array { - l.push(from_yaml_value(uri, value)?); - } - - Ok(Value::new(uri, ValueKind::Array(l))) - } - - // 1. Yaml NULL - // 2. BadValue – It shouldn't be possible to hit BadValue as this only happens when - // using the index trait badly or on a type error but we send back nil. - // 3. Alias – No idea what to do with this and there is a note in the lib that its - // not fully supported yet anyway - _ => Ok(Value::new(uri, ValueKind::Nil)), - } -} - -#[derive(Debug, Copy, Clone)] -struct MultipleDocumentsError(usize); - -impl fmt::Display for MultipleDocumentsError { - fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result { - write!(format, "Got {} YAML documents, expected 1", self.0) - } -} - -impl Error for MultipleDocumentsError { - fn description(&self) -> &str { - "More than one YAML document provided" - } -} - -#[derive(Debug, Clone)] -struct FloatParsingError(String); - -impl fmt::Display for FloatParsingError { - fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result { - write!(format, "Parsing {} as floating point number failed", self.0) - } -} - -impl Error for FloatParsingError { - fn description(&self) -> &str { - "Floating point number parsing failed" - } -} diff --git a/src/format.rs b/src/format.rs index cdf3c81a..312f6c45 100644 --- a/src/format.rs +++ b/src/format.rs @@ -62,6 +62,7 @@ pub enum ParsedValue { Float(f64), #[serde(deserialize_with = "deserialize_parsed_string")] String(String), + #[serde(deserialize_with = "deserialize_parsed_map")] Table(Map), Array(Vec), Option(Option>), @@ -139,3 +140,42 @@ where }, } } + +fn deserialize_parsed_map<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::de::Deserializer<'de>, +{ + #[derive(serde::Deserialize)] + #[serde(untagged)] + enum ParsedMap { + // Anything that can deserialize into a Map successfully: + Table(Map), + // Config specific support for types that need string conversion: + #[cfg(feature = "yaml")] + YamlMap(serde_yaml::Mapping), + } + + match ParsedMap::deserialize(deserializer)? { + ParsedMap::Table(v) => Ok(v), + #[cfg(feature = "yaml")] + ParsedMap::YamlMap(table) => { + table + .into_iter() + .map(|(key, value)| { + let key = match key { + serde_yaml::Value::Number(k) => Some(k.to_string()), + serde_yaml::Value::String(k) => Some(k), + _ => None, + }; + let value = serde_yaml::from_value::(value).ok(); + + // Option to Result: + match (key, value) { + (Some(k), Some(v)) => Ok((k, v)), + _ => Err(serde::de::Error::custom("should not be serialized to Map")), + } + }) + .collect() + } + } +} diff --git a/tests/file_yaml.rs b/tests/file_yaml.rs index b961c2a6..a506614f 100644 --- a/tests/file_yaml.rs +++ b/tests/file_yaml.rs @@ -81,12 +81,13 @@ fn test_error_parse() { let path_with_extension: PathBuf = ["tests", "Settings-invalid.yaml"].iter().collect(); + // Should fail to parse block mapping as no `:` exists to identify a key assert!(res.is_err()); assert_eq!( res.unwrap_err().to_string(), format!( - "while parsing a block mapping, did not find expected key at \ - line 2 column 1 in {}", + "could not find expected ':' at line 3 column 1, \ + while scanning a simple key at line 2 column 1 in {}", path_with_extension.display() ) ); diff --git a/tests/legacy/file_yaml.rs b/tests/legacy/file_yaml.rs index 21d41384..844a7aa0 100644 --- a/tests/legacy/file_yaml.rs +++ b/tests/legacy/file_yaml.rs @@ -81,12 +81,13 @@ fn test_error_parse() { let path_with_extension: PathBuf = ["tests", "Settings-invalid.yaml"].iter().collect(); + // Should fail to parse block mapping as no `:` exists to identify a key assert!(res.is_err()); assert_eq!( res.unwrap_err().to_string(), format!( - "while parsing a block mapping, did not find expected key at \ - line 2 column 1 in {}", + "could not find expected ':' at line 3 column 1, \ + while scanning a simple key at line 2 column 1 in {}", path_with_extension.display() ) ); From ccf35f3d3dd791d2f8aafb93ccf9760bef98101f Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Tue, 17 Oct 2023 18:10:38 +1300 Subject: [PATCH 8/8] chore: `ParsedString` should use a generic `char` variant Any format that supports a `char` type would now be matched for conversion to the expected `String` type. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/format.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/format.rs b/src/format.rs index 312f6c45..3c0b4757 100644 --- a/src/format.rs +++ b/src/format.rs @@ -121,23 +121,16 @@ where // Anything that can deserialize into a string successfully: String(String), // Config specific support for types that need string conversion: + Char(char), #[cfg(feature = "toml")] TomlDateTime(toml::value::Datetime), - #[cfg(feature = "ron")] - RonChar(ron::Value), } match ParsedString::deserialize(deserializer)? { ParsedString::String(v) => Ok(v), + ParsedString::Char(v) => Ok(v.to_string()), #[cfg(feature = "toml")] ParsedString::TomlDateTime(v) => Ok(v.to_string()), - #[cfg(feature = "ron")] - ParsedString::RonChar(variant) => match variant { - ron::Value::Char(v) => Ok(v.to_string()), - _ => Err(serde::de::Error::custom( - "should not be serialized to string", - )), - }, } }