Skip to main content

qubit_redact/facade/redactor/
json.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! JSON redaction operations.
9
10use super::Redactor;
11use crate::RedactionInspection;
12use crate::RedactionInspectionError;
13use crate::RedactionTextOutput;
14
15impl Redactor {
16    /// Redacts JSON text through one completed text transaction.
17    #[must_use]
18    pub fn redact_json(&self, text: &str) -> RedactionTextOutput {
19        let mut session = self.text_runtime();
20        session.json(|json| {
21            let _ = json.text(text);
22        });
23        session.finish()
24    }
25
26    /// Redacts a borrowed parsed JSON value without taking ownership of it.
27    #[must_use]
28    pub fn redact_json_value(&self, value: &serde_json::Value) -> RedactionTextOutput {
29        let mut session = self.text_runtime();
30        session.json(|json| {
31            let _ = json.value(value);
32        });
33        session.finish()
34    }
35
36    /// Inspects one JSON document without rendering it.
37    ///
38    /// # Errors
39    ///
40    /// Returns [`RedactionInspectionError`] when JSON parsing fails or a
41    /// shared resource limit prevents complete inspection.
42    pub fn inspect_json(&self, text: &str) -> Result<RedactionInspection, RedactionInspectionError> {
43        let mut session = self.inspection_runtime();
44        crate::formats::json::inspection::inspect_text(&mut session, text);
45        session.finish()
46    }
47
48    /// Inspects a borrowed parsed JSON value without taking ownership of it.
49    ///
50    /// # Errors
51    ///
52    /// Returns [`RedactionInspectionError`] when a shared structural, value,
53    /// or input limit prevents complete inspection.
54    pub fn inspect_json_value(
55        &self,
56        value: &serde_json::Value,
57    ) -> Result<RedactionInspection, RedactionInspectionError> {
58        let mut session = self.inspection_runtime();
59        crate::formats::json::inspection::inspect_borrowed_value(&mut session, value);
60        session.finish()
61    }
62}