Crate value_extra

Crate value_extra 

Source
Expand description

§value-extra

A tri-state Patch<T> type for partial update semantics.

§The Problem

When handling partial updates (PATCH requests, config merging, etc.), Option<T> conflates two distinct states:

  • Field is absent → don’t touch the existing value
  • Field is explicitly null → clear/reset the value

§The Solution

Patch<T> provides three states:

  • Patch::Some(T) — set the field to this value
  • Patch::Empty — field was absent, leave unchanged
  • Patch::None — field was explicitly null, clear it

§Quick Start

use value_extra::Patch;

fn apply_name_patch(current: &mut Option<String>, patch: Patch<String>) {
    match patch {
        Patch::Some(new) => *current = Some(new),
        Patch::None => *current = None,
        Patch::Empty => {}
    }
}

§Serde Usage

With the serde feature enabled:

#[derive(Deserialize, Serialize)]
struct UserPatch {
    #[serde(default, skip_serializing_if = "Patch::is_empty")]
    name: Patch<String>,
}
JSON InputPatch StateMeaning
{ "name": "Alice" }Patch::Some("Alice")Set to value
{ "name": null }Patch::NoneExplicitly clear
{ }Patch::EmptyLeave unchanged

Enums§

Patch
A tri-state type for partial update semantics.