Skip to content

Commit

Permalink
chore: Switch to serde_gura
Browse files Browse the repository at this point in the history
- No `.unwrap()` concern now.
- Leverage the common `from_parsed_value` method.
- Greatly simplified format support.

Signed-off-by: Brennan Kinney <[email protected]>
  • Loading branch information
polarathene committed Oct 7, 2023
1 parent 0b781b8 commit 4140214
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"
Expand Down
44 changes: 3 additions & 41 deletions src/file/format/gura.rs
Original file line number Diff line number Diff line change
@@ -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<Map<String, Value>, Box<dyn Error + Send + Sync>> {
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)
}
2 changes: 1 addition & 1 deletion src/file/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum FileFormat {
#[cfg(feature = "json5")]
Json5,

/// GURA (parsed with gura)
/// GURA (parsed with serde_gura)
#[cfg(feature = "gura")]
Gura,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Settings-enum-test.ura
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# comment

bar: "bar is a lowercase param"
bar: "bar is a lowercase param"
2 changes: 1 addition & 1 deletion tests/Settings.ura
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ place:
email: "jsmith@localhost"

FOO: "FOO should be overridden"
bar: "I am bar"
bar: "I am bar"

0 comments on commit 4140214

Please sign in to comment.