Skip to main content

ConfigLoader

Struct ConfigLoader 

Source
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

Source

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");
Source

pub const fn with_base_dir(base_dir: PathBuf) -> Self

Creates a ConfigLoader with a custom base directory.

§Arguments
  • base_dir - The base directory for TxGate files
§Examples
use txgate_core::config_loader::ConfigLoader;
use std::path::PathBuf;

let loader = ConfigLoader::with_base_dir(PathBuf::from("/custom/txgate"));
Source

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"
Source

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"
Source

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");
Source

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),
}
Source

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");
Source

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");
Source

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

Source§

fn clone(&self) -> ConfigLoader

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 Debug for ConfigLoader

Source§

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

Formats the value using the given formatter. Read more

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