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! {
#[derive(Debug)]
pub struct ParseJson(serde_json::Value);
impl ParseJson {
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn get(&self) -> &serde_json::Value {
&self.0
}
#[cfg(feature="arc")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn arc(self) -> std::sync::Arc<serde_json::Value> {
std::sync::Arc::new(self.0)
}
#[cfg(feature = "tokio")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn tokio_mutex(self) -> tokio::sync::Mutex<serde_json::Value> {
tokio::sync::Mutex::new(self.0)
}
#[cfg(feature="mutex")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn mutex(self) -> std::sync::Mutex<serde_json::Value> {
std::sync::Mutex::new(self.0)
}
#[cfg(feature="refcell")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn ref_cell(self) -> std::cell::RefCell<serde_json::Value> {
std::cell::RefCell::new(self.0)
}
#[cfg(feature="unsafecell")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn unsafe_cell(self) -> std::cell::UnsafeCell<serde_json::Value> {
std::cell::UnsafeCell::new(self.0)
}
#[cfg(feature="oncecell")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn once_cell(self) -> std::cell::OnceCell<serde_json::Value> {
let cell = std::cell::OnceCell::new();
cell.set(self.0).ok();
cell
}
#[cfg(feature="serde_json")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn from(self) -> Result<#struct_name, serde_json::Error> {
serde_json::from_value(self.0)
}
#[cfg(feature="serde_json")]
#[cfg_attr(feature = "nekotracing", nekotracing::nekotracing)]
pub fn from_value(&self, value: &serde_json::Value) -> Result<#struct_name, serde_json::Error> {
serde_json::from_value(value.clone())
}
}
}
}