pub struct Redactions { /* private fields */ }Expand description
Replace data with placeholders
This can be used for:
- Handling test-run dependent data like temp directories or elapsed time
- Making special characters more obvious (e.g. redacting a tab a
[TAB]) - Normalizing platform-specific data like
std::env::consts::EXE_SUFFIX
§Examples
let mut subst = snapbox::Redactions::new();
subst.insert("[LOCATION]", "World");
assert_eq!(subst.redact("Hello World!"), "Hello [LOCATION]!");Implementations§
Source§impl Redactions
impl Redactions
pub const fn new() -> Self
Sourcepub fn insert(
&mut self,
placeholder: &'static str,
value: impl Into<RedactedValue>,
) -> Result<()>
pub fn insert( &mut self, placeholder: &'static str, value: impl Into<RedactedValue>, ) -> Result<()>
Insert an additional match pattern
placeholder must be enclosed in [ and ].
let mut subst = snapbox::Redactions::new();
subst.insert("[EXE]", std::env::consts::EXE_SUFFIX);With the regex feature, you can define patterns using regexes.
You can choose to replace a subset of the regex by giving it the named capture group
redacted.
let mut subst = snapbox::Redactions::new();
subst.insert("[OBJECT]", regex::Regex::new("(?<redacted>(world|moon))").unwrap());
assert_eq!(subst.redact("Hello world!"), "Hello [OBJECT]!");
assert_eq!(subst.redact("Hello moon!"), "Hello [OBJECT]!");
assert_eq!(subst.redact("Hello other!"), "Hello other!");Sourcepub fn extend(
&mut self,
vars: impl IntoIterator<Item = (&'static str, impl Into<RedactedValue>)>,
) -> Result<()>
pub fn extend( &mut self, vars: impl IntoIterator<Item = (&'static str, impl Into<RedactedValue>)>, ) -> Result<()>
Insert additional match patterns
Placeholders must be enclosed in [ and ].
pub fn remove(&mut self, placeholder: &'static str) -> Result<()>
Sourcepub fn redact(&self, input: &str) -> String
pub fn redact(&self, input: &str) -> String
Apply redaction only, no pattern-dependent globs
§Examples
let mut subst = snapbox::Redactions::new();
subst.insert("[LOCATION]", "World");
let output = subst.redact("Hello World!");
assert_eq!(output, "Hello [LOCATION]!");Sourcepub fn clear_unused<'v>(&self, pattern: &'v str) -> Cow<'v, str>
pub fn clear_unused<'v>(&self, pattern: &'v str) -> Cow<'v, str>
Clear unused redactions from expected data
Some redactions can be conditionally present, like redacting std::env::consts::EXE_SUFFIX.
When the redaction is not present, it needs to be removed from the expected data so it can
be matched against the actual data.
Trait Implementations§
Source§impl Clone for Redactions
impl Clone for Redactions
Source§fn clone(&self) -> Redactions
fn clone(&self) -> Redactions
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Redactions
impl Debug for Redactions
Source§impl Default for Redactions
impl Default for Redactions
Source§fn default() -> Redactions
fn default() -> Redactions
Returns the “default value” for a type. Read more
Source§impl PartialEq for Redactions
impl PartialEq for Redactions
impl Eq for Redactions
impl StructuralPartialEq for Redactions
Auto Trait Implementations§
impl Freeze for Redactions
impl RefUnwindSafe for Redactions
impl Send for Redactions
impl Sync for Redactions
impl Unpin for Redactions
impl UnwindSafe for Redactions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more