-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added Gura support #467
Open
polarathene
wants to merge
8
commits into
rust-cli:main
Choose a base branch
from
polarathene:gura
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d03821e
feat: Added Gura support
mkvolkov 41ef19e
chore: Add `gura` to README
mkvolkov 061f45b
chore: Review feedback
polarathene 48d7de8
chore: Appease the linting gods
polarathene 04254a8
refactor: `from_gura_value` (json referenced)
polarathene 73e4304
refactor: `from_gura_value` (json5 referenced)
polarathene 0b781b8
chore: Use the common parser root check method
polarathene 4140214
chore: Switch to `serde_gura`
polarathene File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use std::error::Error; | ||
|
||
use crate::format; | ||
use crate::map::Map; | ||
use crate::value::Value; | ||
|
||
pub fn parse( | ||
uri: Option<&String>, | ||
text: &str, | ||
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> { | ||
// 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# comment | ||
|
||
bar: "bar is a lowercase param" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
#![cfg(feature = "gura")] | ||
|
||
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<String>, | ||
reviews: u64, | ||
creator: Map<String, Value>, | ||
rating: Option<f32>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Settings { | ||
debug: f64, | ||
production: Option<String>, | ||
place: Place, | ||
#[serde(rename = "arr")] | ||
elements: Vec<String>, | ||
} | ||
|
||
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<(String, config::Value)>>(), | ||
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::<CapSettings>(); | ||
let lower_settings = cfg.try_deserialize::<StructSettings>().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::Gura)) | ||
.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()) | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test suite check is failing here with:
EDIT: I just ran the test suite with the
gura
crate reverted back to0.3
and tests pass. Unfortunately there's no release notes / changelog for this crate, so it's unclear what happened or what the migration is 😕Presumably if their equivalent serde crate was used instead (uses the
gura
crate), this would be a non-issue 🤷♂️UPDATE: Adapted to
serde_gura
and no difference. Identified the failure occurs in0.5.0
onwards. Raised a bug report upstream. Not much I can do until then, other than revert the version back (I'm not sure what meaningful differences the newer releases have introduced due to lack of changelog / release notes).NOTE: Despite the test suite claiming to pass for the nightly toolchain, it actually did fail too.