Skip to main content

Redactions

Struct Redactions 

Source
pub struct Redactions { /* private fields */ }
Expand description

An ordered set of text rewrites applied to a value before it is compared against (or written to) a snapshot.

Build one with the chained methods, then hand it to check!(value).matches_snapshot_with(name, &redactions) (or the inline variant). Rules run in the order they were added, each on the output of the last.

use test_better_core::TestResult;
use test_better_matchers::{eq, check};
use test_better_snapshot::Redactions;

let redactions = Redactions::new().redact_uuids();
let rendered = "user 550e8400-e29b-41d4-a716-446655440000 logged in";
check!(redactions.apply(rendered))
    .satisfies(eq("user [uuid] logged in".to_string()))?;

Implementations§

Source§

impl Redactions

Source

pub fn new() -> Redactions

An empty set: apply returns its input unchanged until a rule is added.

Source

pub fn replace( self, needle: impl Into<String>, placeholder: impl Into<String>, ) -> Redactions

Replaces every occurrence of the literal needle with placeholder.

This is the rule for a value you already know, e.g. a generated id you captured earlier in the test. An empty needle is a no-op rule rather than a rule that matches everywhere.

Source

pub fn redact_uuids(self) -> Redactions

Replaces every UUID (the canonical 8-4-4-4-12 hex form, either case) with [uuid].

Source

pub fn redact_rfc3339_timestamps(self) -> Redactions

Replaces every RFC 3339 / ISO 8601 date-time (e.g. 2026-05-14T12:34:56Z, with optional fractional seconds and either a Z or a ±hh:mm offset) with [timestamp].

Source

pub fn redact_with( self, rule: impl Fn(&str) -> String + Send + Sync + 'static, ) -> Redactions

Adds an arbitrary rewriting rule: the escape hatch for content the built-ins do not cover.

The closure is handed the running text and returns its rewritten form.

Source

pub fn apply(&self, input: &str) -> String

Runs every rule, in order, and returns the rewritten text. With no rules added this returns input unchanged.

Source

pub fn is_empty(&self) -> bool

Whether any rule has been added. An empty set is worth skipping: its apply is an allocation that changes nothing.

Trait Implementations§

Source§

impl Debug for Redactions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Redactions

Source§

fn default() -> Redactions

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> RuntimeAvailable for T
where T: ?Sized,