-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathjson_handler.rs
More file actions
51 lines (45 loc) · 1.79 KB
/
Copy pathjson_handler.rs
File metadata and controls
51 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
use serde_json::{Value, json};
use std::path::PathBuf;
/// A handler for building and exporting JSON data structures.
///
/// `JsonHandler` provides a convenient interface for constructing JSON objects,
/// adding key-value pairs, appending to arrays, and exporting the final result
/// to a file.
pub struct JsonHandler {
/// The JSON data being constructed.
pub(crate) data: Value,
/// Optional path where the JSON data will be exported.
export_path: Option<PathBuf>,
}
impl JsonHandler {
/// Creates a new `JsonHandler` with an optional export path.
/// If `export_path` is `None`, calls to `export()` will be no-ops.
pub fn new(export_path: Option<PathBuf>) -> Self {
Self { data: json!({}), export_path }
}
/// Adds or updates a key-value pair in the JSON object.
/// If the key already exists, its value will be overwritten.
pub fn add_item(&mut self, key: &str, value: Value) {
self.data[key] = value;
}
/// Appends a value to the array at the specified key.
/// Creates a new array if the key doesn't exist or is null.
/// Panics if the key exists but is not an array or null.
pub fn add_harness_detail(&mut self, key: &str, value: Value) {
if self.data[key].is_null() {
self.data[key] = json!([]);
}
self.data[key].as_array_mut().unwrap().push(value);
}
/// Exports the JSON data to the configured file path with pretty-printing.
/// Returns an error if the file cannot be written.
pub fn export(&self) -> Result<(), std::io::Error> {
if let Some(path) = &self.export_path {
std::fs::write(path, serde_json::to_string_pretty(&self.data)?)
} else {
Ok(())
}
}
}