diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2022-09-03 21:23:44 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2022-09-03 21:25:55 -0300 |
commit | 19340aea20881674d8d87d63101da19983c64479 (patch) | |
tree | 827a8e173237e39a504a7f68b23c6d577c8483e5 /src |
[Project] Serde Transmute
Transmute objects through Serde! This crate allows converting a value which can be serialized into a type which can be deserialized.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2bcf3e4 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,79 @@ +// Copyright (C) 2022 Soni L. +// SPDX-License-Identifier: MIT OR Apache-2.0 + +//! Transmute objects through serde! +//! +//! This crate allows converting a `Serialize` value into a `Deserialize` type. +//! +//! # Caveats +//! +//! The main caveat of note is that `Serialize` is not lifetime-aware, so the +//! `Deserialize` (or `DeserializeSeed`) cannot borrow from it. +//! +//! But we don't care because this crate was built to power parts of `datafu`. +//! And it's pretty good at that. + +use serde::{Deserialize, de::DeserializeSeed, Serialize}; + +/// Transmutes the given `Serialize` into a stateless `Deserialize`. +pub fn transmute<'de, D: Deserialize<'de>, S: Serialize>( + s: S, + settings: &Settings, +) -> Result<D, TransmuteError> { + transmute_seed(s, core::marker::PhantomData::<D>, settings) +} + +/// Transmutes the given `Serialize` with a stateful `DeserializeSeed`. +pub fn transmute_seed<'de, D: DeserializeSeed<'de>, S: Serialize>( + s: S, + d: D, + settings: &Settings, +) -> Result<D::Value, TransmuteError> { + todo!() +} + +#[derive(Debug)] +pub struct TransmuteError(()); + +/// Transmutation settings. +#[derive(Copy, Clone, Debug, Default)] +pub struct Settings { + /// Whether to use human-readable format. + human_readable: bool, + /// Whether structs are sequences. + structs_are_seqs: bool, +} + +impl Settings { + /// Creates a new transmutation settings with the defaults. + pub fn new() -> Self { + Default::default() + } + + /// Sets whether to use human readable formats for transmutation. + /// + /// Some data structures may serialize differently for human-readable and + /// non-human-readable formats. + /// + /// The default is to use non-human-readable transmutation. + pub fn set_human_readable(&mut self, human_readable: bool) -> &mut Self { + self.human_readable = human_readable; + self + } + + /// Treat structs like `struct Foo { bar: Bar }` as maps when + /// deserializing. + /// + /// This is the default. + pub fn structs_are_maps(&mut self) -> &mut Self { + self.structs_are_seqs = false; + self + } + + /// Treat structs like `struct Foo { bar: Bar }` as seqs when + /// deserializing. + pub fn structs_are_seqs(&mut self) -> &mut Self { + self.structs_are_seqs = true; + self + } +} |