Skip to main content

rustbrain_core/mutator/
mod.rs

1//! Optional jshift-backed in-place JSON field mutation helpers.
2//!
3//! Enabled with the `jshift` Cargo feature. Not used by the core indexing path;
4//! provided for tooling that streams JSONL manifests or agent side-channels
5//! without full `serde_json` re-serialization.
6
7#[cfg(feature = "jshift")]
8mod imp {
9    use crate::error::{BrainError, Result};
10    use jshift::{find_value, mutate_value, parse_path};
11
12    /// Mutate a JSON byte buffer in-place using jshift.
13    pub fn update_json_field_inplace(
14        json_bytes: &mut Vec<u8>,
15        path_expr: &str,
16        new_value_bytes: &[u8],
17    ) -> Result<()> {
18        let path = parse_path(path_expr);
19        mutate_value(json_bytes, &path, new_value_bytes)
20            .map_err(|e| BrainError::other(format!("jshift mutate failed: {e}")))?;
21        Ok(())
22    }
23
24    /// Zero-copy extraction of a JSON field slice from raw bytes.
25    pub fn extract_json_field_slice<'a>(
26        json_bytes: &'a [u8],
27        path_expr: &str,
28    ) -> Option<&'a [u8]> {
29        let path = parse_path(path_expr);
30        find_value(json_bytes, &path).ok()
31    }
32
33    #[cfg(test)]
34    mod tests {
35        use super::*;
36
37        #[test]
38        fn jshift_inplace_mutation() {
39            let mut json =
40                b"{\"node_id\": \"concept-1\", \"status\": \"draft\", \"weight\": 0.5}".to_vec();
41            update_json_field_inplace(&mut json, "status", b"\"published\"").unwrap();
42            let status = extract_json_field_slice(&json, "status").unwrap();
43            assert_eq!(status, b"\"published\"");
44        }
45    }
46}
47
48#[cfg(feature = "jshift")]
49pub use imp::*;