Skip to main content

RepoFile

Struct RepoFile 

Source
pub struct RepoFile {
    pub preamble: Vec<String>,
    pub main: Option<SectionBlock<MainConfig>>,
    pub repos: IndexMap<RepoId, SectionBlock<Repo>>,
}
Expand description

A complete parsed .repo file.

Represents the entire INI document structure: a preamble (comments/lines before the first section), an optional [main] section, and zero or more [repo-id] repository sections.

§Examples

use dnf_repofile::RepoFile;

let input = "[main]\ncachedir=/var/cache/dnf\n\n[epel]\nname=EPEL\nbaseurl=https://example.com/\nenabled=1\n";
let rf = RepoFile::parse(input).unwrap();
assert_eq!(rf.len(), 1);
assert!(rf.main().is_some());

// Render back to string
let output = rf.render();
assert!(output.contains("[epel]"));
assert!(output.contains("[main]"));

Fields§

§preamble: Vec<String>

Lines appearing before the first section header (preamble comments/blanks).

§main: Option<SectionBlock<MainConfig>>

The optional [main] configuration section.

§repos: IndexMap<RepoId, SectionBlock<Repo>>

Repository sections keyed by RepoId.

Implementations§

Source§

impl RepoFile

Source

pub fn new() -> Self

Create an empty RepoFile with no sections.

§Examples
use dnf_repofile::RepoFile;

let rf = RepoFile::new();
assert!(rf.is_empty());
assert!(rf.main().is_none());
Source

pub fn parse(input: &str) -> Result<Self, ParseError>

Parse a .repo file string into a RepoFile.

Handles INI syntax with [section] headers, key=value pairs, # and ; comment lines, inline comments, and blank lines. The [main] section is parsed into a MainConfig, while other sections become Repo values.

§Errors

Returns ParseError for malformed input: invalid section headers, missing = separators, empty section names, or invalid repo IDs.

§Examples
use dnf_repofile::RepoFile;

let input = "[test]\nname=Test\nbaseurl=https://example.com/\n";
let rf = RepoFile::parse(input).unwrap();
assert_eq!(rf.len(), 1);
Source

pub fn render(&self) -> String

Render the RepoFile back to INI text.

Preserves comments, blank lines, and entry ordering from the original parse. Entries are rendered in their original order via the raw_entries recorded during parsing.

§Examples
use dnf_repofile::RepoFile;

let input = "[test]\nname=Test\nbaseurl=https://example.com/\n";
let rf = RepoFile::parse(input).unwrap();
let output = rf.render();
assert!(output.contains("[test]"));
assert!(output.contains("name=Test"));
Source

pub fn get(&self, id: &RepoId) -> Option<&SectionBlock<Repo>>

Get a reference to a repository section by RepoId.

Returns None if no repo with this ID exists.

§Examples
use dnf_repofile::{RepoFile, RepoId};

let rf = RepoFile::parse("[epel]\nname=EPEL\nbaseurl=https://example.com/\n").unwrap();
let block = rf.get(&RepoId::try_new("epel").unwrap()).unwrap();
assert_eq!(block.data.name.as_ref().unwrap().as_ref(), "EPEL");
Source

pub fn get_mut(&mut self, id: &RepoId) -> Option<&mut SectionBlock<Repo>>

Get a mutable reference to a repository section by RepoId.

Returns None if no repo with this ID exists.

§Examples
use dnf_repofile::{RepoFile, RepoId, DnfBool};

let mut rf = RepoFile::parse("[epel]\nname=EPEL\nbaseurl=https://example.com/\n").unwrap();
let block = rf.get_mut(&RepoId::try_new("epel").unwrap()).unwrap();
block.data.enabled = Some(DnfBool::False);
Source

pub fn add(&mut self, repo: Repo) -> Result<()>

Add a repository to the file.

The repo’s ID is used as the section name.

§Errors

Returns Error::DuplicateRepo if a repo with the same ID already exists.

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
let repo = Repo::new(RepoId::try_new("custom").unwrap());
rf.add(repo).unwrap();
assert_eq!(rf.len(), 1);
Source

pub fn set(&mut self, repo: Repo)

Insert or replace a repository by ID.

Unlike add, this will overwrite any existing repo with the same ID.

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
rf.set(Repo::new(RepoId::try_new("test").unwrap()));
assert_eq!(rf.len(), 1);
Source

pub fn remove(&mut self, id: &RepoId) -> Option<SectionBlock<Repo>>

Remove a repository by RepoId and return its section, if present.

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
rf.set(Repo::new(RepoId::try_new("test").unwrap()));
let removed = rf.remove(&RepoId::try_new("test").unwrap());
assert!(removed.is_some());
assert!(rf.is_empty());
Source

