Skip to main content

Redactor

Trait Redactor 

Source
pub trait Redactor {
    // Required method
    fn apply(&self, path: &str, current: &Value) -> Value;
}
Available on crate feature redaction only.
Expand description

Trait for types that can be used to redact values.

This trait defines how a redactor transforms a value at a given path. Implementations receive both the concrete JSON Pointer path and the current value, allowing for path-aware or value-based transformations.

§Implementations

  • &str - Replace with a static string
  • String - Replace with a string
  • serde_json::Value - Replace with a JSON value
  • Fn(&str, &Value) -> Value - Transform using a function

§Examples

// Static replacement
.redact("/id", "stable-uuid")?

// Value-based transformation (ignore path)
.redact("$..notes", |_path, val| {
    if val.as_str().map(|s| s.len() > 50).unwrap_or(false) {
        serde_json::json!("[REDACTED]")
    } else {
        val.clone()
    }
})?

// Path-aware transformation
.redact("$.items[*].id", |path, _val| {
    let idx = path.split('/').nth(2).unwrap_or("0");
    serde_json::json!(format!("stable-id-{idx}"))
})?

Required Methods§

Source

fn apply(&self, path: &str, current: &Value) -> Value

Apply the redaction to a value at the given path.

§Arguments
  • path - The concrete JSON Pointer path (e.g., /items/0/id)
  • current - The current value at that path
§Returns

The new value to replace the current one.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Redactor for &str

Source§

fn apply(&self, _path: &str, _current: &Value) -> Value

Source§

impl Redactor for String

Source§

fn apply(&self, _path: &str, _current: &Value) -> Value

Source§

impl Redactor for Value

Source§

fn apply(&self, _path: &str, _current: &Value) -> Value

Implementors§

Source§

impl<F> Redactor for F
where F: Fn(&str, &Value) -> Value,