1 unstable release

0.1.0 Feb 2, 2024

#913 in Encoding

22 downloads per month

MIT/Apache

50KB
1.5K SLoC

serde_dbgfmt

ci

Deserialize the output of #[derive(Debug)] using serde.

This library allows you to deserialize the debug representation of rust types via serde. This includes all types which use #[derive(Debug)] but also any other ones that use the debug helpers in std::fmt to output their debug representation.

Example

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Test {
    message: String,
}

let text = format!("{:?}", Test { message: "Hello, World!".into() });
let value: Test = serde_defmt::from_str(&text)
    .expect("failed to deserialize from the debug repr");

assert_eq!(value.message, "Hello, World!");

Caveats

  • This library parses the format emitted by the debug helpers in std::fmt. This should cover all types with #[derive(Debug)] and many custom impls but since custom impls can do anything it is not guaranteed to work.
  • The debug format emitted by the debug helpers is not guaranteed to be stable. While it has remained rather stable in the past there is no guarantee that it will not be changed in the future.
  • The names of structs used to deserialize must match those in the text debug representation. You can use #[serde(rename = "..")] if you want to use a different struct name in your codebase.

See Also

  • The serde_fmt library is the inverse of this crate. It allows you to print a debug representation for any struct which implements Serialize.

Dependencies

~115–365KB