Skip to main content

WriteOptions

Struct WriteOptions 

Source
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

Source

pub fn new() -> Self

Creates the default options (see the type-level documentation).

Source

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.

Source

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.

Source

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.

Source

pub fn semicolon_separators(self, enabled: bool) -> Self

Whether settings are terminated with ; (the grammar makes the terminator optional). Default: on.

Source

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).

Source

pub fn colon_assignment_for_non_groups(self, enabled: bool) -> Self

Writes name : value instead of name = value for non-group settings. Default: off.

Source

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).

Trait Implementations§

Source§

impl Clone for WriteOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WriteOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WriteOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for WriteOptions

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for WriteOptions

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.