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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use mokuya::components::prelude::get_struct_name;
use proc_macro2::TokenStream;
use quote::quote;
use syn::DeriveInput;
pub fn generate_parse_toml(input: &DeriveInput) -> TokenStream {
let struct_name = get_struct_name(input);
quote! {
/// Wrapper around `toml::Value` generated by `#[derive(Parser)]` for TOML parsing.
pub struct ParseToml(toml::Value);
impl ParseToml {
/// Returns an immutable reference to the internal `toml::Value`.
///
/// Useful when you want to inspect or manipulate the raw value.
pub fn get(&self) -> &toml::Value {
&self.0
}
/// Consumes `ParseToml` and wraps it in `Arc<toml::Value>`.
///
/// Enables shared ownership across threads.
pub fn arc(self) -> std::sync::Arc<toml::Value> {
std::sync::Arc::new(self.0)
}
/// Consumes `ParseToml` and wraps it in `tokio::sync::Mutex`.
///
/// Useful in async environments for interior mutability.
pub fn tokio_mutex(self) -> tokio::sync::Mutex<toml::Value> {
tokio::sync::Mutex::new(self.0)
}
/// Consumes `ParseToml` and wraps it in `std::sync::Mutex`.
///
/// Enables interior mutability in synchronous code.
pub fn mutex(self) -> std::sync::Mutex<toml::Value> {
std::sync::Mutex::new(self.0)
}
/// Consumes `ParseToml` and wraps it in `RefCell`.
///
/// Allows mutable borrows at runtime in single-threaded contexts.
pub fn ref_cell(self) -> std::cell::RefCell<toml::Value> {
std::cell::RefCell::new(self.0)
}
/// Consumes `ParseToml` and wraps it in `UnsafeCell`.
///
/// Low-level container for interior mutability.
pub fn unsafe_cell(self) -> std::cell::UnsafeCell<toml::Value> {
std::cell::UnsafeCell::new(self.0)
}
/// Consumes `ParseToml` and initializes a `OnceCell` with the TOML value.
///
/// Returns the populated `OnceCell`. If already set internally, silently does nothing.
pub fn once_cell(self) -> std::cell::OnceCell<toml::Value> {
let cell = std::cell::OnceCell::new();
let _ = cell.set(self.0);
cell
}
/// Attempts to deserialize the internal `toml::Value` into the original struct.
///
/// # Errors
/// Returns a `toml::de::Error` if deserialization fails.
pub fn from(self) -> Result<#struct_name, toml::de::Error> {
self.0.try_into()
}
/// Attempts to deserialize the given `toml::Value` into the original struct.
///
/// This method borrows the `value` rather than consuming `self`.
///
/// # Errors
/// Returns a `toml::de::Error` if deserialization fails.
pub fn from_value(&self, value: &toml::Value) -> Result<#struct_name, toml::de::Error> {
value.clone().try_into()
}
}
}
}