From d03821e44b8e8ec5733d9680c9d72f897f73347b Mon Sep 17 00:00:00 2001 From: Mikhail Volkov Date: Sat, 29 Jul 2023 08:08:03 +0300 Subject: [PATCH 1/8] feat: Added Gura support Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- Cargo.toml | 4 +- src/file/format/gura.rs | 57 ++++++++++ src/file/format/mod.rs | 13 +++ tests/Settings-enum-test.ura | 3 + tests/Settings.ura | 20 ++++ tests/file_gura.rs | 199 +++++++++++++++++++++++++++++++++++ 6 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 src/file/format/gura.rs create mode 100644 tests/Settings-enum-test.ura create mode 100644 tests/Settings.ura create mode 100644 tests/file_gura.rs diff --git a/Cargo.toml b/Cargo.toml index 8b08f5be..51f60d4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,12 @@ edition = "2018" maintenance = { status = "actively-developed" } [features] -default = ["toml", "json", "yaml", "ini", "ron", "json5", "convert-case", "async"] +default = ["toml", "json", "yaml", "ini", "ron", "json5", "ura", "convert-case", "async"] json = ["serde_json"] yaml = ["yaml-rust"] ini = ["rust-ini"] json5 = ["json5_rs", "serde/derive"] +ura = ["gura"] convert-case = ["convert_case"] preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order", "ron?/indexmap"] async = ["async-trait"] @@ -36,6 +37,7 @@ yaml-rust = { version = "0.4", optional = true } rust-ini = { version = "0.19", optional = true } ron = { version = "0.8", optional = true } json5_rs = { version = "0.4", optional = true, package = "json5" } +gura = { version = "0.3.0", optional = true, package = "gura" } indexmap = { version = "2.0.0", features = ["serde"], optional = true } convert_case = { version = "0.6", optional = true } pathdiff = "0.2" diff --git a/src/file/format/gura.rs b/src/file/format/gura.rs new file mode 100644 index 00000000..b3f1371f --- /dev/null +++ b/src/file/format/gura.rs @@ -0,0 +1,57 @@ +use std::error::Error; + +use gura::GuraType; + +use crate::map::Map; +use crate::value::{Value, ValueKind}; + +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result, Box> { + let val = from_gura_value(uri, &gura::parse(text).unwrap()); + + match val.kind { + ValueKind::Table(map) => Ok(map), + + _ => Ok(Map::new()) + } +} + +fn from_gura_value(uri: Option<&String>, val: &GuraType) -> Value { + match val { + GuraType::Null => Value::new(uri, ValueKind::Nil), + + GuraType::Object(ref table) => { + let mut m = Map::new(); + + for (key, val) in table { + m.insert(key.clone(), from_gura_value(uri, val)); + } + + Value::new(uri, ValueKind::Table(m)) + } + + GuraType::Bool(val) => Value::new(uri, ValueKind::Boolean(*val)), + + GuraType::String(ref val) => Value::new(uri, ValueKind::String(val.clone())), + + GuraType::Integer(val) => Value::new(uri, ValueKind::I64(*val as i64)), + + GuraType::BigInteger(val) => Value::new(uri, ValueKind::I128(*val)), + + GuraType::Float(val) => Value::new(uri, ValueKind::Float(*val)), + + GuraType::Array(ref arr) => { + let mut l = Vec::new(); + + for val in arr { + l.push(from_gura_value(uri, val)); + } + + Value::new(uri, ValueKind::Array(l)) + } + + _ => Value::new(uri, ValueKind::Nil), + } +} \ No newline at end of file diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 025e98a9..5984ecd6 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -27,6 +27,9 @@ mod ron; #[cfg(feature = "json5")] mod json5; +#[cfg(feature = "ura")] +mod gura; + /// File formats provided by the library. /// /// Although it is possible to define custom formats using [`Format`] trait it is recommended to use FileFormat if possible. @@ -55,6 +58,10 @@ pub enum FileFormat { /// JSON5 (parsed with json5) #[cfg(feature = "json5")] Json5, + + /// GURA (parsed with ura) + #[cfg(feature = "ura")] + Gura, } lazy_static! { @@ -81,6 +88,9 @@ lazy_static! { #[cfg(feature = "json5")] formats.insert(FileFormat::Json5, vec!["json5"]); + #[cfg(feature = "ura")] + formats.insert(FileFormat::Gura, vec!["ura"]); + formats }; } @@ -117,6 +127,9 @@ impl FileFormat { #[cfg(feature = "json5")] FileFormat::Json5 => json5::parse(uri, text), + #[cfg(feature = "ura")] + FileFormat::Gura => gura::parse(uri, text), + #[cfg(all( not(feature = "toml"), not(feature = "json"), diff --git a/tests/Settings-enum-test.ura b/tests/Settings-enum-test.ura new file mode 100644 index 00000000..e0500383 --- /dev/null +++ b/tests/Settings-enum-test.ura @@ -0,0 +1,3 @@ +# comment + +bar: "bar is a lowercase param" \ No newline at end of file diff --git a/tests/Settings.ura b/tests/Settings.ura new file mode 100644 index 00000000..46593319 --- /dev/null +++ b/tests/Settings.ura @@ -0,0 +1,20 @@ +#comment + +debug: true +debug_gura: true +production: false +arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +place: + name: "Torre di Pisa" + longitude: 43.7224985 + latitude: 10.3970522 + favorite: false + reviews: 3866 + rating: 4.5 + creator: + name: "John Smith" + username: "jsmith" + email: "jsmith@localhost" + +FOO: "FOO should be overridden" +bar: "I am bar" \ No newline at end of file diff --git a/tests/file_gura.rs b/tests/file_gura.rs new file mode 100644 index 00000000..2bd0cca0 --- /dev/null +++ b/tests/file_gura.rs @@ -0,0 +1,199 @@ +#![cfg(feature = "json")] + +use serde_derive::Deserialize; + +use config::{Config, File, FileFormat, Map, Value}; +use float_cmp::ApproxEqUlps; + +#[derive(Debug, Deserialize)] +struct Place { + name: String, + longitude: f64, + latitude: f64, + favorite: bool, + telephone: Option, + reviews: u64, + creator: Map, + rating: Option, +} + +#[derive(Debug, Deserialize)] +struct Settings { + debug: f64, + production: Option, + place: Place, + #[serde(rename = "arr")] + elements: Vec, +} + +fn make() -> Config { + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Gura)) + .build() + .unwrap() +} + +#[test] +fn test_file() { + let c = make(); + + // Deserialize the entire file as single struct + let s: Settings = c.try_deserialize().unwrap(); + + assert!(s.debug.approx_eq_ulps(&1.0, 2)); + assert_eq!(s.production, Some("false".to_string())); + assert_eq!(s.place.name, "Torre di Pisa"); + assert!(s.place.longitude.approx_eq_ulps(&43.722_498_5, 2)); + assert!(s.place.latitude.approx_eq_ulps(&10.397_052_2, 2)); + assert!(!s.place.favorite); + assert_eq!(s.place.reviews, 3866); + assert_eq!(s.place.rating, Some(4.5)); + assert_eq!(s.place.telephone, None); + assert_eq!(s.elements.len(), 10); + assert_eq!(s.elements[3], "4".to_string()); + if cfg!(feature = "preserve_order") { + assert_eq!( + s.place + .creator + .into_iter() + .collect::>(), + vec![ + ("name".to_string(), "John Smith".into()), + ("username".into(), "jsmith".into()), + ("email".into(), "jsmith@localhost".into()), + ] + ); + } else { + assert_eq!( + s.place.creator["name"].clone().into_string().unwrap(), + "John Smith".to_string() + ); + } +} + +#[test] +fn test_gura_vec() { + let c = Config::builder() + .add_source(File::from_str( + r#" + hosts: [ + "alpha", + "omega" + ] + "#, + FileFormat::Gura, + )) + .build() + .unwrap(); + + let v = c.get_array("hosts").unwrap(); + let mut vi = v.into_iter(); + assert_eq!(vi.next().unwrap().into_string().unwrap(), "alpha"); + assert_eq!(vi.next().unwrap().into_string().unwrap(), "omega"); + assert!(vi.next().is_none()); +} + +#[derive(Debug, Deserialize, PartialEq)] +enum EnumSettings { + Bar(String), +} + +#[derive(Debug, Deserialize, PartialEq)] +struct StructSettings { + foo: String, + bar: String, +} +#[derive(Debug, Deserialize, PartialEq)] +#[allow(non_snake_case)] +struct CapSettings { + FOO: String, +} + +#[test] +fn test_override_uppercase_value_for_struct() { + std::env::set_var("APP_FOO", "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE"); + + let cfg = Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Gura)) + .add_source(config::Environment::with_prefix("APP").separator("_")) + .build() + .unwrap(); + + let cap_settings = cfg.clone().try_deserialize::(); + let lower_settings = cfg.try_deserialize::().unwrap(); + + match cap_settings { + Ok(v) => { + // this assertion will ensure that the map has only lowercase keys + assert_ne!(v.FOO, "FOO should be overridden"); + assert_eq!( + lower_settings.foo, + "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_string() + ); + } + Err(e) => { + if e.to_string().contains("missing field `FOO`") { + println!("triggered error {:?}", e); + assert_eq!( + lower_settings.foo, + "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_string() + ); + } else { + panic!("{}", e); + } + } + } +} + +#[test] +fn test_override_lowercase_value_for_struct() { + std::env::set_var("config_foo", "I have been overridden_with_lower_case"); + + let cfg = Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Gura)) + .add_source(config::Environment::with_prefix("config").separator("_")) + .build() + .unwrap(); + + let values: StructSettings = cfg.try_deserialize().unwrap(); + assert_eq!( + values.foo, + "I have been overridden_with_lower_case".to_string() + ); + assert_ne!(values.foo, "I am bar".to_string()); +} + +#[test] +fn test_override_uppercase_value_for_enums() { + std::env::set_var("APPS_BAR", "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE"); + + let cfg = Config::builder() + .add_source(File::new("tests/Settings-enum-test", FileFormat::Gura)) + .add_source(config::Environment::with_prefix("APPS").separator("_")) + .build() + .unwrap(); + let val: EnumSettings = cfg.try_deserialize().unwrap(); + + assert_eq!( + val, + EnumSettings::Bar("I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_string()) + ); +} + +#[test] +fn test_override_lowercase_value_for_enums() { + std::env::set_var("test_bar", "I have been overridden_with_lower_case"); + + let cfg = Config::builder() + .add_source(File::new("tests/Settings-enum-test", FileFormat::Json)) + .add_source(config::Environment::with_prefix("test").separator("_")) + .build() + .unwrap(); + + let param: EnumSettings = cfg.try_deserialize().unwrap(); + + assert_eq!( + param, + EnumSettings::Bar("I have been overridden_with_lower_case".to_string()) + ); +} \ No newline at end of file From 41ef19eebfe511960e801310d28d1d96d9c302f7 Mon Sep 17 00:00:00 2001 From: Mikhail Volkov Date: Sat, 29 Jul 2023 08:54:05 +0300 Subject: [PATCH 2/8] chore: Add `gura` to README Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index badab891..6df9de0f 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - Set defaults - Set explicit values (to programmatically override) - - Read from [JSON], [TOML], [YAML], [INI], [RON], [JSON5] files + - Read from [JSON], [TOML], [YAML], [INI], [RON], [JSON5], [GURA] files - Read from environment - Loosely typed — Configuration values may be read in any supported type, as long as there exists a reasonable conversion - Access nested fields using a formatted path — Uses a subset of JSONPath; currently supports the child ( `redis.port` ) and subscript operators ( `databases[0].name` ) @@ -22,6 +22,7 @@ [INI]: https://github.com/zonyitoo/rust-ini [RON]: https://github.com/ron-rs/ron [JSON5]: https://github.com/callum-oakley/json5-rs +[GURA]: https://github.com/gura-conf/gura-rs-parser Please note this library @@ -43,6 +44,7 @@ config = "0.13.1" - `toml` - Adds support for reading TOML files - `ron` - Adds support for reading RON files - `json5` - Adds support for reading JSON5 files + - `gura` - Adds support for reading GURA files ### Support for custom formats From 061f45b0c6626e5547c5f917ab5f47aba101b7da Mon Sep 17 00:00:00 2001 From: Brennan Kinney <5098581+polarathene@users.noreply.github.com> Date: Fri, 6 Oct 2023 10:21:38 +1300 Subject: [PATCH 3/8] chore: Review feedback - `ura` => `gura` corrections - `src/lib.rs` add mention for Gura support tests(fix): Use `FileFormat::Gura` chore: Version bump `gura` Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- Cargo.toml | 4 ++-- src/file/format/mod.rs | 11 ++++++----- src/lib.rs | 2 +- tests/file_gura.rs | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 51f60d4e..cac3baf7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" maintenance = { status = "actively-developed" } [features] -default = ["toml", "json", "yaml", "ini", "ron", "json5", "ura", "convert-case", "async"] +default = ["toml", "json", "yaml", "ini", "ron", "json5", "gura", "convert-case", "async"] json = ["serde_json"] yaml = ["yaml-rust"] ini = ["rust-ini"] @@ -37,7 +37,7 @@ yaml-rust = { version = "0.4", optional = true } rust-ini = { version = "0.19", optional = true } ron = { version = "0.8", optional = true } json5_rs = { version = "0.4", optional = true, package = "json5" } -gura = { version = "0.3.0", optional = true, package = "gura" } +gura = { version = "0.5", optional = true, package = "gura" } indexmap = { version = "2.0.0", features = ["serde"], optional = true } convert_case = { version = "0.6", optional = true } pathdiff = "0.2" diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 5984ecd6..64e0db24 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -27,7 +27,7 @@ mod ron; #[cfg(feature = "json5")] mod json5; -#[cfg(feature = "ura")] +#[cfg(feature = "gura")] mod gura; /// File formats provided by the library. @@ -59,8 +59,8 @@ pub enum FileFormat { #[cfg(feature = "json5")] Json5, - /// GURA (parsed with ura) - #[cfg(feature = "ura")] + /// GURA (parsed with gura) + #[cfg(feature = "gura")] Gura, } @@ -88,7 +88,7 @@ lazy_static! { #[cfg(feature = "json5")] formats.insert(FileFormat::Json5, vec!["json5"]); - #[cfg(feature = "ura")] + #[cfg(feature = "gura")] formats.insert(FileFormat::Gura, vec!["ura"]); formats @@ -127,7 +127,7 @@ impl FileFormat { #[cfg(feature = "json5")] FileFormat::Json5 => json5::parse(uri, text), - #[cfg(feature = "ura")] + #[cfg(feature = "gura")] FileFormat::Gura => gura::parse(uri, text), #[cfg(all( @@ -137,6 +137,7 @@ impl FileFormat { not(feature = "ini"), not(feature = "ron"), not(feature = "json5"), + not(feature = "gura"), ))] _ => unreachable!("No features are enabled, this library won't work without features"), } diff --git a/src/lib.rs b/src/lib.rs index 511c638a..14fa4d5e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ //! - Environment variables //! - String literals in well-known formats //! - Another Config instance -//! - Files: TOML, JSON, YAML, INI, RON, JSON5 and custom ones defined with Format trait +//! - Files: TOML, JSON, YAML, INI, RON, JSON5, GURA and custom ones defined with Format trait //! - Manual, programmatic override (via a `.set` method on the Config instance) //! //! Additionally, Config supports: diff --git a/tests/file_gura.rs b/tests/file_gura.rs index 2bd0cca0..d55e663a 100644 --- a/tests/file_gura.rs +++ b/tests/file_gura.rs @@ -1,4 +1,4 @@ -#![cfg(feature = "json")] +#![cfg(feature = "gura")] use serde_derive::Deserialize; @@ -185,7 +185,7 @@ fn test_override_lowercase_value_for_enums() { std::env::set_var("test_bar", "I have been overridden_with_lower_case"); let cfg = Config::builder() - .add_source(File::new("tests/Settings-enum-test", FileFormat::Json)) + .add_source(File::new("tests/Settings-enum-test", FileFormat::Gura)) .add_source(config::Environment::with_prefix("test").separator("_")) .build() .unwrap(); From 48d7de89d39df6bea1e3ec647eb98c7328e40621 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:08:10 +1300 Subject: [PATCH 4/8] chore: Appease the linting gods Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/gura.rs | 6 +++--- tests/file_gura.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/file/format/gura.rs b/src/file/format/gura.rs index b3f1371f..9f0b181a 100644 --- a/src/file/format/gura.rs +++ b/src/file/format/gura.rs @@ -14,7 +14,7 @@ pub fn parse( match val.kind { ValueKind::Table(map) => Ok(map), - _ => Ok(Map::new()) + _ => Ok(Map::new()), } } @@ -50,8 +50,8 @@ fn from_gura_value(uri: Option<&String>, val: &GuraType) -> Value { } Value::new(uri, ValueKind::Array(l)) - } + } _ => Value::new(uri, ValueKind::Nil), } -} \ No newline at end of file +} diff --git a/tests/file_gura.rs b/tests/file_gura.rs index d55e663a..77724894 100644 --- a/tests/file_gura.rs +++ b/tests/file_gura.rs @@ -196,4 +196,4 @@ fn test_override_lowercase_value_for_enums() { param, EnumSettings::Bar("I have been overridden_with_lower_case".to_string()) ); -} \ No newline at end of file +} From 04254a86eb573eaad984f786009e320ed5e26190 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Sat, 7 Oct 2023 18:43:27 +1300 Subject: [PATCH 5/8] refactor: `from_gura_value` (json referenced) This method seems to be adapted from `format/json.rs:from_json_value()`. - I adjusted the order of matched types to mirror that of `format/json.rs`. - `val` => `value`. - No need to dereference values, in this case we consume the `value` parameter, rather than expect a reference to borrow. No need to use `ref` or `clone()` to create owned values. - Potential Improvement: Adopt the iter + collect pattern used by other formats instead of the `for` loop used for array and object types here. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/gura.rs | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/file/format/gura.rs b/src/file/format/gura.rs index 9f0b181a..82d548c6 100644 --- a/src/file/format/gura.rs +++ b/src/file/format/gura.rs @@ -9,49 +9,46 @@ pub fn parse( uri: Option<&String>, text: &str, ) -> Result, Box> { - let val = from_gura_value(uri, &gura::parse(text).unwrap()); - - match val.kind { + let value = from_gura_value(uri, gura::parse(text).unwrap()); + match value.kind { ValueKind::Table(map) => Ok(map), _ => Ok(Map::new()), } } -fn from_gura_value(uri: Option<&String>, val: &GuraType) -> Value { - match val { - GuraType::Null => Value::new(uri, ValueKind::Nil), +fn from_gura_value(uri: Option<&String>, value: GuraType) -> Value { + match value { + GuraType::String(value) => Value::new(uri, ValueKind::String(value)), + + GuraType::Integer(value) => Value::new(uri, ValueKind::I64(value as i64)), + GuraType::BigInteger(value) => Value::new(uri, ValueKind::I128(value)), + GuraType::Float(value) => Value::new(uri, ValueKind::Float(value)), - GuraType::Object(ref table) => { + GuraType::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), + + GuraType::Object(table) => { let mut m = Map::new(); - for (key, val) in table { - m.insert(key.clone(), from_gura_value(uri, val)); + for (key, value) in table { + m.insert(key, from_gura_value(uri, value)); } Value::new(uri, ValueKind::Table(m)) } - GuraType::Bool(val) => Value::new(uri, ValueKind::Boolean(*val)), - - GuraType::String(ref val) => Value::new(uri, ValueKind::String(val.clone())), - - GuraType::Integer(val) => Value::new(uri, ValueKind::I64(*val as i64)), - - GuraType::BigInteger(val) => Value::new(uri, ValueKind::I128(*val)), - - GuraType::Float(val) => Value::new(uri, ValueKind::Float(*val)), - - GuraType::Array(ref arr) => { + GuraType::Array(array) => { let mut l = Vec::new(); - for val in arr { - l.push(from_gura_value(uri, val)); + for value in array { + l.push(from_gura_value(uri, value)); } Value::new(uri, ValueKind::Array(l)) } + // Null or remaining types (only intended for internal use): + GuraType::Null => Value::new(uri, ValueKind::Nil), _ => Value::new(uri, ValueKind::Nil), } } From 73e43046f356418000ffbbe69f3924f6882f4e02 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:23:36 +1300 Subject: [PATCH 6/8] refactor: `from_gura_value` (json5 referenced) Now adapted to the better approach handled by json5 equivalent method. - DRY, avoid the value wrapper reptition via storing the match value and using a final return afterwards to value wrap: `Value::new(uri, vk)`. - Object and Array kinds now adopt the json5 iter + collect approach. Additionally corrects the `Cargo.toml` feature `ura` to `gura`. As the feature name conflicts with the equivalent dependency name, the feature needs to prefix the dependency with `dep:`, removing the need for the package to include `pacakge = "gura"`. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- Cargo.toml | 4 ++-- src/file/format/gura.rs | 45 +++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cac3baf7..9aeec9e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ json = ["serde_json"] yaml = ["yaml-rust"] ini = ["rust-ini"] json5 = ["json5_rs", "serde/derive"] -ura = ["gura"] +gura = ["dep:gura"] convert-case = ["convert_case"] preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order", "ron?/indexmap"] async = ["async-trait"] @@ -37,7 +37,7 @@ yaml-rust = { version = "0.4", optional = true } rust-ini = { version = "0.19", optional = true } ron = { version = "0.8", optional = true } json5_rs = { version = "0.4", optional = true, package = "json5" } -gura = { version = "0.5", optional = true, package = "gura" } +gura = { version = "0.5", optional = true } indexmap = { version = "2.0.0", features = ["serde"], optional = true } convert_case = { version = "0.6", optional = true } pathdiff = "0.2" diff --git a/src/file/format/gura.rs b/src/file/format/gura.rs index 82d548c6..48d160b7 100644 --- a/src/file/format/gura.rs +++ b/src/file/format/gura.rs @@ -18,37 +18,38 @@ pub fn parse( } fn from_gura_value(uri: Option<&String>, value: GuraType) -> Value { - match value { - GuraType::String(value) => Value::new(uri, ValueKind::String(value)), + let vk = match value { + GuraType::String(value) => ValueKind::String(value), - GuraType::Integer(value) => Value::new(uri, ValueKind::I64(value as i64)), - GuraType::BigInteger(value) => Value::new(uri, ValueKind::I128(value)), - GuraType::Float(value) => Value::new(uri, ValueKind::Float(value)), + GuraType::Integer(value) => ValueKind::I64(value as i64), + GuraType::BigInteger(value) => ValueKind::I128(value), + GuraType::Float(value) => ValueKind::Float(value), - GuraType::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), + GuraType::Bool(value) => ValueKind::Boolean(value), GuraType::Object(table) => { - let mut m = Map::new(); + let m = table + .into_iter() + .map(|(k, v)| (k, from_gura_value(uri, v))) + .collect(); - for (key, value) in table { - m.insert(key, from_gura_value(uri, value)); - } - - Value::new(uri, ValueKind::Table(m)) + ValueKind::Table(m) } GuraType::Array(array) => { - let mut l = Vec::new(); - - for value in array { - l.push(from_gura_value(uri, value)); - } + let l = array + .into_iter() + .map(|v| from_gura_value(uri, v)) + .collect(); - Value::new(uri, ValueKind::Array(l)) + ValueKind::Array(l) } - // Null or remaining types (only intended for internal use): - GuraType::Null => Value::new(uri, ValueKind::Nil), - _ => Value::new(uri, ValueKind::Nil), - } + GuraType::Null => ValueKind::Nil, + + // Remaining types (only intended for internal use): + _ => ValueKind::Nil, + }; + + Value::new(uri, vk) } From 0b781b8f9fc019a7ce990c9fcca8acb1aaf1d21c Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:41:24 +1300 Subject: [PATCH 7/8] chore: Use the common parser root check method Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/gura.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/file/format/gura.rs b/src/file/format/gura.rs index 48d160b7..658dd94f 100644 --- a/src/file/format/gura.rs +++ b/src/file/format/gura.rs @@ -2,6 +2,7 @@ use std::error::Error; use gura::GuraType; +use crate::format; use crate::map::Map; use crate::value::{Value, ValueKind}; @@ -10,11 +11,7 @@ pub fn parse( text: &str, ) -> Result, Box> { let value = from_gura_value(uri, gura::parse(text).unwrap()); - match value.kind { - ValueKind::Table(map) => Ok(map), - - _ => Ok(Map::new()), - } + format::extract_root_table(uri, value) } fn from_gura_value(uri: Option<&String>, value: GuraType) -> Value { From 4140214c3fcd8e7c52838f81f4d8c3a487f50f17 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:38:08 +1300 Subject: [PATCH 8/8] chore: Switch to `serde_gura` - No `.unwrap()` concern now. - Leverage the common `from_parsed_value` method. - Greatly simplified format support. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- Cargo.toml | 4 ++-- src/file/format/gura.rs | 44 +++--------------------------------- src/file/format/mod.rs | 2 +- tests/Settings-enum-test.ura | 2 +- tests/Settings.ura | 2 +- 5 files changed, 8 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9aeec9e1..ea695059 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ json = ["serde_json"] yaml = ["yaml-rust"] ini = ["rust-ini"] json5 = ["json5_rs", "serde/derive"] -gura = ["dep:gura"] +gura = ["serde_gura"] convert-case = ["convert_case"] preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order", "ron?/indexmap"] async = ["async-trait"] @@ -37,7 +37,7 @@ yaml-rust = { version = "0.4", optional = true } rust-ini = { version = "0.19", optional = true } ron = { version = "0.8", optional = true } json5_rs = { version = "0.4", optional = true, package = "json5" } -gura = { version = "0.5", optional = true } +serde_gura = { version = "0.1", optional = true } indexmap = { version = "2.0.0", features = ["serde"], optional = true } convert_case = { version = "0.6", optional = true } pathdiff = "0.2" diff --git a/src/file/format/gura.rs b/src/file/format/gura.rs index 658dd94f..324631e6 100644 --- a/src/file/format/gura.rs +++ b/src/file/format/gura.rs @@ -1,52 +1,14 @@ use std::error::Error; -use gura::GuraType; - 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_gura_value(uri, gura::parse(text).unwrap()); + // Parse a Gura input from the provided text + let value = format::from_parsed_value(uri, serde_gura::from_str(text)?); format::extract_root_table(uri, value) } - -fn from_gura_value(uri: Option<&String>, value: GuraType) -> Value { - let vk = match value { - GuraType::String(value) => ValueKind::String(value), - - GuraType::Integer(value) => ValueKind::I64(value as i64), - GuraType::BigInteger(value) => ValueKind::I128(value), - GuraType::Float(value) => ValueKind::Float(value), - - GuraType::Bool(value) => ValueKind::Boolean(value), - - GuraType::Object(table) => { - let m = table - .into_iter() - .map(|(k, v)| (k, from_gura_value(uri, v))) - .collect(); - - ValueKind::Table(m) - } - - GuraType::Array(array) => { - let l = array - .into_iter() - .map(|v| from_gura_value(uri, v)) - .collect(); - - ValueKind::Array(l) - } - - GuraType::Null => ValueKind::Nil, - - // Remaining types (only intended for internal use): - _ => ValueKind::Nil, - }; - - Value::new(uri, vk) -} diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 64e0db24..779fdb50 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -59,7 +59,7 @@ pub enum FileFormat { #[cfg(feature = "json5")] Json5, - /// GURA (parsed with gura) + /// GURA (parsed with serde_gura) #[cfg(feature = "gura")] Gura, } diff --git a/tests/Settings-enum-test.ura b/tests/Settings-enum-test.ura index e0500383..082272dd 100644 --- a/tests/Settings-enum-test.ura +++ b/tests/Settings-enum-test.ura @@ -1,3 +1,3 @@ # comment -bar: "bar is a lowercase param" \ No newline at end of file +bar: "bar is a lowercase param" diff --git a/tests/Settings.ura b/tests/Settings.ura index 46593319..0ba64562 100644 --- a/tests/Settings.ura +++ b/tests/Settings.ura @@ -17,4 +17,4 @@ place: email: "jsmith@localhost" FOO: "FOO should be overridden" -bar: "I am bar" \ No newline at end of file +bar: "I am bar"