From 82c545e6d1676a1eb0e9b6d7ecafc8b045671c99 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Mon, 20 Mar 2023 22:37:42 +0100 Subject: [PATCH] simplify iterator items --- src/archiver/file_archiver.rs | 4 ++-- src/archiver/parent.rs | 31 +++++++++---------------------- src/archiver/tree.rs | 10 +++++++--- src/archiver/tree_archiver.rs | 5 ++--- 4 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/archiver/file_archiver.rs b/src/archiver/file_archiver.rs index 486431d..35e5a58 100644 --- a/src/archiver/file_archiver.rs +++ b/src/archiver/file_archiver.rs @@ -46,7 +46,7 @@ impl FileArchiver { 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 FileArchiver { } else { (node, 0) }; - TreeType::Other((path, node, parent, filesize)) + TreeType::Other((path, node, (parent, filesize))) } }) } diff --git a/src/archiver/parent.rs b/src/archiver/parent.rs index 4d84d9b..d025f2e 100644 --- a/src/archiver/parent.rs +++ b/src/archiver/parent.rs @@ -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 ParentResult { Self::NotMatched => ParentResult::NotMatched, } } - - pub fn into_type_only(self) -> ParentResult<()> { - self.map(|_| ()) - } } -impl ParentResult<&Node> { - pub fn into_tree_id(self) -> ParentResult { - self.map(|node| node.subtree().unwrap()) - } -} - -pub type ItemWithParent = - TreeType<(PathBuf, Node, O, ParentResult<()>), (PathBuf, Node, ParentResult)>; +pub type ItemWithParent = TreeType<(O, ParentResult<()>), ParentResult>; impl Parent { pub fn new(be: &BE, tree_id: Option, ignore_ctime: bool, ignore_inode: bool) -> Self { @@ -155,13 +143,12 @@ impl Parent { Ok(()) } - pub fn process( - &mut self, - item: TreeType<(PathBuf, Node, O), (PathBuf, Node, OsString)>, - ) -> Result> { + pub fn process(&mut self, item: TreeType) -> Result> { 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 Parent { 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 Parent { 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) } diff --git a/src/archiver/tree.rs b/src/archiver/tree.rs index bc6f5b4..c516278 100644 --- a/src/archiver/tree.rs +++ b/src/archiver/tree.rs @@ -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 { iter: I, path: PathBuf, @@ -25,16 +29,16 @@ where #[derive(Debug)] pub enum TreeType { - NewTree(U), + NewTree((PathBuf, Node, U)), EndTree, - Other(T), + Other((PathBuf, Node, T)), } impl Iterator for TreeIterator<(PathBuf, Node, O), I> where I: Iterator, { - type Item = TreeType<(PathBuf, Node, O), (PathBuf, Node, OsString)>; + type Item = TreeType; fn next(&mut self) -> Option { match &self.item { None => { diff --git a/src/archiver/tree_archiver.rs b/src/archiver/tree_archiver.rs index 3d13cd3..06bbddc 100644 --- a/src/archiver/tree_archiver.rs +++ b/src/archiver/tree_archiver.rs @@ -20,8 +20,7 @@ pub struct TreeArchiver { summary: SnapshotSummary, } -pub type TreeItem = - TreeType<(PathBuf, Node, ParentResult<()>, u64), (PathBuf, Node, ParentResult)>; +pub type TreeItem = TreeType<(ParentResult<()>, u64), ParentResult>; impl TreeArchiver { pub fn new( @@ -70,7 +69,7 @@ impl TreeArchiver { 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); } }