-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate intro and extract files iterator
- Loading branch information
Showing
3 changed files
with
79 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
█▀▄▀█ █▀▀ ▄▄ █▀█ █▀▀ █▀█ ▄▀█ █▀▀ █▄▀ by Szeweq | ||
█ ▀ █ █▄▄ █▀▄ ██▄ █▀▀ █▀█ █▄▄ █ █ (https://szeweq.xyz/mc-repack) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::{io, path::Path}; | ||
|
||
|
||
type FileResult = io::Result<(Option<bool>, Box<Path>)>; | ||
|
||
pub enum Files { | ||
Single(Option<FileResult>), | ||
Dir(std::vec::IntoIter<FileResult>) | ||
} | ||
impl Files { | ||
pub fn from_path(p: &Path) -> io::Result<(Box<Path>, Self)> { | ||
let ft = p.metadata()?.file_type(); | ||
let p: Box<Path> = Box::from(p); | ||
if ft.is_dir() { | ||
Ok((p.clone(), Self::Dir(walkdir::WalkDir::new(p).into_iter().map(|r| Ok(check_dir_entry(r?))).collect::<Vec<_>>().into_iter()))) | ||
} else if ft.is_file() { | ||
let parent = p.parent().unwrap(); | ||
Ok((parent.into(), Self::Single(Some(Ok((Some(false), p)))))) | ||
} else { | ||
Err(io::Error::new(io::ErrorKind::Other, "Not a file or directory")) | ||
} | ||
} | ||
} | ||
impl Iterator for Files { | ||
type Item = FileResult; | ||
fn next(&mut self) -> Option<Self::Item> { | ||
match self { | ||
Self::Single(it) => it.take(), | ||
Self::Dir(it) => it.next() | ||
} | ||
} | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
match self { | ||
Self::Single(Some(_)) => (1, Some(1)), | ||
Self::Single(None) => (0, Some(0)), | ||
Self::Dir(it) => it.size_hint() | ||
} | ||
} | ||
} | ||
impl ExactSizeIterator for Files { | ||
fn len(&self) -> usize { | ||
match self { | ||
Self::Single(Some(_)) => 1, | ||
Self::Single(None) => 0, | ||
Self::Dir(it) => it.len() | ||
} | ||
} | ||
} | ||
|
||
fn check_dir_entry(de: walkdir::DirEntry) -> (Option<bool>, Box<Path>) { | ||
let ft = de.file_type(); | ||
let p = de.into_path().into_boxed_path(); | ||
(if ft.is_dir() { | ||
Some(true) | ||
} else if ft.is_file() { | ||
Some(false) | ||
} else { | ||
None | ||
}, p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters