summary refs log blame commit diff stats
path: root/src/lib.rs
blob: 2bcf3e4989d1a0db75d107819364d634278bd609 (plain) (tree)














































































                                                                               
// 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
    }
}