Lines
100 %
Functions
48.54 %
Branches
use std::{cmp::PartialEq, fmt::Debug};
use ron::{de::from_str, ser::to_string};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Unit;
struct Tuple(u8, u8);
enum Inner {
Foo,
Bar,
}
enum EnumStructExternally {
VariantA {
foo: u32,
bar: Unit,
#[serde(with = "ByteStr")]
baz: Vec<u8>,
different: Tuple,
},
VariantB {
#[serde(tag = "type")]
enum EnumStructInternally {
VariantA { foo: u32, bar: u32, different: u32 },
VariantB { foo: u32, bar: u32 },
#[serde(tag = "type", content = "content")]
enum EnumStructAdjacently {
VariantA { foo: f64, bar: (), different: Inner },
VariantB { foo: f64, bar: () },
#[serde(untagged)]
enum EnumStructUntagged {
fn test_ser<T: Serialize>(value: &T, expected: &str) {
let actual = to_string(value).expect("Failed to serialize");
assert_eq!(actual, expected);
fn test_de<T>(s: &str, expected: T)
where
T: for<'a> Deserialize<'a> + Debug + PartialEq,
{
let actual: Result<T, _> = from_str(s);
assert_eq!(actual, Ok(expected));
fn test_roundtrip<T>(value: T)
T: Serialize + for<'a> Deserialize<'a> + Debug + PartialEq,
let s = to_string(&value).expect("Failed to serialize");
let actual: Result<T, _> = from_str(&s);
assert_eq!(actual, Ok(value));
#[test]
fn test_externally_a_ser() {
let v = EnumStructExternally::VariantA {
foo: 1,
baz: vec![b'a'],
different: Tuple(2, 3),
};
let e = "VariantA(foo:1,bar:(),baz:b\"a\",different:(2,3))";
test_ser(&v, e);
fn test_externally_b_ser() {
let v = EnumStructExternally::VariantB {
let e = "VariantB(foo:1,bar:(),baz:b\"a\")";
fn test_internally_a_ser() {
let v = EnumStructInternally::VariantA {
bar: 2,
different: 3,
let e = "(type:\"VariantA\",foo:1,bar:2,different:3)";
fn test_internally_b_ser() {
let v = EnumStructInternally::VariantB { foo: 1, bar: 2 };
let e = "(type:\"VariantB\",foo:1,bar:2)";
fn test_adjacently_a_ser() {
let v = EnumStructAdjacently::VariantA {
foo: 1.0,
bar: (),
different: Inner::Foo,
let e = "(type:VariantA,content:(foo:1.0,bar:(),different:Foo))";
fn test_adjacently_b_ser() {
let v = EnumStructAdjacently::VariantB { foo: 1.0, bar: () };
let e = "(type:VariantB,content:(foo:1.0,bar:()))";
fn test_untagged_a_ser() {
let v = EnumStructUntagged::VariantA {
let e = "(foo:1,bar:2,different:3)";
fn test_untagged_b_ser() {
let v = EnumStructUntagged::VariantB { foo: 1, bar: 2 };
let e = "(foo:1,bar:2)";
fn test_externally_a_de() {
let s = "VariantA(foo:1,bar:Unit,baz:b\"a\",different:Tuple(2,3))";
let e = EnumStructExternally::VariantA {
test_de(s, e);
fn test_externally_b_de() {
let s = "VariantB(foo:1,bar:Unit,baz:b\"a\")";
let e = EnumStructExternally::VariantB {
fn test_internally_a_de() {
let s = "(type:\"VariantA\",foo:1,bar:2,different:3)";
let e = EnumStructInternally::VariantA {
fn test_internally_b_de() {
let s = "(type:\"VariantB\",foo:1,bar:2)";
let e = EnumStructInternally::VariantB { foo: 1, bar: 2 };
fn test_adjacently_a_de() {
let s = "(type:VariantA,content:(foo:1.0,bar:(),different:Foo))";
let e = EnumStructAdjacently::VariantA {
fn test_adjacently_b_de() {
let s = "(type:VariantB,content:(foo:1.0,bar:()))";
let e = EnumStructAdjacently::VariantB { foo: 1.0, bar: () };
fn test_untagged_a_de() {
let s = "(foo:1,bar:2,different:3)";
let e = EnumStructUntagged::VariantA {
fn test_untagged_b_de() {
let s = "(foo:1,bar:2)";
let e = EnumStructUntagged::VariantB { foo: 1, bar: 2 };
fn test_externally_a_roundtrip() {
test_roundtrip(v);
fn test_externally_b_roundtrip() {
fn test_internally_a_roundtrip() {
fn test_internally_b_roundtrip() {
fn test_adjacently_a_roundtrip() {
fn test_adjacently_b_roundtrip() {
fn test_untagged_a_roundtrip() {
fn test_untagged_b_roundtrip() {
enum ByteStr {}
impl ByteStr {
fn serialize<S: serde::Serializer>(data: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_bytes(data)
fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> {
struct ByteStrVisitor;
impl<'de> serde::de::Visitor<'de> for ByteStrVisitor {
type Value = Vec<u8>;
// GRCOV_EXCL_START
fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.write_str("a Rusty byte string")
// GRCOV_EXCL_STOP
fn visit_bytes<E: serde::de::Error>(self, bytes: &[u8]) -> Result<Self::Value, E> {
Ok(bytes.to_vec())
deserializer.deserialize_bytes(ByteStrVisitor)