use crate::*;
#[derive(Debug, Clone)]
pub struct Entry {
meta: Metadata,
}
impl PartialEq for Entry {
fn eq(&self, other: &Self) -> bool {
self.path() == other.path() && self.meta == other.meta
}
}
impl Eq for Entry {}
impl Entry {
pub fn new(path: &str, meta: Metadata) -> Entry {
Self::with(path.to_string(), meta)
}
pub fn with(mut path: String, meta: Metadata) -> Entry {
if path.is_empty() {
path = "/".to_string();
}
debug_assert!(
meta.mode().is_dir() == path.ends_with('/'),
"mode {:?} not match with path {}",
meta.mode(),
path
);
let mut builder = meta.into_builder();
builder.path(path);
Entry {
meta: builder.build(),
}
}
pub fn set_path(&mut self, path: &str) -> &mut Self {
let mut builder = self.meta.clone().into_builder();
builder.path(path);
self.meta = builder.build();
self
}
pub fn path(&self) -> &str {
self.meta
.path()
.expect("listed entry metadata contains its path")
}
pub fn mode(&self) -> EntryMode {
self.meta.mode()
}
pub(crate) fn into_entry(self) -> crate::Entry {
crate::Entry::from_metadata(self.meta)
}
pub fn metadata(&self) -> &Metadata {
&self.meta
}
pub fn into_parts(self) -> (String, Metadata) {
(self.path().to_string(), self.meta)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entry_equality_includes_path() {
let metadata = MetadataBuilder::file(0).build();
assert_ne!(
Entry::new("first", metadata.clone()),
Entry::new("second", metadata)
);
}
}