Skip to content
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

refactor: Use a common from_value method #472

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -27,12 +27,13 @@ async = ["async-trait"]
[dependencies]
lazy_static = "1.0"
serde = "1.0.8"
serde_with = "3"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required for the Nil variant fallback (scoped to it's own commit) if deserialize_any fails to match anything prior.

The crate could be avoided if you wanted to implement the equivalent directly 🤷‍♂️

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" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 3 additions & 43 deletions src/file/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, Value>, Box<dyn Error + Send + Sync>> {
// 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),
}
}
46 changes: 3 additions & 43 deletions src/file/format/json5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>),
Object(Map<String, Self>),
}
use crate::value::Value;

pub fn parse(
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
let value = from_json5_value(uri, json5_rs::from_str::<Val>(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)
}
2 changes: 1 addition & 1 deletion src/file/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
54 changes: 2 additions & 52 deletions src/file/format/ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, Value>, Box<dyn Error + Send + Sync>> {
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<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::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::<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<Map<_, _>, _>>()?;

ValueKind::Table(map)
}
};

Ok(Value::new(uri, kind))
}
35 changes: 2 additions & 33 deletions src/file/format/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,7 @@ pub fn parse(
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
// 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 = 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 {
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)
}

toml::Value::Array(ref array) => {
let mut l = Vec::new();

for value in array {
l.push(from_toml_value(uri, value));
}

Value::new(uri, l)
}

toml::Value::Datetime(ref datetime) => Value::new(uri, datetime.to_string()),
}
}
97 changes: 3 additions & 94 deletions src/file/format/yaml.rs
Original file line number Diff line number Diff line change
@@ -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<Map<String, Value>, Box<dyn Error + Send + Sync>> {
// 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<Value, Box<dyn Error + Send + Sync>> {
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::<f64>()
.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"
}
}
Loading