Config

Struct Config 

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

A parsed Git configuration.

Git configuration files use an INI-like format with sections and key-value pairs. This struct provides read-only access to configuration values.

Implementations§

Source§

impl Config

Source

pub fn new() -> Self

Creates a new empty configuration.

Source

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

Parses configuration from a file.

§Arguments
  • path - Path to the configuration file.
§Returns

A Config instance, or an error if the file cannot be read or parsed.

Note: This method does not process include directives. Use [from_file_with_includes] to process includes.

Source

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

Parses configuration from a file, processing include directives.

This method handles Git’s include.path and includeIf directives, loading additional configuration files as specified.

§Arguments
  • path - Path to the configuration file.
§Returns

A Config instance with all included files merged.

Source

pub fn from_str(content: &str) -> Result<Self>

Parses configuration from a string.

§Arguments
  • content - The configuration file content.
§Returns

A Config instance, or an error if parsing fails.

Source

pub fn get(&self, section: &str, key: &str) -> Option<&str>

Gets a configuration value.

§Arguments
  • section - The section name (e.g., “user”, “core”).
  • key - The key name (e.g., “name”, “email”).
§Returns

The value if found, or None if the key doesn’t exist.

§Example
use zerogit::config::Config;

let config = Config::from_file(".git/config").unwrap();
if let Some(name) = config.get("user", "name") {
    println!("User: {}", name);
}
Source

pub fn get_subsection( &self, section: &str, subsection: &str, key: &str, ) -> Option<&str>

Gets a configuration value from a section with a subsection.

§Arguments
  • section - The section name (e.g., “remote”, “branch”).
  • subsection - The subsection name (e.g., “origin”, “main”).
  • key - The key name (e.g., “url”, “remote”).
§Returns

The value if found, or None if the key doesn’t exist.

§Example
use zerogit::config::Config;

let config = Config::from_file(".git/config").unwrap();
if let Some(url) = config.get_subsection("remote", "origin", "url") {
    println!("Origin URL: {}", url);
}
Source

pub fn get_bool(&self, section: &str, key: &str) -> Result<bool>

Gets a configuration value as a boolean.

Git config supports various boolean representations:

  • true, yes, on, 1 -> true
  • false, no, off, 0 -> false
§Arguments
  • section - The section name.
  • key - The key name.
§Returns

Ok(bool) if the value exists and is a valid boolean, Ok(false) if the key doesn’t exist, Err if the value is not a valid boolean.

Source

pub fn get_bool_subsection( &self, section: &str, subsection: &str, key: &str, ) -> Result<bool>

Gets a configuration value as a boolean from a section with a subsection.

Source

pub fn get_int(&self, section: &str, key: &str) -> Result<i64>

Gets a configuration value as an integer.

Git config supports optional suffixes:

  • k or K -> multiply by 1024
  • m or M -> multiply by 1024^2
  • g or G -> multiply by 1024^3
§Arguments
  • section - The section name.
  • key - The key name.
§Returns

Ok(i64) if the value exists and is a valid integer, Ok(0) if the key doesn’t exist, Err if the value is not a valid integer.

Source

pub fn get_int_subsection( &self, section: &str, subsection: &str, key: &str, ) -> Result<i64>

Gets a configuration value as an integer from a section with a subsection.

Source

pub fn sections(&self) -> Vec<&str>

Returns all section names in the configuration.

§Returns

A vector of section names (without subsections).

Source

pub fn subsections(&self, section: &str) -> Vec<&str>

Returns all subsections for a given section.

§Arguments
  • section - The section name.
§Returns

A vector of subsection names. Empty subsection (“”) is excluded.

Source

pub fn keys(&self, section: &str) -> Vec<&str>

Returns all keys in a section.

§Arguments
  • section - The section name.
§Returns

A vector of key names.

Source

pub fn keys_subsection(&self, section: &str, subsection: &str) -> Vec<&str>

Returns all keys in a section with a subsection.

Source

pub fn merge(&mut self, other: &Config)

Merges another configuration into this one.

Values from other will override values in self.

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

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. 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, 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.