Skip to main content

ConfigFile

Struct ConfigFile 

Source
pub struct ConfigFile {
    pub path: PathBuf,
    pub scope: ConfigScope,
    pub entries: Vec<ConfigEntry>,
    pub include_origin: ConfigIncludeOrigin,
    /* private fields */
}
Expand description

A parsed configuration file that preserves the raw text for round-trip editing (set/unset/rename-section/remove-section).

Fields§

§path: PathBuf

The path to this config file on disk.

§scope: ConfigScope

The scope this file represents.

§entries: Vec<ConfigEntry>

Parsed entries (in file order).

§include_origin: ConfigIncludeOrigin

Source kind for [include] resolution (Git CONFIG_ORIGIN_*).

Implementations§

Source§

impl ConfigFile

Source

pub fn parse(path: &Path, content: &str, scope: ConfigScope) -> Result<Self>

Parse a config file from its raw text content.

§Parameters
  • path — the file path (stored for diagnostics and round-trip writes).
  • content — the raw text of the file.
  • scope — the ConfigScope this file represents.
§Errors

Returns Error::ConfigError on malformed input.

Source

pub fn parse_with_origin( path: &Path, content: &str, scope: ConfigScope, include_origin: ConfigIncludeOrigin, ) -> Result<Self>

Parse like Self::parse but record a non-disk include origin (blob, stdin, command line).

Source

pub fn from_git_config_parameters(path: &Path, raw: &str) -> Result<Self>

Build a synthetic ConfigFile from GIT_CONFIG_PARAMETERS / git -c payloads.

Unlike Self::parse, this accepts flat key=value assignments without [section] headers, matching how Git injects command-line configuration.

Source

pub fn from_path(path: &Path, scope: ConfigScope) -> Result<Option<Self>>

Read and parse a config file from disk.

Returns Ok(None) if the file does not exist.

§Errors

Returns Error::Io on read failure (other than not-found) or Error::ConfigError on parse failure.

Source

pub fn set(&mut self, key: &str, value: &str) -> Result<()>

Set a value in this config file, creating the section if needed.

If the key already exists, its last occurrence is updated in-place. Otherwise a new entry is appended (creating the section header if necessary).

§Parameters
  • key — canonical key (e.g. core.bare).
  • value — the value to set.
Source

pub fn set_with_comment( &mut self, key: &str, value: &str, comment: Option<&str>, ) -> Result<()>

Set a value in this config file, optionally appending an inline comment.

Source

pub fn replace_all( &mut self, key: &str, value: &str, value_pattern: Option<&str>, ) -> Result<()>

Replace ALL occurrences of a key with a new value.

Removes all but the last occurrence from the file, then updates the last occurrence with the new value (matching Git behaviour).

Source

pub fn replace_all_with_comment( &mut self, key: &str, value: &str, value_pattern: Option<&str>, comment: Option<&str>, ) -> Result<()>

Replace all occurrences, optionally appending an inline comment.

Value patterns starting with ! are treated as negated regex (matching values that do NOT match the pattern).

Source

pub fn count(&self, key: &str) -> Result<usize>

Count how many entries exist for a key.

Source

pub fn unset_last(&mut self, key: &str) -> Result<usize>

Unset (remove) only the last occurrence of a key.

Returns the number of entries removed (0 or 1).

Source

pub fn unset(&mut self, key: &str) -> Result<usize>

Unset (remove) all occurrences of a key.

§Parameters
  • key — canonical key (e.g. core.bare).
§Returns

The number of entries removed.

Source

pub fn unset_matching( &mut self, key: &str, value_pattern: Option<&str>, preserve_empty_section_header: bool, ) -> Result<usize>

Unset entries matching a key and optional value-pattern regex.

If value_pattern is None, removes all entries with the given key. If value_pattern is Some(pat), only removes entries whose value matches the regex.

When preserve_empty_section_header is true, a section header is kept even if the section has no remaining keys (Git’s config unset --all). When false, empty sections are stripped (config --unset, config --unset-all, and value-pattern unsets).

Source

pub fn remove_section(&mut self, section: &str) -> Result<bool>

Remove an entire section (and all its entries).

§Parameters
  • section — section name (e.g. "core", "remote.origin").
Source

pub fn rename_section(&mut self, old_name: &str, new_name: &str) -> Result<bool>

Rename a section.

§Parameters
  • old_name — current section name (e.g. "branch.main").
  • new_name — new section name (e.g. "branch.develop").
Source

pub fn add_value(&mut self, key: &str, value: &str) -> Result<()>

Append a new value for a key without removing existing entries.

This is the behaviour of git config --add section.key value. If the section doesn’t exist, it is created.

Source

pub fn add_value_with_comment( &mut self, key: &str, value: &str, comment: Option<&str>, ) -> Result<()>

Append a new value with an optional inline comment.

Source

pub fn write(&self) -> Result<()>

§Errors

Returns Error::Io on write failure.

Trait Implementations§

Source§

impl Clone for ConfigFile

Source§

fn clone(&self) -> ConfigFile

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 ConfigFile

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

Source§

type Output = T

Should always be Self
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.