GitXplorerGitXplorer
R

serdebug

public
65 stars
2 forks
0 issues

Commits

List of commits on branch main.
Unverified
ca822d160fe20e2a037a26267b5ca001a3cb795f

Fix CI conditions

RRReverser committed 4 months ago
Unverified
07cf718cb99c9e1459c94884b6a4522fc09c0148

Verify no-std on CI

RRReverser committed 4 months ago
Unverified
2ba49e465e8d61d7b65a9bdcfe56c2ac108ec6c2

Simplify simple_impl macro

RRReverser committed 4 months ago
Unverified
8959810d1ef23e01e6fe847a1c620e41e71c4e5b

chore: Release

RRReverser committed 4 months ago
Unverified
b98f659f5644253fe66acc0314670c98070b3394

Avoid double-reference in `debug` wrapper

RRReverser committed 4 months ago
Unverified
0a8825ae431256651c568fac77a0f368290061c0

#![no_std]

RRReverser committed 4 months ago

README

The README file for this repository.

serdebug

Crates.io docs.rs

This is a drop-in replacement for #[derive(Debug)] that uses serde::Serialize under the hood to provide advanced control over output serialisation.

Usage

By default, the generated code will produce exactly same output as #[derive(Debug)] for compatibility.

However, this might be not very interesting, so let's add some serde attributes to see how we can control debug representation:

use serde::Serialize;
use serdebug::SerDebug;

pub struct CustomType(u32);

#[derive(Serialize, SerDebug)]
pub enum MyEnum {
    // renaming items works as expected
    #[serde(rename = "AAAAAAA!!!")]
    A,

    B(u32),

    C { flag: bool },
}

#[derive(Serialize, SerDebug)]
// so does bulk rename on containers
#[serde(rename_all = "PascalCase")]
pub struct MyStruct {
    number: u32,

    my_enum: Vec<MyEnum>,

    // we might want to hide some items from the output
    #[serde(skip_serializing)]
    hidden: bool,

    // or override serialisation for otherwise verbose wrappers or
    // third-party types that don't implement `Debug` and/or `Serialize`
    #[serde(serialize_with = "custom_serialize")]
    custom_type: CustomType,
}

fn custom_serialize<S: serde::Serializer>(value: &CustomType, ser: S) -> Result<S::Ok, S::Error> {
    value.0.serialize(ser)
}

fn main() {
    let s = MyStruct {
        number: 42,
        my_enum: vec![MyEnum::A, MyEnum::B(10), MyEnum::C { flag: true }],
        hidden: true,
        custom_type: CustomType(20),
    };

    assert_eq!(format!("{s:#?}"), "
MyStruct {
    Number: 42,
    MyEnum: [
        AAAAAAA!!!,
        B(
            10,
        ),
        C {
            flag: true,
        },
    ],
    CustomType: 20,
}
".trim());
}