binroots 0.2.2

Serialize and expose data, one file per field.
Documentation
//! ## `binroots::field`
//! Contains the [`BinrootsField`][`crate::field::BinrootsField`] struct

/// # BinrootsField
/// A wrapper type for fields generated by [`binroots::binroots_struct`][`crate::binroots_struct`]
///
/// ## Saving
///
/// [`BinrootsField::save`][struct.BinrootsField.html#method.save] contains a couple differences than other implementations:
/// - Unlike [`binroots::binroots_struct`][`crate::binroots_struct`], it requires a root folder to save to (typically `Struct::ROOT_FOLDER`)
/// - Modifies the root save path by appending `BinrootsField::N` (generated as the field name by [`binroots::binroots_struct`][`crate::binroots_struct`])
#[derive(Default)]
pub struct BinrootsField<const N: &'static str, T> {
    pub(crate) value: T,
}

impl<const N: &'static str, T: serde::Serialize> serde::Serialize for BinrootsField<N, T> {
    /// Serializes the field's inner value if the value implements [`binroots::Serialize`][`crate::Serialize`]
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.value.serialize(serializer)
    }
}

impl<const N: &'static str, T> std::ops::Deref for BinrootsField<N, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<const N: &'static str, T> std::ops::DerefMut for BinrootsField<N, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

impl<const N: &'static str, T> AsRef<T> for BinrootsField<N, T> {
    fn as_ref(&self) -> &T {
        &self.value
    }
}

impl<const N: &'static str, T> AsMut<T> for BinrootsField<N, T> {
    fn as_mut(&mut self) -> &mut T {
        &mut self.value
    }
}

impl<const N: &'static str, T: std::fmt::Debug> std::fmt::Debug for BinrootsField<N, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(&format!("BinrootsField<\"{}\">", N))
            .field("value", &self.value)
            .finish()
    }
}

impl<const N: &'static str, T> BinrootsField<N, T> {
    /// Constructs BinrootsField using `value` as the interior value. `BinrootsField::N` must be declared ahead-of-time.
    ///
    /// See [`![feature(adt_const_params)]`][<https://github.com/rust-lang/rust/issues/95174>] for using constant `&'static str` generics.
    pub const fn new(value: T) -> Self {
        Self { value }
    }

    /// Returns `BinrootsField::N`, often the name of the field if generated by [`binroots::binroots_struct`][`crate::binroots_struct`]
    pub const fn name() -> &'static str {
        N
    }
}