context_deserialize/
lib.rs

1//! Context-aware deserialization for Rust data structures.
2//!
3//! This crate provides the `ContextDeserialize` trait, which extends serde's deserialization
4//! capabilities by allowing you to pass additional context during deserialization.
5
6mod impls;
7
8#[cfg(feature = "derive")]
9pub use context_deserialize_derive::context_deserialize;
10
11use serde::de::Deserializer;
12
13/// A trait for deserializing data structures with additional context.
14///
15/// This trait is similar to serde's `Deserialize` trait, but with an additional context parameter
16/// of type `C` that can be passed through the deserialization process. This is useful when you need
17/// external information to properly deserialize your data structures.
18pub trait ContextDeserialize<'de, C>: Sized {
19    /// Deserialize this value from the given serde deserializer with additional context.
20    ///
21    /// # Parameters
22    ///
23    /// - `deserializer`: The serde deserializer to read from
24    /// - `context`: Additional context that can be used during deserialization
25    ///
26    /// # Errors
27    ///
28    /// Returns a deserialization error if the data cannot be deserialized into this type.
29    fn context_deserialize<D>(deserializer: D, context: C) -> Result<Self, D::Error>
30    where
31        D: Deserializer<'de>;
32}