Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rreverser/serdebug
Serde-based replacement for #[derive(Debug)]
https://github.com/rreverser/serdebug
debug formatting rust rust-lang serde serialization
Last synced: 5 days ago
JSON representation
Serde-based replacement for #[derive(Debug)]
- Host: GitHub
- URL: https://github.com/rreverser/serdebug
- Owner: RReverser
- License: mit
- Created: 2018-02-02T20:21:20.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-09-30T22:12:20.000Z (4 months ago)
- Last Synced: 2025-01-18T20:50:54.908Z (12 days ago)
- Topics: debug, formatting, rust, rust-lang, serde, serialization
- Language: Rust
- Homepage: https://crates.io/crates/serdebug
- Size: 43 KB
- Stars: 65
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# serdebug
[![Crates.io](https://img.shields.io/crates/v/serdebug.svg)](https://crates.io/crates/serdebug)
[![docs.rs](https://docs.rs/serdebug/badge.svg)](https://docs.rs/serdebug)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:
```rust
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,
// 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(value: &CustomType, ser: S) -> Result {
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());
}
```