Skip to content

Commit

Permalink
Make Filters constructible from IntoIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 28, 2024
1 parent 4dcccca commit 1d70238
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to insta and cargo-insta are documented here.

## 1.38.0

- `Filters` is now constructible from `IntoIterator`. #400

## 1.37.0

- All macros for file snapshots should now handle trailing commas (but not yet inline snapshots)
Expand Down
23 changes: 21 additions & 2 deletions insta/src/filters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::iter::FromIterator;

use regex::Regex;

Expand All @@ -10,9 +11,15 @@ pub struct Filters {
}

impl<'a> From<Vec<(&'a str, &'a str)>> for Filters {
fn from(value: Vec<(&'a str, &'a str)>) -> Filters {
fn from(value: Vec<(&'a str, &'a str)>) -> Self {
Self::from_iter(value)
}
}

impl<'a> FromIterator<(&'a str, &'a str)> for Filters {
fn from_iter<I: IntoIterator<Item = (&'a str, &'a str)>>(iter: I) -> Self {
let mut rv = Filters::default();
for (regex, replacement) in value {
for (regex, replacement) in iter {
rv.add(regex, replacement);
}
rv
Expand Down Expand Up @@ -58,3 +65,15 @@ fn test_filters() {
"hellohello [NAME] [a]bc"
);
}

#[test]
fn test_static_str_array_conversion() {
let arr: [(&'static str, &'static str); 2] = [("a1", "b1"), ("a2", "b2")];
let _ = Filters::from_iter(arr);
}

#[test]
fn test_vec_str_conversion() {
let vec: Vec<(&str, &str)> = Vec::from([("a1", "b1"), ("a2", "b2")]);
let _ = Filters::from(vec);
}

0 comments on commit 1d70238

Please sign in to comment.