shori 0.0.3

Flexible parsing and transformation utilities for structured data. #[derive(Parser)] adds methods like .parse().json(), .toml(), .bin(), .map(), and .from() to simplify conversions using common wrappers like Arc, Mutex, Box, OnceCell, etc.
Documentation
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()
            }
        }
    }
}