Skip to content

Commit

Permalink
flake-info: Adapt RFC-166 style
Browse files Browse the repository at this point in the history
  • Loading branch information
dasJ committed Mar 29, 2024
1 parent 7081483 commit be4a444
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions flake-info/src/data/prettyprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,51 @@ fn print_value_indent(value: Value, indent: Indent) -> String {
if a.is_empty() {
return "[ ]".to_owned();
}
let items = a
.into_iter()
.map(|v| print_value_indent(v, indent.next()))
.collect::<Vec<_>>()
.join(&format!("\n{}", indent.next()));

return format!(
"[
if a.len() == 1 {
return format!("[ {} ]", print_value(a.first().unwrap().clone()));
} else {
let items = a
.into_iter()
.map(|v| print_value_indent(v, indent.next()))
.collect::<Vec<_>>()
.join(&format!("\n{}", indent.next()));

return format!(
"[
{next_indent}{items}
{indent}]",
indent = indent,
next_indent = indent.next(),
items = items
);
indent = indent,
next_indent = indent.next(),
items = items
);
}
}
Value::Object(o) => {
if o.is_empty() {
return "{ }".to_owned();
}
let items = o
.into_iter()
.map(|(k, v)| format!("{} = {}", k, print_value_indent(v, indent.next())))
.collect::<Vec<_>>()
.join(&format!(";\n{}", indent.next()));

return format!(
"{{
if o.len() == 1 {
return format!(
"{{ {} = {}; }}",
o.keys().next().unwrap(),
o.values().next().unwrap()
);
} else {
let items = o
.into_iter()
.map(|(k, v)| format!("{} = {}", k, print_value_indent(v, indent.next())))
.collect::<Vec<_>>()
.join(&format!(";\n{}", indent.next()));

return format!(
"{{
{next_indent}{items};
{indent}}}",
indent = indent,
next_indent = indent.next(),
items = items
);
indent = indent,
next_indent = indent.next(),
items = items
);
}
}
}
}
Expand Down Expand Up @@ -128,6 +140,12 @@ World
assert_eq!(print_value(json), "[ ]");
}

#[test]
fn test_list_one_item() {
let json = json!([1]);
assert_eq!(print_value(json), "[ 1 ]");
}

#[test]
fn test_filled_list() {
let json = json!([1, "hello", true, null]);
Expand All @@ -148,12 +166,19 @@ World
assert_eq!(print_value(json), "{ }");
}

#[test]
fn test_set_one_item() {
let json = json!({ "hello": "world" });
assert_eq!(print_value(json), "{ hello = \"world\"; }");
}

#[test]
fn test_filled_set() {
let json = json!({"hello": "world"});
let json = json!({"hello": "world", "another": "test"});
assert_eq!(
print_value(json),
"{
another = \"test\";
hello = \"world\";
}"
);
Expand Down

0 comments on commit be4a444

Please sign in to comment.