pub fn contains(&self, id: &RepoId) -> bool

Check if a repository with the given RepoId exists.

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
rf.set(Repo::new(RepoId::try_new("test").unwrap()));
assert!(rf.contains(&RepoId::try_new("test").unwrap()));
Source

pub fn len(&self) -> usize

Return the number of repository sections.

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
rf.set(Repo::new(RepoId::try_new("a").unwrap()));
rf.set(Repo::new(RepoId::try_new("b").unwrap()));
assert_eq!(rf.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if there are no repository sections.

§Examples
use dnf_repofile::RepoFile;

let rf = RepoFile::new();
assert!(rf.is_empty());
Source

pub fn iter(&self) -> impl Iterator<Item = (&RepoId, &SectionBlock<Repo>)>

Iterate over all (RepoId, SectionBlock<Repo>) pairs.

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
rf.set(Repo::new(RepoId::try_new("test").unwrap()));
for (id, _block) in rf.iter() {
    println!("Found repo: {}", id);
}
Source

pub fn repos(&self) -> impl Iterator<Item = &Repo>

Iterate over all Repo data values (wrapping SectionBlock).

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
rf.set(Repo::new(RepoId::try_new("test").unwrap()));
for repo in rf.repos() {
    println!("Repo ID: {}", repo.id);
}
Source

pub fn repo_ids(&self) -> impl Iterator<Item = &RepoId>

Iterate over all repository IDs.

§Examples
use dnf_repofile::{RepoFile, Repo, RepoId};

let mut rf = RepoFile::new();
rf.set(Repo::new(RepoId::try_new("test").unwrap()));
let ids: Vec<&RepoId> = rf.repo_ids().collect();
assert_eq!(ids.len(), 1);
Source

pub fn main(&self) -> Option<&SectionBlock<MainConfig>>

Get a reference to the [main] section, if present.

§Examples
use dnf_repofile::RepoFile;

let rf = RepoFile::parse("[main]\ncachedir=/var/cache/dnf\n").unwrap();
assert!(rf.main().is_some());
Source

pub fn main_mut(&mut self) -> Option<&mut SectionBlock<MainConfig>>

Get a mutable reference to the [main] section, if present.

§Examples
use dnf_repofile::RepoFile;

let mut rf = RepoFile::parse("[main]\ncachedir=/var/cache/dnf\n").unwrap();
if let Some(main) = rf.main_mut() {
    main.data.keepcache = Some(dnf_repofile::DnfBool::True);
}
Source

pub fn set_main(&mut self, config: MainConfig)

Set the [main] configuration, replacing any existing one.

§Examples
use dnf_repofile::{RepoFile, MainConfig};

let mut rf = RepoFile::new();
rf.set_main(MainConfig::default());
assert!(rf.main().is_some());
Source

pub fn remove_main(&mut self)

Remove the [main] section, if present.

§Examples
use dnf_repofile::{RepoFile, MainConfig};

let mut rf = RepoFile::new();
rf.set_main(MainConfig::default());
rf.remove_main();
assert!(rf.main().is_none());
Source

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

Merge another RepoFile into this one.

§[main] merge strategy

For each field in the other [main] section, if the other’s field is Some and self’s field is None, the value is copied over (i.e., other’s values fill self’s gaps). Other’s inline comments are also added if not already present.

§Repo merge strategy

For repo sections, the other’s repos overwrite self’s repos by ID. If a repo with the same ID already exists, the other’s version replaces it entirely. New repo IDs from the other file are appended.

§Examples
use dnf_repofile::RepoFile;

let mut rf = RepoFile::parse("[a]\nname=A\nbaseurl=https://a.com/\n").unwrap();
let other = RepoFile::parse("[b]\nname=B\nbaseurl=https://b.com/\n").unwrap();
rf.merge(other);
assert_eq!(rf.len(), 2);

Trait Implementations§

Source§

impl Clone for RepoFile

Source§

fn clone(&self) -> RepoFile

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RepoFile

Source§

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

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

impl Default for RepoFile

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for RepoFile

Source§

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

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

impl From<RepoFile> for String

Source§

fn from(rf: RepoFile) -> Self

Converts to this type from the input type.
Source§

impl FromStr for RepoFile

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<'a> IntoIterator for &'a RepoFile

Source§

type Item = (&'a RepoId, &'a SectionBlock<Repo>)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, RepoId, SectionBlock<Repo>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for RepoFile

Source§

fn eq(&self, other: &RepoFile) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for RepoFile

Source§

impl StructuralPartialEq for RepoFile

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.