Sha256: e826ee0ab31616e49c3e3f3711c8441001ee424b3e7a8c4c466cfcc4f8a7701a

Contents?: true

Size: 1.98 KB

Versions: 37

Compression:

Stored size: 1.98 KB

Contents

//! Untagged serialization/deserialization support for Either<L, R>.
//!
//! `Either` uses default, externally-tagged representation.
//! However, sometimes it is useful to support several alternative types.
//! For example, we may have a field which is generally Map<String, i32>
//! but in typical cases Vec<String> would suffice, too.
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use either::Either;
//! use std::collections::HashMap;
//!
//! #[derive(serde::Serialize, serde::Deserialize, Debug)]
//! #[serde(transparent)]
//! struct IntOrString {
//!     #[serde(with = "either::serde_untagged")]
//!     inner: Either<Vec<String>, HashMap<String, i32>>
//! };
//!
//! // serialization
//! let data = IntOrString {
//!     inner: Either::Left(vec!["Hello".to_string()])
//! };
//! // notice: no tags are emitted.
//! assert_eq!(serde_json::to_string(&data)?, r#"["Hello"]"#);
//!
//! // deserialization
//! let data: IntOrString = serde_json::from_str(
//!     r#"{"a": 0, "b": 14}"#
//! )?;
//! println!("found {:?}", data);
//! # Ok(())
//! # }
//! ```

use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
enum Either<L, R> {
    Left(L),
    Right(R),
}

pub fn serialize<L, R, S>(this: &super::Either<L, R>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
    L: Serialize,
    R: Serialize,
{
    let untagged = match this {
        super::Either::Left(left) => Either::Left(left),
        super::Either::Right(right) => Either::Right(right),
    };
    untagged.serialize(serializer)
}

pub fn deserialize<'de, L, R, D>(deserializer: D) -> Result<super::Either<L, R>, D::Error>
where
    D: Deserializer<'de>,
    L: Deserialize<'de>,
    R: Deserialize<'de>,
{
    match Either::deserialize(deserializer) {
        Ok(Either::Left(left)) => Ok(super::Either::Left(left)),
        Ok(Either::Right(right)) => Ok(super::Either::Right(right)),
        Err(error) => Err(error),
    }
}

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
wasmtime-28.0.0 ./ext/cargo-vendor/either-1.13.0/src/serde_untagged.rs
wasmtime-27.0.0 ./ext/cargo-vendor/either-1.13.0/src/serde_untagged.rs
wasmtime-26.0.0 ./ext/cargo-vendor/either-1.13.0/src/serde_untagged.rs
wasmtime-25.0.2 ./ext/cargo-vendor/either-1.13.0/src/serde_untagged.rs
wasmtime-25.0.1 ./ext/cargo-vendor/either-1.13.0/src/serde_untagged.rs
wasmtime-25.0.0 ./ext/cargo-vendor/either-1.13.0/src/serde_untagged.rs
wasmtime-24.0.0 ./ext/cargo-vendor/either-1.13.0/src/serde_untagged.rs
wasmtime-23.0.2 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-22.0.0 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-21.0.1 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-20.0.2 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-20.0.0 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-18.0.3 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-17.0.1 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-17.0.0 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-16.0.0 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-15.0.1 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-15.0.0 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-14.0.4 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs
wasmtime-14.0.3 ./ext/cargo-vendor/either-1.9.0/src/serde_untagged.rs