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
impl Config
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self>
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
Error::Configif the file extension is not recognizedError::Ioif the file cannot be readError::Serializationif the file cannot be parsed
Sourcepub fn from_file_optional(path: impl AsRef<Path>) -> Result<Self>
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")?;Sourcepub fn with_format(path: impl AsRef<Path>, format: ConfigFormat) -> Result<Self>
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 fileformat- 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)?;Sourcepub fn reload(&mut self) -> Result<()>
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.