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
impl RepoFile
Sourcepub fn parse(input: &str) -> Result<Self, ParseError>
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);Sourcepub fn render(&self) -> String
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"));Sourcepub fn get(&self, id: &RepoId) -> Option<&SectionBlock<Repo>>
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");Sourcepub fn get_mut(&mut self, id: &RepoId) -> Option<&mut SectionBlock<Repo>>
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);Sourcepub fn add(&mut self, repo: Repo) -> Result<()>
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);Sourcepub fn remove(&mut self, id: &RepoId) -> Option<SectionBlock<Repo>>
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());Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn iter(&self) -> impl Iterator<Item = (&RepoId, &SectionBlock<Repo>)>
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);
}Sourcepub fn repo_ids(&self) -> impl Iterator<Item = &RepoId>
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);Sourcepub fn main(&self) -> Option<&SectionBlock<MainConfig>>
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());Sourcepub fn main_mut(&mut self) -> Option<&mut SectionBlock<MainConfig>>
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);
}Sourcepub fn set_main(&mut self, config: MainConfig)
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());Sourcepub fn remove_main(&mut self)
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());Sourcepub fn merge(&mut self, other: RepoFile)
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<'a> IntoIterator for &'a RepoFile
impl<'a> IntoIterator for &'a RepoFile
impl Eq for RepoFile
impl StructuralPartialEq for RepoFile
Auto Trait Implementations§
impl Freeze for RepoFile
impl RefUnwindSafe for RepoFile
impl Send for RepoFile
impl Sync for RepoFile
impl Unpin for RepoFile
impl UnsafeUnpin for RepoFile
impl UnwindSafe for RepoFile
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.