pub struct ConfigLoader { /* private fields */ }Expand description
Configuration loader that handles reading and writing configuration files.
The ConfigLoader manages configuration file operations including:
- Loading configuration from TOML files
- Saving configuration to TOML files
- Creating default configuration files
- Path expansion for
~(home directory)
§Examples
use txgate_core::config_loader::ConfigLoader;
// Create with default base directory (~/.txgate)
let loader = ConfigLoader::new().expect("failed to create loader");
// Load configuration (returns defaults if file doesn't exist)
let config = loader.load().expect("failed to load config");Implementations§
Source§impl ConfigLoader
impl ConfigLoader
Sourcepub fn new() -> Result<Self, ConfigError>
pub fn new() -> Result<Self, ConfigError>
Creates a new ConfigLoader with the default base directory (~/.txgate).
§Errors
Returns ConfigError::NoHomeDirectory if the home directory cannot be determined.
§Examples
use txgate_core::config_loader::ConfigLoader;
let loader = ConfigLoader::new().expect("failed to create loader");Sourcepub const fn with_base_dir(base_dir: PathBuf) -> Self
pub const fn with_base_dir(base_dir: PathBuf) -> Self
Sourcepub fn config_path(&self) -> PathBuf
pub fn config_path(&self) -> PathBuf
Returns the path to the configuration file.
§Examples
use txgate_core::config_loader::ConfigLoader;
let loader = ConfigLoader::new().expect("failed to create loader");
let path = loader.config_path();
// path is something like "/home/user/.txgate/config.toml"Sourcepub fn base_dir(&self) -> &Path
pub fn base_dir(&self) -> &Path
Returns the base directory for TxGate files.
§Examples
use txgate_core::config_loader::ConfigLoader;
let loader = ConfigLoader::new().expect("failed to create loader");
let base = loader.base_dir();
// base is something like "/home/user/.txgate"Sourcepub fn load(&self) -> Result<Config, ConfigError>
pub fn load(&self) -> Result<Config, ConfigError>
Loads configuration from the file.
If the configuration file doesn’t exist, returns the default configuration. If the file exists but contains invalid TOML, returns a parse error.
§Errors
Returns ConfigError::ParseFailed if the file contains invalid TOML.
Returns ConfigError::Io if there’s an I/O error reading the file.
§Examples
use txgate_core::config_loader::ConfigLoader;
let loader = ConfigLoader::new().expect("failed to create loader");
let config = loader.load().expect("failed to load config");Sourcepub fn load_required(&self) -> Result<Config, ConfigError>
pub fn load_required(&self) -> Result<Config, ConfigError>
Loads configuration from the file, failing if the file doesn’t exist.
Unlike load, this method returns an error if the
configuration file is not found.
§Errors
Returns ConfigError::FileNotFound if the configuration file doesn’t exist.
Returns ConfigError::ParseFailed if the file contains invalid TOML.
Returns ConfigError::Io if there’s an I/O error reading the file.
§Examples
use txgate_core::config_loader::ConfigLoader;
let loader = ConfigLoader::new().expect("failed to create loader");
match loader.load_required() {
Ok(config) => println!("Config loaded: {:?}", config),
Err(e) => eprintln!("Config required but not found: {}", e),
}Sourcepub fn save(&self, config: &Config) -> Result<(), ConfigError>
pub fn save(&self, config: &Config) -> Result<(), ConfigError>
Saves configuration to the file.
Creates the base directory if it doesn’t exist.
§Errors
Returns ConfigError::Io if there’s an I/O error writing the file.
Returns ConfigError::ParseFailed if the configuration cannot be serialized.
§Examples
use txgate_core::config_loader::ConfigLoader;
use txgate_core::config::Config;
let loader = ConfigLoader::new().expect("failed to create loader");
let config = Config::builder()
.timeout_secs(60)
.build();
loader.save(&config).expect("failed to save config");Sourcepub fn write_default(&self) -> Result<(), ConfigError>
pub fn write_default(&self) -> Result<(), ConfigError>
Writes the default configuration file.
Creates the base directory if it doesn’t exist.
Uses the formatted default TOML from Config::default_toml.
§Errors
Returns ConfigError::Io if there’s an I/O error writing the file.
§Examples
use txgate_core::config_loader::ConfigLoader;
let loader = ConfigLoader::new().expect("failed to create loader");
loader.write_default().expect("failed to write default config");Sourcepub fn exists(&self) -> bool
pub fn exists(&self) -> bool
Checks if the configuration file exists.
§Examples
use txgate_core::config_loader::ConfigLoader;
let loader = ConfigLoader::new().expect("failed to create loader");
if loader.exists() {
println!("Config file found");
} else {
println!("Config file not found, will use defaults");
}Trait Implementations§
Source§impl Clone for ConfigLoader
impl Clone for ConfigLoader
Source§fn clone(&self) -> ConfigLoader
fn clone(&self) -> ConfigLoader
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more