pub struct WriteOptions { /* private fields */ }Expand description
Output formatting options, mirroring the writer options of the C API
(config_set_options, config_set_float_precision,
config_set_tab_width — manual chapter 3). Every combination produces
grammar-valid output; the options only choose among equivalent spellings.
The defaults keep this crate’s canonical output: = assignments,
semicolon terminators, opening braces on the same line, 2-space
indentation, and floats in the shortest decimal form that round-trips
exactly. (The C library’s writer defaults differ cosmetically: it uses
: for groups, puts { on its own line, and writes floats with 6
fixed digits.)
§Examples
use libconfig::{Config, WriteOptions};
let config = Config::from_str("port = 80; db = { host = \"h\"; };").unwrap();
let opts = WriteOptions::new()
.colon_assignment_for_groups(true)
.tab_width(4);
let text = config.to_string_with_options(&opts).unwrap();
assert!(text.contains("db :"));
assert!(text.contains(" host"));Implementations§
Source§impl WriteOptions
impl WriteOptions
Sourcepub fn float_precision(self, digits: u8) -> Self
pub fn float_precision(self, digits: u8) -> Self
Writes floats with a fixed number of digits after the decimal point
(the C API’s config_set_float_precision; clamped to 17). The
default is the shortest representation that round-trips exactly.
A decimal point is always kept so values re-read as floats, even
with a precision of 0.
Sourcepub fn scientific_notation(self, enabled: bool) -> Self
pub fn scientific_notation(self, enabled: bool) -> Self
When enabled, floats are written in exponent notation (2.5e3),
like the C API’s CONFIG_OPTION_ALLOW_SCIENTIFIC_NOTATION.
Default: off.
Sourcepub fn tab_width(self, width: u8) -> Self
pub fn tab_width(self, width: u8) -> Self
Spaces of indentation per nesting level, or a single tab character
per level when 0 (the C API’s config_set_tab_width; clamped to 15).
Default: 2.
Sourcepub fn semicolon_separators(self, enabled: bool) -> Self
pub fn semicolon_separators(self, enabled: bool) -> Self
Whether settings are terminated with ; (the grammar makes the
terminator optional). Default: on.
Sourcepub fn colon_assignment_for_groups(self, enabled: bool) -> Self
pub fn colon_assignment_for_groups(self, enabled: bool) -> Self
Writes name : { ... } instead of name = { ... } for group-valued
settings (= and : are interchangeable per the grammar).
Default: off (the C library defaults to on).
Sourcepub fn colon_assignment_for_non_groups(self, enabled: bool) -> Self
pub fn colon_assignment_for_non_groups(self, enabled: bool) -> Self
Writes name : value instead of name = value for non-group
settings. Default: off.
Sourcepub fn open_brace_on_separate_line(self, enabled: bool) -> Self
pub fn open_brace_on_separate_line(self, enabled: bool) -> Self
Writes the opening { of a group on its own line. Default: off
(the C library defaults to on).