use std::{collections::VecDeque, sync::Arc};
use super::Metadata;
use crate::protocol::FileType;
#[derive(Debug)]
pub struct DirEntry {
parent: Arc<str>,
file: String,
metadata: Metadata,
}
impl DirEntry {
pub fn file_name(&self) -> String {
self.file.to_owned()
}
pub fn file_type(&self) -> FileType {
self.metadata.file_type()
}
pub fn metadata(&self) -> Metadata {
self.metadata.to_owned()
}
pub fn path(&self) -> String {
if self.parent.is_empty() {
self.file.clone()
} else if self.parent.ends_with('/') {
format!("{}{}", self.parent, self.file)
} else {
format!("{}/{}", self.parent, self.file)
}
}
}
pub struct ReadDir {
pub(crate) parent: Arc<str>,
pub(crate) entries: VecDeque<(String, Metadata)>,
}
impl Iterator for ReadDir {
type Item = DirEntry;
fn next(&mut self) -> Option<Self::Item> {
match self.entries.pop_front() {
None => None,
Some(entry) if entry.0 == "." || entry.0 == ".." => self.next(),
Some(entry) => Some(DirEntry {
parent: self.parent.clone(),
file: entry.0,
metadata: entry.1,
}),
}
}
}