tuwunel_database/deserialized.rs
1//! Typed decoding adapters for stored database values.
2//!
3//! The extension trait composes value deserialization with query results and
4//! follow-up mapping. Implementations preserve input errors and decode only
5//! successful raw values.
6
7use std::convert::identity;
8
9use serde::Deserialize;
10use tuwunel_core::Result;
11
12/// Converts a stored database value into a typed Serde value.
13///
14/// Implementations adapt pinned value handles and their result wrappers to the
15/// database decoder. Use [`Deserialized::map_de`] to transform the decoded
16/// value or [`Deserialized::deserialized`] to return it directly.
17pub trait Deserialized {
18 /// Deserializes an intermediate value and maps it into the requested
19 /// output.
20 ///
21 /// The mapping closure runs only after successful deserialization. Any
22 /// input or decoding error is returned without invoking the closure.
23 fn map_de<T, U, F>(self, f: F) -> Result<U>
24 where
25 F: FnOnce(T) -> U,
26 T: for<'de> Deserialize<'de>;
27
28 /// Deserializes the stored value directly into the requested type.
29 ///
30 /// This is equivalent to mapping with the identity function. Any input or
31 /// decoding error is returned unchanged.
32 #[inline]
33 fn deserialized<T>(self) -> Result<T>
34 where
35 T: for<'de> Deserialize<'de>,
36 Self: Sized,
37 {
38 self.map_de(identity::<T>)
39 }
40}