mirror of
https://github.com/nushell/nushell
synced 2024-12-25 12:33:17 +00:00
refactor: switch serde_yaml to serde_yml
This commit is contained in:
parent
bf8763fc11
commit
d6c134c98f
5 changed files with 68 additions and 67 deletions
28
Cargo.lock
generated
28
Cargo.lock
generated
|
@ -2685,6 +2685,16 @@ dependencies = [
|
||||||
"vcpkg",
|
"vcpkg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libyml"
|
||||||
|
version = "0.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3302702afa434ffa30847a83305f0a69d6abd74293b6554c18ec85c7ef30c980"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libz-sys"
|
name = "libz-sys"
|
||||||
version = "1.1.20"
|
version = "1.1.20"
|
||||||
|
@ -3396,7 +3406,7 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"serde_yaml",
|
"serde_yml",
|
||||||
"sha2",
|
"sha2",
|
||||||
"sysinfo 0.32.1",
|
"sysinfo 0.32.1",
|
||||||
"tabled",
|
"tabled",
|
||||||
|
@ -6234,16 +6244,18 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_yaml"
|
name = "serde_yml"
|
||||||
version = "0.9.34+deprecated"
|
version = "0.0.12"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
|
checksum = "59e2dd588bf1597a252c3b920e0143eb99b0f76e4e082f4c92ce34fbc9e71ddd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"itoa",
|
"itoa",
|
||||||
|
"libyml",
|
||||||
|
"memchr",
|
||||||
"ryu",
|
"ryu",
|
||||||
"serde",
|
"serde",
|
||||||
"unsafe-libyaml",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -7251,12 +7263,6 @@ version = "0.2.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce"
|
checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unsafe-libyaml"
|
|
||||||
version = "0.2.11"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "untrusted"
|
name = "untrusted"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
|
|
|
@ -151,7 +151,7 @@ scopeguard = { version = "1.2.0" }
|
||||||
serde = { version = "1.0" }
|
serde = { version = "1.0" }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde_urlencoded = "0.7.1"
|
serde_urlencoded = "0.7.1"
|
||||||
serde_yaml = "0.9"
|
serde_yml = "0.0.12"
|
||||||
sha2 = "0.10"
|
sha2 = "0.10"
|
||||||
strip-ansi-escapes = "0.2.0"
|
strip-ansi-escapes = "0.2.0"
|
||||||
syn = "2.0"
|
syn = "2.0"
|
||||||
|
|
|
@ -84,7 +84,7 @@ scopeguard = { workspace = true }
|
||||||
serde = { workspace = true, features = ["derive"] }
|
serde = { workspace = true, features = ["derive"] }
|
||||||
serde_json = { workspace = true, features = ["preserve_order"] }
|
serde_json = { workspace = true, features = ["preserve_order"] }
|
||||||
serde_urlencoded = { workspace = true }
|
serde_urlencoded = { workspace = true }
|
||||||
serde_yaml = { workspace = true }
|
serde_yml = { workspace = true }
|
||||||
sha2 = { workspace = true }
|
sha2 = { workspace = true }
|
||||||
sysinfo = { workspace = true }
|
sysinfo = { workspace = true }
|
||||||
tabled = { workspace = true, features = ["ansi"], default-features = false }
|
tabled = { workspace = true, features = ["ansi"], default-features = false }
|
||||||
|
|
|
@ -72,7 +72,7 @@ impl Command for FromYml {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_yaml_value_to_nu_value(
|
fn convert_yaml_value_to_nu_value(
|
||||||
v: &serde_yaml::Value,
|
v: &serde_yml::Value,
|
||||||
span: Span,
|
span: Span,
|
||||||
val_span: Span,
|
val_span: Span,
|
||||||
) -> Result<Value, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
|
@ -83,22 +83,22 @@ fn convert_yaml_value_to_nu_value(
|
||||||
input_span: val_span,
|
input_span: val_span,
|
||||||
};
|
};
|
||||||
Ok(match v {
|
Ok(match v {
|
||||||
serde_yaml::Value::Bool(b) => Value::bool(*b, span),
|
serde_yml::Value::Bool(b) => Value::bool(*b, span),
|
||||||
serde_yaml::Value::Number(n) if n.is_i64() => {
|
serde_yml::Value::Number(n) if n.is_i64() => {
|
||||||
Value::int(n.as_i64().ok_or(err_not_compatible_number)?, span)
|
Value::int(n.as_i64().ok_or(err_not_compatible_number)?, span)
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Number(n) if n.is_f64() => {
|
serde_yml::Value::Number(n) if n.is_f64() => {
|
||||||
Value::float(n.as_f64().ok_or(err_not_compatible_number)?, span)
|
Value::float(n.as_f64().ok_or(err_not_compatible_number)?, span)
|
||||||
}
|
}
|
||||||
serde_yaml::Value::String(s) => Value::string(s.to_string(), span),
|
serde_yml::Value::String(s) => Value::string(s.to_string(), span),
|
||||||
serde_yaml::Value::Sequence(a) => {
|
serde_yml::Value::Sequence(a) => {
|
||||||
let result: Result<Vec<Value>, ShellError> = a
|
let result: Result<Vec<Value>, ShellError> = a
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| convert_yaml_value_to_nu_value(x, span, val_span))
|
.map(|x| convert_yaml_value_to_nu_value(x, span, val_span))
|
||||||
.collect();
|
.collect();
|
||||||
Value::list(result?, span)
|
Value::list(result?, span)
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Mapping(t) => {
|
serde_yml::Value::Mapping(t) => {
|
||||||
// Using an IndexMap ensures consistent ordering
|
// Using an IndexMap ensures consistent ordering
|
||||||
let mut collected = IndexMap::new();
|
let mut collected = IndexMap::new();
|
||||||
|
|
||||||
|
@ -111,19 +111,19 @@ fn convert_yaml_value_to_nu_value(
|
||||||
input_span: val_span,
|
input_span: val_span,
|
||||||
};
|
};
|
||||||
match (k, v) {
|
match (k, v) {
|
||||||
(serde_yaml::Value::Number(k), _) => {
|
(serde_yml::Value::Number(k), _) => {
|
||||||
collected.insert(
|
collected.insert(
|
||||||
k.to_string(),
|
k.to_string(),
|
||||||
convert_yaml_value_to_nu_value(v, span, val_span)?,
|
convert_yaml_value_to_nu_value(v, span, val_span)?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
(serde_yaml::Value::Bool(k), _) => {
|
(serde_yml::Value::Bool(k), _) => {
|
||||||
collected.insert(
|
collected.insert(
|
||||||
k.to_string(),
|
k.to_string(),
|
||||||
convert_yaml_value_to_nu_value(v, span, val_span)?,
|
convert_yaml_value_to_nu_value(v, span, val_span)?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
(serde_yaml::Value::String(k), _) => {
|
(serde_yml::Value::String(k), _) => {
|
||||||
collected.insert(
|
collected.insert(
|
||||||
k.clone(),
|
k.clone(),
|
||||||
convert_yaml_value_to_nu_value(v, span, val_span)?,
|
convert_yaml_value_to_nu_value(v, span, val_span)?,
|
||||||
|
@ -132,16 +132,16 @@ fn convert_yaml_value_to_nu_value(
|
||||||
// Hard-code fix for cases where "v" is a string without quotations with double curly braces
|
// Hard-code fix for cases where "v" is a string without quotations with double curly braces
|
||||||
// e.g. k = value
|
// e.g. k = value
|
||||||
// value: {{ something }}
|
// value: {{ something }}
|
||||||
// Strangely, serde_yaml returns
|
// Strangely, serde_yml returns
|
||||||
// "value" -> Mapping(Mapping { map: {Mapping(Mapping { map: {String("something"): Null} }): Null} })
|
// "value" -> Mapping(Mapping { map: {Mapping(Mapping { map: {String("something"): Null} }): Null} })
|
||||||
(serde_yaml::Value::Mapping(m), serde_yaml::Value::Null) => {
|
(serde_yml::Value::Mapping(m), serde_yml::Value::Null) => {
|
||||||
return m
|
return m
|
||||||
.iter()
|
.iter()
|
||||||
.take(1)
|
.take(1)
|
||||||
.collect_vec()
|
.collect_vec()
|
||||||
.first()
|
.first()
|
||||||
.and_then(|e| match e {
|
.and_then(|e| match e {
|
||||||
(serde_yaml::Value::String(s), serde_yaml::Value::Null) => {
|
(serde_yml::Value::String(s), serde_yml::Value::Null) => {
|
||||||
Some(Value::string("{{ ".to_owned() + s.as_str() + " }}", span))
|
Some(Value::string("{{ ".to_owned() + s.as_str() + " }}", span))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -156,22 +156,22 @@ fn convert_yaml_value_to_nu_value(
|
||||||
|
|
||||||
Value::record(collected.into_iter().collect(), span)
|
Value::record(collected.into_iter().collect(), span)
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Tagged(t) => {
|
serde_yml::Value::Tagged(t) => {
|
||||||
let tag = &t.tag;
|
let tag = &t.tag;
|
||||||
let value = match &t.value {
|
let value = match &t.value {
|
||||||
serde_yaml::Value::String(s) => {
|
serde_yml::Value::String(s) => {
|
||||||
let val = format!("{} {}", tag, s).trim().to_string();
|
let val = format!("{} {}", tag, s).trim().to_string();
|
||||||
Value::string(val, span)
|
Value::string(val, span)
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Number(n) => {
|
serde_yml::Value::Number(n) => {
|
||||||
let val = format!("{} {}", tag, n).trim().to_string();
|
let val = format!("{} {}", tag, n).trim().to_string();
|
||||||
Value::string(val, span)
|
Value::string(val, span)
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Bool(b) => {
|
serde_yml::Value::Bool(b) => {
|
||||||
let val = format!("{} {}", tag, b).trim().to_string();
|
let val = format!("{} {}", tag, b).trim().to_string();
|
||||||
Value::string(val, span)
|
Value::string(val, span)
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Null => {
|
serde_yml::Value::Null => {
|
||||||
let val = format!("{}", tag).trim().to_string();
|
let val = format!("{}", tag).trim().to_string();
|
||||||
Value::string(val, span)
|
Value::string(val, span)
|
||||||
}
|
}
|
||||||
|
@ -180,7 +180,7 @@ fn convert_yaml_value_to_nu_value(
|
||||||
|
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Null => Value::nothing(span),
|
serde_yml::Value::Null => Value::nothing(span),
|
||||||
x => unimplemented!("Unsupported YAML case: {:?}", x),
|
x => unimplemented!("Unsupported YAML case: {:?}", x),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -188,9 +188,9 @@ fn convert_yaml_value_to_nu_value(
|
||||||
pub fn from_yaml_string_to_value(s: &str, span: Span, val_span: Span) -> Result<Value, ShellError> {
|
pub fn from_yaml_string_to_value(s: &str, span: Span, val_span: Span) -> Result<Value, ShellError> {
|
||||||
let mut documents = vec![];
|
let mut documents = vec![];
|
||||||
|
|
||||||
for document in serde_yaml::Deserializer::from_str(s) {
|
for document in serde_yml::Deserializer::from_str(s) {
|
||||||
let v: serde_yaml::Value =
|
let v: serde_yml::Value =
|
||||||
serde_yaml::Value::deserialize(document).map_err(|x| ShellError::UnsupportedInput {
|
serde_yml::Value::deserialize(document).map_err(|x| ShellError::UnsupportedInput {
|
||||||
msg: format!("Could not load YAML: {x}"),
|
msg: format!("Could not load YAML: {x}"),
|
||||||
input: "value originates from here".into(),
|
input: "value originates from here".into(),
|
||||||
msg_span: span,
|
msg_span: span,
|
||||||
|
@ -393,8 +393,8 @@ mod test {
|
||||||
];
|
];
|
||||||
|
|
||||||
for test_case in test_cases {
|
for test_case in test_cases {
|
||||||
let doc = serde_yaml::Deserializer::from_str(test_case.input);
|
let doc = serde_yml::Deserializer::from_str(test_case.input);
|
||||||
let v: serde_yaml::Value = serde_yaml::Value::deserialize(doc.last().unwrap()).unwrap();
|
let v: serde_yml::Value = serde_yml::Value::deserialize(doc.last().unwrap()).unwrap();
|
||||||
let result = convert_yaml_value_to_nu_value(&v, Span::test_data(), Span::test_data());
|
let result = convert_yaml_value_to_nu_value(&v, Span::test_data(), Span::test_data());
|
||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
assert!(result.ok().unwrap() == test_case.expected.ok().unwrap());
|
assert!(result.ok().unwrap() == test_case.expected.ok().unwrap());
|
||||||
|
|
|
@ -40,29 +40,24 @@ impl Command for ToYaml {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
pub fn value_to_yaml_value(v: &Value) -> Result<serde_yml::Value, ShellError> {
|
||||||
Ok(match &v {
|
Ok(match &v {
|
||||||
Value::Bool { val, .. } => serde_yaml::Value::Bool(*val),
|
Value::Bool { val, .. } => serde_yml::Value::Bool(*val),
|
||||||
Value::Int { val, .. } => serde_yaml::Value::Number(serde_yaml::Number::from(*val)),
|
Value::Int { val, .. } => serde_yml::Value::Number(serde_yml::Number::from(*val)),
|
||||||
Value::Filesize { val, .. } => {
|
Value::Filesize { val, .. } => serde_yml::Value::Number(serde_yml::Number::from(val.get())),
|
||||||
serde_yaml::Value::Number(serde_yaml::Number::from(val.get()))
|
Value::Duration { val, .. } => serde_yml::Value::String(val.to_string()),
|
||||||
}
|
Value::Date { val, .. } => serde_yml::Value::String(val.to_string()),
|
||||||
Value::Duration { val, .. } => serde_yaml::Value::String(val.to_string()),
|
Value::Range { .. } => serde_yml::Value::Null,
|
||||||
Value::Date { val, .. } => serde_yaml::Value::String(val.to_string()),
|
Value::Float { val, .. } => serde_yml::Value::Number(serde_yml::Number::from(*val)),
|
||||||
Value::Range { .. } => serde_yaml::Value::Null,
|
|
||||||
Value::Float { val, .. } => serde_yaml::Value::Number(serde_yaml::Number::from(*val)),
|
|
||||||
Value::String { val, .. } | Value::Glob { val, .. } => {
|
Value::String { val, .. } | Value::Glob { val, .. } => {
|
||||||
serde_yaml::Value::String(val.clone())
|
serde_yml::Value::String(val.clone())
|
||||||
}
|
}
|
||||||
Value::Record { val, .. } => {
|
Value::Record { val, .. } => {
|
||||||
let mut m = serde_yaml::Mapping::new();
|
let mut m = serde_yml::Mapping::new();
|
||||||
for (k, v) in &**val {
|
for (k, v) in &**val {
|
||||||
m.insert(
|
m.insert(serde_yml::Value::String(k.clone()), value_to_yaml_value(v)?);
|
||||||
serde_yaml::Value::String(k.clone()),
|
|
||||||
value_to_yaml_value(v)?,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
serde_yaml::Value::Mapping(m)
|
serde_yml::Value::Mapping(m)
|
||||||
}
|
}
|
||||||
Value::List { vals, .. } => {
|
Value::List { vals, .. } => {
|
||||||
let mut out = vec![];
|
let mut out = vec![];
|
||||||
|
@ -71,28 +66,28 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||||
out.push(value_to_yaml_value(value)?);
|
out.push(value_to_yaml_value(value)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
serde_yaml::Value::Sequence(out)
|
serde_yml::Value::Sequence(out)
|
||||||
}
|
}
|
||||||
Value::Closure { .. } => serde_yaml::Value::Null,
|
Value::Closure { .. } => serde_yml::Value::Null,
|
||||||
Value::Nothing { .. } => serde_yaml::Value::Null,
|
Value::Nothing { .. } => serde_yml::Value::Null,
|
||||||
Value::Error { error, .. } => return Err(*error.clone()),
|
Value::Error { error, .. } => return Err(*error.clone()),
|
||||||
Value::Binary { val, .. } => serde_yaml::Value::Sequence(
|
Value::Binary { val, .. } => serde_yml::Value::Sequence(
|
||||||
val.iter()
|
val.iter()
|
||||||
.map(|x| serde_yaml::Value::Number(serde_yaml::Number::from(*x)))
|
.map(|x| serde_yml::Value::Number(serde_yml::Number::from(*x)))
|
||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
Value::CellPath { val, .. } => serde_yaml::Value::Sequence(
|
Value::CellPath { val, .. } => serde_yml::Value::Sequence(
|
||||||
val.members
|
val.members
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| match &x {
|
.map(|x| match &x {
|
||||||
PathMember::String { val, .. } => Ok(serde_yaml::Value::String(val.clone())),
|
PathMember::String { val, .. } => Ok(serde_yml::Value::String(val.clone())),
|
||||||
PathMember::Int { val, .. } => {
|
PathMember::Int { val, .. } => {
|
||||||
Ok(serde_yaml::Value::Number(serde_yaml::Number::from(*val)))
|
Ok(serde_yml::Value::Number(serde_yml::Number::from(*val)))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<serde_yaml::Value>, ShellError>>()?,
|
.collect::<Result<Vec<serde_yml::Value>, ShellError>>()?,
|
||||||
),
|
),
|
||||||
Value::Custom { .. } => serde_yaml::Value::Null,
|
Value::Custom { .. } => serde_yml::Value::Null,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,9 +100,9 @@ fn to_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError>
|
||||||
let value = input.into_value(head)?;
|
let value = input.into_value(head)?;
|
||||||
|
|
||||||
let yaml_value = value_to_yaml_value(&value)?;
|
let yaml_value = value_to_yaml_value(&value)?;
|
||||||
match serde_yaml::to_string(&yaml_value) {
|
match serde_yml::to_string(&yaml_value) {
|
||||||
Ok(serde_yaml_string) => {
|
Ok(serde_yml_string) => {
|
||||||
Ok(Value::string(serde_yaml_string, head)
|
Ok(Value::string(serde_yml_string, head)
|
||||||
.into_pipeline_data_with_metadata(Some(metadata)))
|
.into_pipeline_data_with_metadata(Some(metadata)))
|
||||||
}
|
}
|
||||||
_ => Ok(Value::error(
|
_ => Ok(Value::error(
|
||||||
|
|
Loading…
Reference in a new issue