Config

Struct Config 

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

Configuration file source.

The Config struct represents a configuration file that can be loaded and parsed. It supports automatic format detection, optional files, and various configuration file formats (JSON, YAML, TOML).

§Examples

use gonfig::Config;

// Load a required configuration file
let config = Config::from_file("app.json")?;

// Load an optional configuration file (won't fail if missing)
let config = Config::from_file_optional("optional.yaml")?;

Implementations§

Source§

impl Config

Source

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

Load a required configuration file with automatic format detection.

The file format is detected from the file extension. If the file doesn’t exist or cannot be parsed, this method returns an error.

§Arguments
  • path - Path to the configuration file
§Examples
use gonfig::Config;

let config = Config::from_file("app.json")?;
let config = Config::from_file("settings.yaml")?;
let config = Config::from_file("config.toml")?;
§Errors
Source

pub fn from_file_optional(path: impl AsRef<Path>) -> Result<Self>

Load an optional configuration file with automatic format detection.

Similar to from_file, but won’t return an error if the file doesn’t exist. Parse errors will still cause the method to fail.

§Arguments
  • path - Path to the optional configuration file
§Examples
use gonfig::Config;

// Won't fail if user.json doesn't exist
let config = Config::from_file_optional("user.json")?;
Source

pub fn with_format(path: impl AsRef<Path>, format: ConfigFormat) -> Result<Self>

Load a configuration file with explicit format specification.

Use this method when you need to override automatic format detection or when working with files that don’t have standard extensions.

§Arguments
  • path - Path to the configuration file
  • format - The format to use for parsing
§Examples
use gonfig::{Config, ConfigFormat};

// Force JSON parsing for a file without extension
let config = Config::with_format("config", ConfigFormat::Json)?;
Source

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

Reload the configuration from disk.

This method re-reads the configuration file and parses it again. Useful for applications that need to respond to configuration changes at runtime.

§Examples
use gonfig::Config;

let mut config = Config::from_file("app.json")?;
// ... some time later ...
config.reload()?; // Re-read from disk
§Errors

Returns the same errors as the original loading method if the file cannot be read or parsed.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl ConfigSource for Config

Source§

fn source_type(&self) -> Source

Source§

fn collect(&self) -> Result<Value>

Source§

fn has_value(&self, key: &str) -> bool

Source§

fn get_value(&self, key: &str) -> Option<Value>

Source§

fn as_any(&self) -> &dyn Any

Source§

impl Debug for Config

Source§

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

Formats the value using the given formatter. Read more

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> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more