read repo_name when opening repos

This commit is contained in:
2025-12-12 01:59:57 +00:00
parent 682cfdac9f
commit a09241dc89
7 changed files with 25 additions and 9 deletions

View File

@@ -20,7 +20,7 @@ fn main() -> Result<(), Box<dyn Error>> {
fs::create_dir_all(&corpus_dir)?;
let repo = Repo::new("/var/db/repos/gentoo");
let repo = Repo::new("/var/db/repos/gentoo").expect("failed to open repo");
let mut atoms = Vec::new();
for category in repo.categories()? {

View File

@@ -17,7 +17,7 @@ fn main() -> Result<(), Box<dyn Error>> {
fs::create_dir_all(&corpus_dir)?;
let repo = Repo::new("/var/db/repos/gentoo");
let repo = Repo::new("/var/db/repos/gentoo").expect("failed to open repo");
let mut versions = Vec::new();
for category in repo.categories()? {

View File

@@ -69,7 +69,8 @@ pub mod atom;
/// ```
/// use gentoo_utils::repo::Repo;
///
/// let repo = Repo::new("/var/db/repos/gentoo");
/// let repo = Repo::new("/var/db/repos/gentoo")
/// .expect("failed to open repo");
///
/// for result in repo.categories().expect("failed to read categories") {
/// let category = result.expect("failed to read category");

View File

@@ -23,6 +23,8 @@ pub mod profile;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid repo: {0}")]
Invalid(String),
#[error("io error: {0}")]
Io(PathBuf, io::Error),
#[error("error while reading directory: {0:?}: {1}")]
@@ -39,6 +41,8 @@ pub enum Error {
pub struct Repo {
#[get(kind = "deref")]
path: PathBuf,
#[get(kind = "deref")]
name: String,
}
#[derive(Debug, Clone, Get)]
@@ -55,10 +59,20 @@ pub struct Categories(PathBuf, fs::ReadDir);
pub struct Ebuilds(PathBuf, fs::ReadDir);
impl Repo {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
Self {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let name_path = path.as_ref().join("profiles/repo_name");
let name = match fs::read_to_string(&name_path) {
Ok(repo_name) => repo_name,
Err(e) if matches!(e.kind(), io::ErrorKind::NotFound) => {
return Err(Error::Invalid("missing repo_name".to_string()));
}
Err(e) => return Err(Error::Io(name_path, e)),
};
Ok(Self {
path: path.as_ref().to_path_buf(),
}
name,
})
}
pub fn categories(&self) -> Result<Categories, Error> {

View File

@@ -2,7 +2,8 @@
//! ```rust
//! use gentoo_utils::repo::Repo;
//!
//! let repo = Repo::new("/var/db/repos/gentoo");
//! let repo = Repo::new("/var/db/repos/gentoo")
//! .expect("failed to open repo");
//! let profile = repo.evaluate_profile("default/linux/23.0")
//! .expect("failed to evaluate profile");
//!

View File

@@ -474,7 +474,7 @@ fn main() {
"prefix/sunos/solaris/5.11/x64",
];
let repo = Repo::new("/var/db/repos/gentoo");
let repo = Repo::new("/var/db/repos/gentoo").expect("failed to open repo");
for profile in profiles {
repo.evaluate_profile(profile)

View File

@@ -3,7 +3,7 @@ use std::error::Error;
use gentoo_utils::repo::Repo;
fn main() -> Result<(), Box<dyn Error>> {
let repo = Repo::new("/var/db/repos/gentoo");
let repo = Repo::new("/var/db/repos/gentoo").unwrap();
for result in repo.categories()? {
let cat = result?;