simplify iterator items

This commit is contained in:
Alexander Weiss 2023-03-20 22:37:42 +01:00
parent f4896e7e57
commit 82c545e6d1
4 changed files with 20 additions and 30 deletions

View File

@ -46,7 +46,7 @@ impl<BE: DecryptWriteBackend, I: IndexedBackend> FileArchiver<BE, I> {
Ok(match item {
TreeType::NewTree(item) => TreeType::NewTree(item),
TreeType::EndTree => TreeType::EndTree,
TreeType::Other((path, node, open, parent)) => {
TreeType::Other((path, node, (open, parent))) => {
let (node, filesize) = if let ParentResult::Matched(()) = parent {
let size = node.meta.size;
p.inc(size);
@ -57,7 +57,7 @@ impl<BE: DecryptWriteBackend, I: IndexedBackend> FileArchiver<BE, I> {
} else {
(node, 0)
};
TreeType::Other((path, node, parent, filesize))
TreeType::Other((path, node, (parent, filesize)))
}
})
}

View File

@ -1,6 +1,5 @@
use std::cmp::Ordering;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
use anyhow::{anyhow, Result};
use log::warn;
@ -35,20 +34,9 @@ impl<T> ParentResult<T> {
Self::NotMatched => ParentResult::NotMatched,
}
}
pub fn into_type_only(self) -> ParentResult<()> {
self.map(|_| ())
}
}
impl ParentResult<&Node> {
pub fn into_tree_id(self) -> ParentResult<Id> {
self.map(|node| node.subtree().unwrap())
}
}
pub type ItemWithParent<O> =
TreeType<(PathBuf, Node, O, ParentResult<()>), (PathBuf, Node, ParentResult<Id>)>;
pub type ItemWithParent<O> = TreeType<(O, ParentResult<()>), ParentResult<Id>>;
impl<BE: IndexedBackend> Parent<BE> {
pub fn new(be: &BE, tree_id: Option<Id>, ignore_ctime: bool, ignore_inode: bool) -> Self {
@ -155,13 +143,12 @@ impl<BE: IndexedBackend> Parent<BE> {
Ok(())
}
pub fn process<O>(
&mut self,
item: TreeType<(PathBuf, Node, O), (PathBuf, Node, OsString)>,
) -> Result<ItemWithParent<O>> {
pub fn process<O>(&mut self, item: TreeType<O, OsString>) -> Result<ItemWithParent<O>> {
let result = match item {
TreeType::NewTree((path, node, tree)) => {
let parent_result = self.is_parent(&node, &tree).into_tree_id();
let parent_result = self
.is_parent(&node, &tree)
.map(|node| node.subtree().unwrap());
self.set_dir(&tree)?;
TreeType::NewTree((path, node, parent_result))
}
@ -169,7 +156,7 @@ impl<BE: IndexedBackend> Parent<BE> {
self.finish_dir()?;
TreeType::EndTree
}
TreeType::Other((path, mut node, open)) => TreeType::Other({
TreeType::Other((path, mut node, open)) => {
let be = self.be.clone();
let parent = self.is_parent(&node, &node.name());
let parent = match parent {
@ -184,10 +171,10 @@ impl<BE: IndexedBackend> Parent<BE> {
ParentResult::NotFound
}
}
parent_result => parent_result.into_type_only(),
parent_result => parent_result.map(|_| ()),
};
(path, node, open, parent)
}),
TreeType::Other((path, node, (open, parent)))
}
};
Ok(result)
}

View File

@ -3,6 +3,10 @@ use std::path::{Component, PathBuf};
use crate::blob::{Metadata, Node, NodeType};
/// `TreeIterator` truns an Iterator yielding items with paths and Nodes into an
/// Iterator which ensures that all subdirectories are visited and closed.
/// The resulting Iterator yielss a `TreeType` which either contains the original
/// item, a new tree to be inserted or a pseudo item which idicates that a tree is finished.
pub struct TreeIterator<T, I> {
iter: I,
path: PathBuf,
@ -25,16 +29,16 @@ where
#[derive(Debug)]
pub enum TreeType<T, U> {
NewTree(U),
NewTree((PathBuf, Node, U)),
EndTree,
Other(T),
Other((PathBuf, Node, T)),
}
impl<I, O> Iterator for TreeIterator<(PathBuf, Node, O), I>
where
I: Iterator<Item = (PathBuf, Node, O)>,
{
type Item = TreeType<(PathBuf, Node, O), (PathBuf, Node, OsString)>;
type Item = TreeType<O, OsString>;
fn next(&mut self) -> Option<Self::Item> {
match &self.item {
None => {

View File

@ -20,8 +20,7 @@ pub struct TreeArchiver<BE: DecryptWriteBackend, I: IndexedBackend> {
summary: SnapshotSummary,
}
pub type TreeItem =
TreeType<(PathBuf, Node, ParentResult<()>, u64), (PathBuf, Node, ParentResult<Id>)>;
pub type TreeItem = TreeType<(ParentResult<()>, u64), ParentResult<Id>>;
impl<BE: DecryptWriteBackend, I: IndexedBackend> TreeArchiver<BE, I> {
pub fn new(
@ -70,7 +69,7 @@ impl<BE: DecryptWriteBackend, I: IndexedBackend> TreeArchiver<BE, I> {
self.tree = tree;
self.tree.add(node);
}
TreeType::Other((path, node, parent, size)) => {
TreeType::Other((path, node, (parent, size))) => {
self.add_file(&path, node, parent, size);
}
}