๐งฑ Lenient Deserialize
A Rust crate for fault-tolerant deserialization using Serde. Automatically wraps fields to ensure invalid values fall back to sensible defaults โ instead of breaking the entire deserialization process.
This is useful when handling user inputs, external APIs, or anything where input reliability is uncertain.
โจ Features
Lenient<T>: wraps any type to gracefully fallback toT::default()on deserialization failure.Optional<T>: alias forLenient<Option<T>>.#[derive(LenientDeserialize)]: a procedural macro to generate fault-tolerant wrappers for entire structs.- Support for field-level
#[lenient]and#[optional]attributes. - Optional error logging via
tracing. - Ergonomic access with
DerefandDerefMutonLenient<T>.
๐ฆ Crate Structure
This workspace contains two crates:
lenient/
โโโ lenient/ # Main library (Lenient wrapper, re-exports macro)
โโโ lenient_derive/ # Procedural macro crate (LenientDeserialize)
๐ Quick Start
1. Add Dependencies
In your Cargo.toml:
[]
= { = "1.0", = ["derive"] }
= "1.0"
= "0.1"
= { = "./lenient" }
2. Example: Lenient Field Wrapping
use LenientDeserialize;
use Deserialize;
;
;
3. Example Input Handling with Deref
use json;
let input = json!;
let profile: UserProfile = from_value.unwrap;
let age = profile.age.0;
let score = profile.score.0;
let nickname = &profile.nickname;
println!;
๐งช Tests
Run Unit Tests for lenient
Sample Test
๐ License
MIT