-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from skreborn/master
Add support for RON format
- Loading branch information
Showing
9 changed files
with
215 additions
and
3 deletions.
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
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,68 @@ | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
|
||
use ron; | ||
|
||
use crate::value::{Value, ValueKind}; | ||
|
||
pub fn parse( | ||
uri: Option<&String>, | ||
text: &str, | ||
) -> Result<HashMap<String, Value>, Box<dyn Error + Send + Sync>> { | ||
let value = from_ron_value(uri, ron::from_str(text)?)?; | ||
match value.kind { | ||
ValueKind::Table(map) => Ok(map), | ||
|
||
_ => Ok(HashMap::new()), | ||
} | ||
} | ||
|
||
fn from_ron_value( | ||
uri: Option<&String>, | ||
value: ron::Value, | ||
) -> Result<Value, Box<dyn Error + Send + Sync>> { | ||
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::Integer(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::<Result<Vec<_>, _>>()?; | ||
|
||
ValueKind::Array(array) | ||
} | ||
|
||
ron::Value::Map(values) => { | ||
let map = values | ||
.iter() | ||
.map(|(key, value)| -> Result<_, Box<dyn Error + Send + Sync>> { | ||
let key = key.clone().into_rust::<String>()?; | ||
let value = from_ron_value(uri, value.clone())?; | ||
|
||
Ok((key, value)) | ||
}) | ||
.collect::<Result<HashMap<_, _>, _>>()?; | ||
|
||
ValueKind::Table(map) | ||
} | ||
}; | ||
|
||
Ok(Value::new(uri, kind)) | ||
} |
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,4 @@ | ||
( | ||
ok: true, | ||
error | ||
) |
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,18 @@ | ||
( | ||
debug: true, | ||
production: false, | ||
arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | ||
place: ( | ||
initials: ('T', 'P'), | ||
name: "Torre di Pisa", | ||
longitude: 43.7224985, | ||
latitude: 10.3970522, | ||
favorite: false, | ||
reviews: 3866, | ||
rating: Some(4.5), | ||
telephone: None, | ||
creator: { | ||
"name": "John Smith" | ||
} | ||
) | ||
) |
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,83 @@ | ||
#![cfg(feature = "ron")] | ||
|
||
extern crate config; | ||
extern crate float_cmp; | ||
extern crate serde; | ||
|
||
#[macro_use] | ||
extern crate serde_derive; | ||
|
||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
|
||
use config::*; | ||
use float_cmp::ApproxEqUlps; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Place { | ||
initials: (char, char), | ||
name: String, | ||
longitude: f64, | ||
latitude: f64, | ||
favorite: bool, | ||
telephone: Option<String>, | ||
reviews: u64, | ||
creator: HashMap<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 { | ||
let mut c = Config::default(); | ||
c.merge(File::new("tests/Settings", FileFormat::Ron)) | ||
.unwrap(); | ||
|
||
c | ||
} | ||
|
||
#[test] | ||
fn test_file() { | ||
let c = make(); | ||
|
||
// Deserialize the entire file as single struct | ||
let s: Settings = c.try_into().unwrap(); | ||
|
||
assert!(s.debug.approx_eq_ulps(&1.0, 2)); | ||
assert_eq!(s.production, Some("false".to_string())); | ||
assert_eq!(s.place.initials, ('T', 'P')); | ||
assert_eq!(s.place.name, "Torre di Pisa"); | ||
assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2)); | ||
assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2)); | ||
assert_eq!(s.place.favorite, false); | ||
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()); | ||
assert_eq!( | ||
s.place.creator["name"].clone().into_string().unwrap(), | ||
"John Smith".to_string() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_error_parse() { | ||
let mut c = Config::default(); | ||
let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Ron)); | ||
|
||
let path_with_extension: PathBuf = ["tests", "Settings-invalid.ron"].iter().collect(); | ||
|
||
assert!(res.is_err()); | ||
assert_eq!( | ||
res.unwrap_err().to_string(), | ||
format!("4:1: Expected colon in {}", path_with_extension.display()) | ||
); | ||
} |