forked from extern/nushell
Add escapes to 'to nuon' (#4964)
This commit is contained in:
parent
12b85beecc
commit
91cd1717e9
@ -137,10 +137,15 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
|
|||||||
}
|
}
|
||||||
Ok(format!("{{{}}}", collection.join(", ")))
|
Ok(format!("{{{}}}", collection.join(", ")))
|
||||||
}
|
}
|
||||||
Value::String { val, .. } => Ok(format!("\"{}\"", val)),
|
Value::String { val, .. } => Ok(format!("\"{}\"", escape(val))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn escape(input: &str) -> String {
|
||||||
|
let output = input.replace('\\', "\\\\");
|
||||||
|
output.replace('"', "\\\"")
|
||||||
|
}
|
||||||
|
|
||||||
fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
|
fn to_nuon(call: &Call, input: PipelineData) -> Result<String, ShellError> {
|
||||||
let v = input.into_value(call.head);
|
let v = input.into_value(call.head);
|
||||||
|
|
||||||
|
@ -74,6 +74,34 @@ fn to_nuon_bool() {
|
|||||||
assert_eq!(actual.out, "false");
|
assert_eq!(actual.out, "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_nuon_escaping() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
"hello\"world"
|
||||||
|
| to nuon
|
||||||
|
| from nuon
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "hello\"world");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_nuon_escaping2() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
"hello\\world"
|
||||||
|
| to nuon
|
||||||
|
| from nuon
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "hello\\world");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn to_nuon_negative_int() {
|
fn to_nuon_negative_int() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
|
Loading…
Reference in New Issue
Block a user