From a09241dc8940d0a9b01a698d3e71d08f71fadea4 Mon Sep 17 00:00:00 2001 From: John Turner Date: Fri, 12 Dec 2025 01:59:57 +0000 Subject: read repo_name when opening repos --- src/lib.rs | 3 ++- src/repo/mod.rs | 20 +++++++++++++++++--- src/repo/profile/mod.rs | 3 ++- 3 files changed, 21 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index b7b01a7..66cf2b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 750cd15..165f96a 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -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>(path: P) -> Self { - Self { + pub fn new>(path: P) -> Result { + 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 { diff --git a/src/repo/profile/mod.rs b/src/repo/profile/mod.rs index 8bdb724..28c6b06 100644 --- a/src/repo/profile/mod.rs +++ b/src/repo/profile/mod.rs @@ -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"); //! -- cgit v1.2.3