HumanOr

Struct HumanOr 

Source
pub struct HumanOr<F, G>(/* private fields */);
Expand description

Adapter for custom serialization when the serialization format is human-readable

If the serialization format is human-readable, F is used to serialize. Otherwise G is used.

§Example

use bincode::Options as _;
use std::{fmt::{self, Display}, str::FromStr};
use serdapt as sa;
use serde::{Deserialize, Serialize};
use serde_json::json;

struct Color(u32);

impl Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "#{:06x}", self.0)
    }
}

impl FromStr for Color {
    type Err = std::num::ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        u32::from_str_radix(s.trim_start_matches('#'), 16).map(Color)
    }
}

impl From<u32> for Color {
    fn from(n: u32) -> Color {
        Color(n)
    }
}

impl From<Color> for u32 {
    fn from(c: Color) -> u32 {
        c.0
    }
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct Palette {
    #[serde(with = "sa::Seq::<sa::HumanOr<sa::Convert<Color, sa::Str>, sa::Id>>")]
    colors: Vec<u32>,
}

let palette = Palette { colors: vec![0x000000, 0xffffff] };
let serialized = serde_json::to_value(&palette).unwrap();
assert_eq!(serialized, json!({ "colors": ["#000000", "#ffffff"] }));
let deserialized = serde_json::from_value::<Palette>(serialized).unwrap();
assert_eq!(deserialized, palette);
let bincode_config = bincode::options().with_fixint_encoding();
let serialized = bincode_config.serialize(&palette).unwrap();
assert_eq!(
    serialized,
    2u64
        .to_le_bytes()
        .into_iter()
        .chain(0u32.to_le_bytes())
        .chain(0xffffffu32.to_le_bytes())
        .collect::<Vec<_>>(),
);
let deserialized = bincode_config.deserialize::<Palette>(&serialized).unwrap();
assert_eq!(deserialized, palette);

Implementations§

Source§

impl<F, G> HumanOr<F, G>

Source

pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where T: ?Sized, S: Serializer, Self: SerializeWith<T>,

Serializes value with adapter

Source

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where D: Deserializer<'de>, Self: DeserializeWith<'de, T>,

Deserializes value with adapter

Trait Implementations§

Source§

impl<'de, F, G, T> DeserializeWith<'de, T> for HumanOr<F, G>
where F: DeserializeWith<'de, T>, G: DeserializeWith<'de, T>,

Source§

fn deserialize_with<D>(deserializer: D) -> Result<T, D::Error>
where D: Deserializer<'de>,

Deserializes a value using deserializer
Source§

impl<F, G, T> SerializeWith<T> for HumanOr<F, G>
where F: SerializeWith<T>, G: SerializeWith<T>, T: ?Sized,

Source§

fn serialize_with<S: Serializer>( value: &T, serializer: S, ) -> Result<S::Ok, S::Error>

Serializes value using serializer

Auto Trait Implementations§

§

impl<F, G> Freeze for HumanOr<F, G>

§

impl<F, G> RefUnwindSafe for HumanOr<F, G>

§

impl<F, G> Send for HumanOr<F, G>
where F: Send, G: Send,

§

impl<F, G> Sync for HumanOr<F, G>
where F: Sync, G: Sync,

§

impl<F, G> Unpin for HumanOr<F, G>
where F: Unpin, G: Unpin,

§

impl<F, G> UnwindSafe for HumanOr<F, G>
where F: UnwindSafe, G: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.