improve repo error message by including the path in the error enum

This commit is contained in:
John Turner
2025-11-03 21:44:04 +00:00
parent 772e645066
commit 5fd26e7c81

View File

@@ -17,7 +17,9 @@ use crate::{
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
#[error("io error: {0}")] #[error("io error: {0}")]
Io(#[from] io::Error), Io(PathBuf, io::Error),
#[error("error while reading directory: {0:?}: {1}")]
ReadDir(PathBuf, io::Error),
#[error("failed to decode path: {0}")] #[error("failed to decode path: {0}")]
Unicode(PathBuf), Unicode(PathBuf),
#[error("parser error: {0}")] #[error("parser error: {0}")]
@@ -38,10 +40,10 @@ pub struct Category {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Categories(fs::ReadDir); pub struct Categories(PathBuf, fs::ReadDir);
#[derive(Debug)] #[derive(Debug)]
pub struct Ebuilds(fs::ReadDir); pub struct Ebuilds(PathBuf, fs::ReadDir);
impl Repo { impl Repo {
pub fn new<P: AsRef<Path>>(path: P) -> Self { pub fn new<P: AsRef<Path>>(path: P) -> Self {
@@ -51,15 +53,21 @@ impl Repo {
} }
pub fn categories(&self) -> Result<Categories, Error> { pub fn categories(&self) -> Result<Categories, Error> {
Ok(Categories(fs::read_dir( let path = self.path.as_path().join("metadata/md5-cache");
self.path.as_path().join("metadata/md5-cache"),
)?)) Ok(Categories(
path.clone(),
fs::read_dir(&path).map_err(|e| Error::Io(path, e))?,
))
} }
} }
impl Category { impl Category {
pub fn ebuilds(&self) -> Result<Ebuilds, Error> { pub fn ebuilds(&self) -> Result<Ebuilds, Error> {
Ok(Ebuilds(fs::read_dir(self.path.as_path())?)) Ok(Ebuilds(
self.path.clone(),
fs::read_dir(&self.path).map_err(|e| Error::Io(self.path.clone(), e))?,
))
} }
} }
@@ -67,12 +75,12 @@ impl Iterator for Categories {
type Item = Result<Category, Error>; type Item = Result<Category, Error>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self.0.next()? { match self.1.next()? {
Ok(entry) => match read_category(entry.path()) { Ok(entry) => match read_category(entry.path()) {
Ok(category) => Some(Ok(category)), Ok(category) => Some(Ok(category)),
Err(e) => Some(Err(e)), Err(e) => Some(Err(e)),
}, },
Err(e) => Some(Err(Error::Io(e))), Err(e) => Some(Err(Error::ReadDir(self.0.clone(), e))),
} }
} }
} }
@@ -81,7 +89,7 @@ impl Iterator for Ebuilds {
type Item = Result<Ebuild, Error>; type Item = Result<Ebuild, Error>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self.0.next()? { match self.1.next()? {
Ok(entry) => match read_ebuild(entry.path()) { Ok(entry) => match read_ebuild(entry.path()) {
Ok(ebuild) => Some(Ok(ebuild)), Ok(ebuild) => Some(Ok(ebuild)),
Err(e) => Some(Err(e)), Err(e) => Some(Err(e)),
@@ -119,7 +127,7 @@ fn read_ebuild(path: PathBuf) -> Result<Ebuild, Error> {
.parse_finished(InputIter::new(file_name)) .parse_finished(InputIter::new(file_name))
.map_err(|_| Error::Parser(file_name.to_string()))?; .map_err(|_| Error::Parser(file_name.to_string()))?;
let metadata = fs::read_to_string(path.as_path())?; let metadata = fs::read_to_string(path.as_path()).map_err(|e| Error::Io(path, e))?;
Ok(Ebuild { Ok(Ebuild {
name, name,