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 |
[Project] Serde Transmute
Transmute objects through Serde! This crate allows converting a value which can be serialized into a type which can be deserialized.
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.toml | 15 | ||||
-rw-r--r-- | README.md | 13 | ||||
-rw-r--r-- | src/lib.rs | 79 | ||||
-rw-r--r-- | tests/foobar.rs | 18 |
5 files changed, 127 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9fc580e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "serde_transmute" +version = "0.1.0" +edition = "2021" +description = "Transmute objects through serde." +license = "MIT OR Apache-2.0" +repository = "https://soniex2.autistic.space/git-repos/serde_transmute.git" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = "1.0.144" + +[dev-dependencies] +serde = {version = "1.0.144", features = ["derive"]} diff --git a/README.md b/README.md new file mode 100644 index 0000000..a8d8ceb --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Serde Transmute + +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. 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 + } +} diff --git a/tests/foobar.rs b/tests/foobar.rs new file mode 100644 index 0000000..8495ccb --- /dev/null +++ b/tests/foobar.rs @@ -0,0 +1,18 @@ + +#[derive(serde::Serialize)] +struct Foo { + s: String, +} + +#[derive(serde::Deserialize)] +struct Bar { + s: String, +} + +#[test] +fn transmute() { + let settings = Default::default(); + let foo = Foo { s: String::from("Hello!") }; + let bar: Bar = serde_transmute::transmute(&foo, &settings).unwrap(); + assert_eq!(foo.s, bar.s); +} |