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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use mokuya::components::prelude::*;
use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;
pub fn generate_parse_json(input: &DeriveInput) -> TokenStream {
let struct_name = get_struct_name(input);
quote! {
pub struct ParseJson(serde_json::Value);
impl ParseJson {
/// Returns a reference to the internal `serde_json::Value`.
pub fn get(&self) -> &serde_json::Value {
&self.0
}
/// Consumes self and returns an `Arc` wrapped `serde_json::Value`
/// for thread-safe shared ownership.
pub fn arc(self) -> std::sync::Arc<serde_json::Value> {
std::sync::Arc::new(self.0)
}
/// Consumes self and returns a Tokio async mutex wrapping the JSON value.
pub fn tokio_mutex(self) -> tokio::sync::Mutex<serde_json::Value> {
tokio::sync::Mutex::new(self.0)
}
/// Consumes self and returns a standard mutex wrapping the JSON value.
pub fn mutex(self) -> std::sync::Mutex<serde_json::Value> {
std::sync::Mutex::new(self.0)
}
/// Consumes self and returns a `RefCell` wrapping the JSON value,
/// allowing interior mutability.
pub fn ref_cell(self) -> std::cell::RefCell<serde_json::Value> {
std::cell::RefCell::new(self.0)
}
/// Consumes self and returns an `UnsafeCell` wrapping the JSON value.
pub fn unsafe_cell(self) -> std::cell::UnsafeCell<serde_json::Value> {
std::cell::UnsafeCell::new(self.0)
}
/// Consumes self and returns a `OnceCell` wrapping the JSON value,
/// allowing one-time initialization.
pub fn once_cell(self) -> std::cell::OnceCell<serde_json::Value> {
let cell = std::cell::OnceCell::new();
cell.set(self.0).ok();
cell
}
/// Consumes self and attempts to deserialize the JSON value into
/// the original struct type. Returns a `Result` with the deserialized struct or an error.
pub fn from(self) -> Result<#struct_name, serde_json::Error> {
serde_json::from_value(self.0)
}
/// Attempts to deserialize a given `serde_json::Value` reference into
/// the original struct type. Returns a `Result` with the deserialized struct or an error.
pub fn from_value(&self, value: &serde_json::Value) -> Result<#struct_name, serde_json::Error> {
serde_json::from_value(value.clone())
}
}
}
}