strict_path/path/virtual_path/iter.rs
1use super::VirtualPath;
2
3// ============================================================
4// VirtualReadDir — Iterator for validated virtual directory entries
5// ============================================================
6
7/// SUMMARY:
8/// Iterator over directory entries that yields validated `VirtualPath` values.
9///
10/// DETAILS:
11/// Created by `VirtualPath::virtual_read_dir()`. Each iteration automatically validates
12/// the directory entry through `virtual_join()`, so you get `VirtualPath` values directly
13/// instead of raw `std::fs::DirEntry` that would require manual re-validation.
14///
15/// EXAMPLE:
16/// ```rust
17/// # use strict_path::{VirtualRoot, VirtualPath};
18/// # let temp = tempfile::tempdir()?;
19/// # let vroot: VirtualRoot = VirtualRoot::try_new(temp.path())?;
20/// # let dir = vroot.virtual_join("assets")?;
21/// # dir.create_dir_all()?;
22/// # vroot.virtual_join("assets/logo.png")?.write(b"PNG")?;
23/// for entry in dir.virtual_read_dir()? {
24/// let child: VirtualPath = entry?;
25/// if child.is_file() {
26/// println!("File: {}", child.virtualpath_display());
27/// }
28/// }
29/// # Ok::<_, Box<dyn std::error::Error>>(())
30/// ```
31pub struct VirtualReadDir<'a, Marker> {
32 pub(super) inner: std::fs::ReadDir,
33 pub(super) parent: &'a VirtualPath<Marker>,
34}
35
36impl<Marker> std::fmt::Debug for VirtualReadDir<'_, Marker> {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 f.debug_struct("VirtualReadDir")
39 .field("parent", &self.parent.virtualpath_display().to_string())
40 .finish_non_exhaustive()
41 }
42}
43
44impl<Marker: Clone> Iterator for VirtualReadDir<'_, Marker> {
45 type Item = std::io::Result<VirtualPath<Marker>>;
46
47 fn next(&mut self) -> Option<Self::Item> {
48 match self.inner.next()? {
49 Ok(entry) => {
50 let file_name = entry.file_name();
51 match self.parent.virtual_join(file_name) {
52 Ok(virtual_path) => Some(Ok(virtual_path)),
53 Err(e) => Some(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e))),
54 }
55 }
56 Err(e) => Some(Err(e)),
57 }
58 }
59}