Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ json = ["serde_json"]
yaml = ["yaml-rust2"]
ini = ["rust-ini"]
json5 = ["json5_rs", "dep:serde-untagged"]
corn = ["dep:corn"]
convert-case = ["convert_case"]
preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order", "ron?/indexmap"]
async = ["async-trait"]
Expand All @@ -141,6 +142,7 @@ yaml-rust2 = { version = "0.10.4", optional = true }
rust-ini = { version = "0.21.3", optional = true }
ron = { version = "0.8.1", optional = true }
json5_rs = { version = "0.4.1", optional = true, package = "json5" }
corn = { version = "0.10.0", optional = true, package = "libcorn" }
indexmap = { version = "2.11.4", features = ["serde"], optional = true }
convert_case = { version = "0.6.0", optional = true }
pathdiff = "0.2.3"
Expand Down
39 changes: 39 additions & 0 deletions src/file/format/corn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::value::{Value, ValueKind};
use crate::{format, Map};
use std::error::Error;

pub(crate) fn parse(
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
let value = from_corn_value(uri, &corn::parse(text)?);
format::extract_root_table(uri, value)
}

fn from_corn_value(uri: Option<&String>, value: &corn::Value<'_>) -> Value {
match value {
corn::Value::String(value) => Value::new(uri, ValueKind::String(value.to_string())),
corn::Value::Integer(value) => Value::new(uri, ValueKind::I64(*value)),
corn::Value::Float(value) => Value::new(uri, ValueKind::Float(*value)),
corn::Value::Boolean(value) => Value::new(uri, ValueKind::Boolean(*value)),
corn::Value::Object(value) => Value::new(
uri,
ValueKind::Table(
value
.iter()
.map(|(key, value)| (key.to_string(), from_corn_value(uri, value)))
.collect(),
),
),
corn::Value::Array(value) => Value::new(
uri,
ValueKind::Array(
value
.iter()
.map(|value| from_corn_value(uri, value))
.collect(),
),
),
corn::Value::Null(_) => Value::new(uri, ValueKind::Nil),
}
}
15 changes: 15 additions & 0 deletions src/file/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ mod ron;
#[cfg(feature = "json5")]
mod json5;

#[cfg(feature = "corn")]
mod corn;

/// 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.
Expand Down Expand Up @@ -50,6 +53,10 @@ pub enum FileFormat {
/// JSON5 (parsed with json5)
#[cfg(feature = "json5")]
Json5,

/// Corn (parsed with `libcorn`)
#[cfg(feature = "corn")]
Corn,
}

impl FileFormat {
Expand All @@ -67,6 +74,8 @@ impl FileFormat {
FileFormat::Ron,
#[cfg(feature = "json5")]
FileFormat::Json5,
#[cfg(feature = "corn")]
FileFormat::Corn,
]
}

Expand All @@ -90,6 +99,9 @@ impl FileFormat {
#[cfg(feature = "json5")]
FileFormat::Json5 => &["json5"],

#[cfg(feature = "corn")]
FileFormat::Corn => &["corn"],

#[cfg(all(
not(feature = "toml"),
not(feature = "json"),
Expand Down Expand Up @@ -126,6 +138,9 @@ impl FileFormat {
#[cfg(feature = "json5")]
FileFormat::Json5 => json5::parse(uri, text),

#[cfg(feature = "corn")]
FileFormat::Corn => corn::parse(uri, text),

#[cfg(all(
not(feature = "toml"),
not(feature = "json"),
Expand Down
Loading
Loading