1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
}
}
|