Repository navigation
Pretty Printing
MarshalIndent writes one element per line and nothing else. PrettyConfig gives you control over the layout, in the spirit of ron-rs's PrettyConfig: keep short lists on one line, limit how deep the output spreads out, escape non-ASCII text, number list elements, and choose the indent, line break and spacing.
out, err := rustyon.MarshalWithConfig(v, rustyon.PrettyConfig{CompactArrays: true, DepthLimit: 2})Whatever the configuration, the output parses back to the same value. That is why Prefix, Indent, NewLine and Separator may only contain spaces, tabs and line breaks.
All examples on this page use these types:
type Level struct {
Name string
Tiles []int
Spawn [2]int
Pos Vec
Props map[string]int
Nested Inner
}
type Vec struct {
_ struct{} `ron:",tuple"`
X, Y float64
}
type Inner struct {
Deep []int
Child struct{ A, B int }
}
l := Level{Name: "café", Tiles: []int{1, 2, 3}, Spawn: [2]int{4, 5}, Pos: Vec{X: 1, Y: 2.5},
Props: map[string]int{"a": 1, "b": 2}}
l.Nested.Deep = []int{9}
l.Nested.Child.A, l.Nested.Child.B = 1, 2| Function | Effect |
|---|---|
MarshalWithConfig(v any, cfg PrettyConfig) ([]byte, error) |
Like Marshal, laid out as cfg says. |
Encoder.SetPrettyConfig(cfg *PrettyConfig) |
Same for every following Encode. nil restores compact output. It replaces SetIndent, and SetIndent replaces it again. |
EncodeOptions{Pretty: &cfg} with MarshalWith
|
Combine a layout with field naming, struct names and the other options. |
PrettyConfig works for any value, including a Value tree. Extension headers of the encoder (implicit_some, unwrap_newtypes) are still written before the value:
var buf bytes.Buffer
enc := rustyon.NewEncoder(&buf)
enc.SetImplicitSome(true)
enc.SetPrettyConfig(&rustyon.PrettyConfig{CompactArrays: true, Indent: " "})
enc.Encode(struct{ A []int }{[]int{1, 2}})
fmt.Print(buf.String())#![enable(implicit_some)]
(
a: [1, 2],
)The zero value, PrettyConfig{}, is the layout of MarshalIndent(v, "", " ").
| Field | Default | Effect |
|---|---|---|
Prefix |
empty | Starts every line after the first, like the prefix of MarshalIndent. |
Indent |
four spaces | Written once per nesting level. |
NewLine |
"\n" |
Separates lines. With EnumerateArrays it must contain a line break. |
Separator |
one space | Written after the colon of a field or map entry. |
DepthLimit |
0 (no limit) | Collections nested more than DepthLimit levels deep are written on one line. With 1 only the outermost collection is spread over lines. |
CompactArrays |
false | Lists on one line: [1, 2, 3]. |
CompactStructs |
false | Structs on one line: (a: 1, b: 2). |
CompactMaps |
false | Maps on one line: {"a": 1, "b": 2}. |
SeparateTupleMembers |
false | Every member of a tuple on its own line. Without it, tuples of simple values stay on one line, as in MarshalIndent. |
EnumerateArrays |
false | A // [0] comment before every element of a list that is spread over several lines. |
EscapeNonASCII |
false | Non-ASCII characters in strings and chars are written as \u{e9} escapes. |
A collection that is written on one line is written on one line together with everything in it, with , between elements.
out, _ := rustyon.MarshalWithConfig(l, rustyon.PrettyConfig{})(
name: "café",
tiles: [
1,
2,
3,
],
spawn: [
4,
5,
],
pos: (1.0, 2.5),
props: {
"a": 1,
"b": 2,
},
nested: (
deep: [
9,
],
child: (
a: 1,
b: 2,
),
),
)rustyon.PrettyConfig{CompactArrays: true}(
name: "café",
tiles: [1, 2, 3],
spawn: [4, 5],
pos: (1.0, 2.5),
props: {
"a": 1,
"b": 2,
},
nested: (
deep: [9],
child: (
a: 1,
b: 2,
),
),
)CompactMaps: true changes only props:
props: {"a": 1, "b": 2},CompactStructs: true writes every struct on one line, and since everything inside a one line collection is on that line too, the whole document becomes one line:
(name: "café", tiles: [1, 2, 3], spawn: [4, 5], pos: (1.0, 2.5), props: {"a": 1, "b": 2}, nested: (deep: [9], child: (a: 1, b: 2)))rustyon.PrettyConfig{DepthLimit: 1}Only the outermost struct is spread over lines:
(
name: "café",
tiles: [1, 2, 3],
spawn: [4, 5],
pos: (1.0, 2.5),
props: {"a": 1, "b": 2},
nested: (deep: [9], child: (a: 1, b: 2)),
)With DepthLimit: 2, the level below the root still gets its own lines, and the collections inside it are inline:
(
name: "café",
tiles: [
1,
2,
3,
],
spawn: [
4,
5,
],
pos: (1.0, 2.5),
props: {
"a": 1,
"b": 2,
},
nested: (
deep: [9],
child: (a: 1, b: 2),
),
)By default pos: (1.0, 2.5) stays on one line. With SeparateTupleMembers: true:
pos: (
1.0,
2.5,
),rustyon.PrettyConfig{EnumerateArrays: true}(
name: "café",
tiles: [
// [0]
1,
// [1]
2,
// [2]
3,
],
spawn: [
// [0]
4,
// [1]
5,
],
pos: (1.0, 2.5),
props: {
"a": 1,
"b": 2,
},
nested: (
deep: [
// [0]
9,
],
child: (
a: 1,
b: 2,
),
),
)The comments appear only in lists that are spread over several lines. A list that is written inline, because of CompactArrays or DepthLimit, has none. The comments are ordinary RON comments, so the output still parses.
rustyon.PrettyConfig{EscapeNonASCII: true, CompactArrays: true, CompactMaps: true}(
name: "caf\u{e9}",
tiles: [1, 2, 3],
spawn: [4, 5],
pos: (1.0, 2.5),
props: {"a": 1, "b": 2},
nested: (
deep: [9],
child: (
a: 1,
b: 2,
),
),
)Useful when the file must stay pure ASCII.
rustyon.PrettyConfig{Indent: "\t", Separator: " ", CompactArrays: true}(
name: "café",
tiles: [1, 2, 3],
spawn: [4, 5],
pos: (1.0, 2.5),
props: {
"a": 1,
"b": 2,
},
nested: (
deep: [9],
child: (
a: 1,
b: 2,
),
),
)(Indentation above is one tab per level.) Prefix starts every line after the first, and NewLine can be "\r\n" for Windows line endings:
out, _ := rustyon.MarshalWithConfig([]int{1, 2}, rustyon.PrettyConfig{Prefix: " ", NewLine: "\r\n"})
fmt.Printf("%q\n", out)
// "[\r\n 1,\r\n 2,\r\n ]"Invalid settings are errors, not silently ignored:
for _, c := range []rustyon.PrettyConfig{
{Indent: "x"},
{Prefix: "> "},
{EnumerateArrays: true, NewLine: " "},
{DepthLimit: -1},
} {
_, err := rustyon.MarshalWithConfig(1, c)
fmt.Println(err)
}rustyon: PrettyConfig.Indent must consist of whitespace, got "x"
rustyon: PrettyConfig.Prefix must consist of whitespace, got "> "
rustyon: PrettyConfig.NewLine must contain a line break with EnumerateArrays, got " "
rustyon: PrettyConfig.DepthLimit must not be negative
ron-rs PrettyConfig
|
rustyon |
|---|---|
depth_limit |
DepthLimit |
new_line, indentor, separator
|
NewLine, Indent, Separator
|
separate_tuple_members |
SeparateTupleMembers |
enumerate_arrays |
EnumerateArrays |
compact_arrays, compact_structs, compact_maps
|
CompactArrays, CompactStructs, CompactMaps
|
extensions |
the encoder setters SetImplicitSome and SetUnwrapNewtypes, or the EncodeOptions fields of the same names |
struct_names |
Encoder.SetStructNames, EncodeOptions.StructNames
|
EscapeNonASCII has no counterpart in this table. Options that only make sense in Rust are not included.
PrettyConfig formats data you encode from Go values. To re-layout an existing .ron file and keep its comments, use Format. To change values in a file without touching the layout, use Editing.
rustyon is licensed under the BSD-3-Clause license. It is an independent implementation of RON and is not affiliated with the ron-rs project.