Config

Struct Config 

Source
pub struct Config { /* private fields */ }
Expand description

High-level configuration manager with format preservation and change tracking

Config provides a comprehensive API for managing configurations throughout their lifecycle. It maintains both the resolved values (for fast access) and format-specific preservation data (for round-trip editing).

§Key Features

  • Format Preservation: Maintains comments, whitespace, and original formatting
  • Change Tracking: Automatic detection of modifications
  • Type Safety: Rich type conversion with comprehensive error handling
  • Path-based Access: Dot notation for nested value access
  • Multi-format Support: CONF, TOML, JSON, NOML formats
  • Schema Validation: Optional schema validation and enforcement
  • Async Support: Non-blocking file operations (with feature flag)

§Examples

use config_lib::Config;

// Load from string
let mut config = Config::from_string("port = 8080\nname = \"MyApp\"", None)?;

// Access values
let port = config.get("port").unwrap().as_integer()?;
let name = config.get("name").unwrap().as_string()?;

// Modify values
config.set("port", 9000)?;

Implementations§

Source§

impl Config

Source

pub fn new() -> Self

Create a new empty configuration

Source

pub fn from_string(source: &str, format: Option<&str>) -> Result<Self>

Load configuration from a string

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self>

Load configuration from a file

Source

pub fn get(&self, path: &str) -> Option<&Value>

Get a value by path

Source

pub fn get_mut(&mut self, path: &str) -> Result<&mut Value>

Get a mutable reference to a value by path

Source

pub fn set<V: Into<Value>>(&mut self, path: &str, value: V) -> Result<()>

Set a value by path

Source

pub fn remove(&mut self, path: &str) -> Result<Option<Value>>

Remove a value by path

Source

pub fn contains_key(&self, path: &str) -> bool

Check if a path exists

Source

pub fn keys(&self) -> Result<Vec<&str>>

Get all keys in the configuration

Source

pub fn is_modified(&self) -> bool

Check if the configuration has been modified

Source

pub fn mark_clean(&mut self)

Mark the configuration as unmodified

Source

pub fn format(&self) -> &str

Get the configuration format

Source

pub fn file_path(&self) -> Option<&Path>

Get the file path (if loaded from file)

Source

pub fn save(&mut self) -> Result<()>

Save the configuration to its original file

Source

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>

Save the configuration to a specific file

Source

pub fn serialize(&self) -> Result<String>

Serialize the configuration to string format

Source

pub fn as_value(&self) -> &Value

Get the underlying Value

Source

pub fn merge(&mut self, other: &Config) -> Result<()>

Merge another configuration into this one

Source

pub fn key(&self, path: &str) -> ConfigValue<'_>

Get a value by path with a more ergonomic API

Source

pub fn has(&self, path: &str) -> bool

Check if configuration has any value at the given path

Source

pub fn get_or<V>(&self, path: &str, default: V) -> V
where V: TryFrom<Value> + Clone, V::Error: Debug,

Get a value with a default fallback

Trait Implementations§

Source§

impl Default for Config

Source§

fn default() -> Self

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

impl From<Value> for Config

Convert Value to Config

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.