microsandbox-types 0.7.7

Shared task and wire contract types for microsandbox.
Documentation
//! Runtime helpers called by code generated by `microsandbox-types-macros`.
//!
//! These functions are public so generated patches in other crates can call them.
//! They are implementation details of the derive and are not intended for direct use.

use serde::{Deserialize, Deserializer, Serialize, Serializer};

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

/// Record presence separately from the value, preserving explicit null for nullable fields.
pub fn deserialize_present<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
    deserializer: D,
) -> Result<Option<T>, D::Error> {
    T::deserialize(deserializer).map(Some)
}

/// Deserialize a collection contribution using the patch's existing merge strategy.
pub fn deserialize_merge<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
    deserializer: D,
) -> Result<Option<(bool, T)>, D::Error> {
    T::deserialize(deserializer).map(|value| Some((false, value)))
}

/// Write collection values in the config-file format, without the in-memory replacement flag.
pub fn serialize_merge<S: Serializer, T: Serialize>(
    value: &Option<(bool, T)>,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    value.as_ref().map(|(_, value)| value).serialize(serializer)
}