Added fix for #7981 - Replaced crate serde_ini with rust-ini for package nu-command/from (#8009)

# Description

Added fix for #7981 - Replaced crate serde_ini with rust-ini for package
nu-command/from

# Tests + Formatting

Added a test to support addition of the rust-ini crate.

`cargo test --package nu-command --lib -- formats::from::ini::tests
--nocapture`

Executed all tests.

`cargo test --workspace`

---------

Co-authored-by: Nitin Londhe <nitin.londhe@genmills.com>
This commit is contained in:
NitinL
2023-02-09 17:17:45 +05:30
committed by GitHub
parent 8e9ed14b89
commit 659d890ecf
4 changed files with 95 additions and 36 deletions

View File

@ -76,7 +76,7 @@ roxmltree = "0.17.0"
rust-embed = "6.3.0"
same-file = "1.0.6"
serde = { version = "1.0.123", features = ["derive"] }
serde_ini = "0.2.0"
rust-ini = "0.18.0"
serde_urlencoded = "0.7.0"
serde_yaml = "0.9.4"
sha2 = "0.10.0"

View File

@ -1,4 +1,3 @@
use indexmap::map::IndexMap;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
@ -58,23 +57,50 @@ pub fn from_ini_string_to_value(
span: Span,
val_span: Span,
) -> Result<Value, ShellError> {
let v: Result<IndexMap<String, IndexMap<String, String>>, serde_ini::de::Error> =
serde_ini::from_str(&s);
match v {
Ok(index_map) => {
let (cols, vals) = index_map
.into_iter()
.fold((vec![], vec![]), |mut acc, (k, v)| {
let (cols, vals) = v.into_iter().fold((vec![], vec![]), |mut acc, (k, v)| {
acc.0.push(k);
acc.1.push(Value::String { val: v, span });
acc
let ini_config: Result<ini::Ini, ini::ParseError> = ini::Ini::load_from_str(&s);
match ini_config {
Ok(config) => {
let mut sections: Vec<String> = Vec::new();
let mut sections_key_value_pairs: Vec<Value> = Vec::new();
for (section, properties) in config.iter() {
let mut keys_for_section: Vec<String> = Vec::new();
let mut values_for_section: Vec<Value> = Vec::new();
// section
match section {
Some(section_name) => {
sections.push(section_name.to_owned());
}
None => {
sections.push(String::new());
}
}
// section's key value pairs
for (key, value) in properties.iter() {
keys_for_section.push(key.to_owned());
values_for_section.push(Value::String {
val: value.to_owned(),
span,
});
acc.0.push(k);
acc.1.push(Value::Record { cols, vals, span });
acc
}
// section with its key value pairs
sections_key_value_pairs.push(Value::Record {
cols: keys_for_section,
vals: values_for_section,
span,
});
Ok(Value::Record { cols, vals, span })
}
// all sections with all its key value pairs
Ok(Value::Record {
cols: sections,
vals: sections_key_value_pairs,
span,
})
}
Err(err) => Err(ShellError::UnsupportedInput(
format!("Could not load ini: {err}"),
@ -104,4 +130,28 @@ mod tests {
test_examples(FromIni {})
}
#[test]
fn read_ini_config_passes() {
let ini_test_config = r"
min-width=450
max-width=820
[normal]
sound-file=/usr/share/sounds/freedesktop/stereo/dialog-information.oga
[critical]
border-color=FAB387ff
default-timeout=20
sound-file=/usr/share/sounds/freedesktop/stereo/dialog-warning.oga
";
let result = from_ini_string_to_value(
ini_test_config.to_owned(),
Span::test_data(),
Span::test_data(),
);
assert!(result.is_ok());
}
}

View File

@ -19,10 +19,10 @@ mod yaml;
pub use self::csv::FromCsv;
pub use self::toml::FromToml;
pub use self::url::FromUrl;
pub use crate::formats::from::ini::FromIni;
pub use command::From;
pub use eml::FromEml;
pub use ics::FromIcs;
pub use ini::FromIni;
pub use json::FromJson;
pub use nuon::FromNuon;
pub use ods::FromOds;