Skip to content

Conversation

redruin1
Copy link

Implements the proposed function. Needs testing, however.

initial implementation
* changed name to `read_dir` to mimic fs
* Now returns a `Vec<ModEntry>` structs, too dumb atm to figure out an iterator and the difference should be minimal for normal sized dirs
*  ModEntry has `is_file()` and `is_dir()` helpers, and most other convenient stuff can be grabbed from `path()`
* ModEntry can be easily expanded with additional metadata if desired
@redruin1
Copy link
Author

Notably, the function here returns a Result<Vec<ModEntry>>, where each ModEntry has a path() and is_file/dir() methods. This slightly changes the syntax from before; e.g:

let mod: Mod = ...;

let cfg_files = mod.read_dir("locale/en")?.iter()
    .filter(|&x| x.is_file() && x.path().extension() == Some("cfg"));

for cfg_file in cfg_files {
    let data = mod.read_file(&cfg_file.path())?;
    // ...
}

The returned value should probably be an iterator instead of a Vec, but I'm too smoothbrained with Rust to be able to do that at the moment.

@fgardt fgardt linked an issue Sep 19, 2025 that may be closed by this pull request
Comment on lines +182 to +184
pub fn path(&self) -> &PathBuf {
&self.path
}

Check warning

Code scanning / clippy

this could be a const fn Warning

this could be a const fn
}

impl ModEntry {
pub fn path(&self) -> &PathBuf {

Check warning

Code scanning / clippy

this method could have a #[must_use] attribute Warning

this method could have a #[must\_use] attribute
Comment on lines +185 to +187
pub fn is_file(&self) -> bool {
self.is_file
}

Check warning

Code scanning / clippy

this could be a const fn Warning

this could be a const fn
pub fn path(&self) -> &PathBuf {
&self.path
}
pub fn is_file(&self) -> bool {

Check warning

Code scanning / clippy

this method could have a #[must_use] attribute Warning

this method could have a #[must\_use] attribute
Comment on lines +188 to +190
pub fn is_dir(&self) -> bool {
!self.is_file
}

Check warning

Code scanning / clippy

this could be a const fn Warning

this could be a const fn
}

return std::fs::read_dir(&path)?
.filter_map(|x| x.ok())

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure

return std::fs::read_dir(&path)?
.filter_map(|x| x.ok())
.map(|x| ModEntry::try_from(x))

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure
let path = internal_prefix.clone() + dir;
let mut zip = zip.try_borrow_mut()?;

if let Err(_) = &zip.by_name(&path) {

Check warning

Code scanning / clippy

redundant pattern matching, consider using is_err() Warning

redundant pattern matching, consider using is\_err()
let entries: Vec<String> = zip
.file_names()
.filter(|&x| x.starts_with(&path) && x != path)
.map(|x| x.into())

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure
Comment on lines +345 to +348
return entries
.iter()
.map(|x| ModEntry::try_from(&zip.by_name(x)?))
.collect();

Check warning

Code scanning / clippy

unneeded return statement Warning

unneeded return statement
Copy link
Owner

@fgardt fgardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks nice for a start. One thought I have is that instead of returning a Vec<ModEntry> we could have a new struct like ModDir that then also implements Iterator so its usage would be very similar to the regular fs::read_dir.

@fgardt
Copy link
Owner

fgardt commented Sep 19, 2025

The CI test report failing is weird but not a blocker, the clippy lints would be nice to solve tho.

@fgardt
Copy link
Owner

fgardt commented Sep 19, 2025

The returned value should probably be an iterator instead of a Vec, but I'm too smoothbrained with Rust to be able to do that at the moment.

oh I just read this now, if you're fine with it I'll clean the PR up and change it into the iterator like I proposed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mod_util] Iterate over a Mod's folder
2 participants