Skip to content

Commit

Permalink
impl FromIterator for PathList
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Mar 12, 2024
1 parent e38d7e3 commit 72add89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
7 changes: 6 additions & 1 deletion crates/core/src/commands/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ pub(crate) fn merge_snapshots<P: ProgressBars, S: IndexedTree>(
) -> RusticResult<SnapshotFile> {
let now = Local::now();

let paths = PathList::from_strings(snapshots.iter().flat_map(|snap| snap.paths.iter())).merge();
let paths = snapshots

Check warning on line 42 in crates/core/src/commands/merge.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/commands/merge.rs#L42

Added line #L42 was not covered by tests
.iter()
.flat_map(|snap| snap.paths.iter())

Check warning on line 44 in crates/core/src/commands/merge.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/commands/merge.rs#L44

Added line #L44 was not covered by tests
.collect::<PathList>()
.merge();

snap.paths.set_paths(&paths.paths())?;

// set snapshot time to time of latest snapshot to be merged
Expand Down
55 changes: 22 additions & 33 deletions crates/core/src/repofile/snapshotfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,42 +1100,31 @@ impl Display for PathList {
}
}

impl PathList {
/// Create a `PathList` from `String`s.
///
/// # Arguments
///
/// * `source` - The `String`s to use
pub fn from_strings<I>(source: I) -> Self
where
I: IntoIterator,
I::Item: AsRef<str>,
{
Self(
source
.into_iter()
.map(|source| PathBuf::from(source.as_ref()))
.collect(),
)
impl FromIterator<PathBuf> for PathList {
fn from_iter<I: IntoIterator<Item = PathBuf>>(iter: I) -> Self {

Check warning on line 1104 in crates/core/src/repofile/snapshotfile.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repofile/snapshotfile.rs#L1104

Added line #L1104 was not covered by tests
Self(iter.into_iter().collect())
}
}

/// Create a `PathList` from a list of `PathBuf`s.
///
/// # Arguments
///
/// * `source` - The `PathBuf`s to use
///
/// # Returns
///
/// A `PathList` containing the `PathBuf`s
pub fn from_iter<I>(source: I) -> Self
where
I: IntoIterator,
I::Item: Into<PathBuf>,
{
Self(source.into_iter().map(Into::into).collect())
impl<'a> FromIterator<&'a String> for PathList {
fn from_iter<I: IntoIterator<Item = &'a String>>(iter: I) -> Self {
Self(iter.into_iter().map(PathBuf::from).collect())

Check warning on line 1111 in crates/core/src/repofile/snapshotfile.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repofile/snapshotfile.rs#L1110-L1111

Added lines #L1110 - L1111 were not covered by tests
}
}

impl FromIterator<String> for PathList {
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
Self(iter.into_iter().map(PathBuf::from).collect())

Check warning on line 1117 in crates/core/src/repofile/snapshotfile.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repofile/snapshotfile.rs#L1116-L1117

Added lines #L1116 - L1117 were not covered by tests
}
}

impl<'a> FromIterator<&'a str> for PathList {
fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
Self(iter.into_iter().map(PathBuf::from).collect())

Check warning on line 1123 in crates/core/src/repofile/snapshotfile.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repofile/snapshotfile.rs#L1122-L1123

Added lines #L1122 - L1123 were not covered by tests
}
}

impl PathList {
/// Create a `PathList` by parsing a Strings containing paths separated by whitspaces.
///
/// # Arguments
Expand All @@ -1149,7 +1138,7 @@ impl PathList {
/// [`SnapshotFileErrorKind::FromSplitError`]: crate::error::SnapshotFileErrorKind::FromSplitError
pub fn from_string(sources: &str) -> RusticResult<Self> {
let sources = split(sources).map_err(SnapshotFileErrorKind::FromSplitError)?;
Ok(Self::from_strings(sources))
Ok(Self::from_iter(sources))
}

/// Number of paths in the `PathList`.
Expand Down

0 comments on commit 72add89

Please sign in to comment.