From c2ea3bb1a59d27edb545d398c63c304218b84d24 Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Sat, 30 Dec 2017 02:16:31 +0000 Subject: [PATCH] Wrote CustomDebug example for decode. Closes #17. --- decode/README.md | 16 +++++++++++++--- test_type/src/lib.rs | 11 +++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/decode/README.md b/decode/README.md index e272ac8..91c9856 100644 --- a/decode/README.md +++ b/decode/README.md @@ -12,6 +12,11 @@ the `serde::Deserialize` and `std::fmt::Debug` traits. [typed]: https://docs.rs/framed/*/framed/typed/index.html +Note: you must install [`jq`][jq] and have it on your path. +[jq]: https://stedolan.github.io/jq/ + +On Ubuntu you can install it with: `sudo apt install jq`. + Here's an example. The main binary in crate `framed_test_type` writes a serialized and encoded `framed_test_type::Test` struct to stdout; first save that to a file: @@ -79,8 +84,13 @@ OPTIONS: Csv, Debug, Json] ``` -Note: you must install [`jq`][jq] and have it on your path. +To customize the output for your type, try creating a tuple struct +with a custom Debug implementation. See `CustomDebug` in test_type for +an example: -On Ubuntu you can install it with: `sudo apt install jq`. +```text +~/framed-rs % export DECODE_TYPE_NAME=framed_test_type::CustomDebug; +~/framed-rs % < test_data bin/decode_typed -[jq]: https://stedolan.github.io/jq/ +CustomDebug a=0x000000ff, b=0x2 +``` diff --git a/test_type/src/lib.rs b/test_type/src/lib.rs index c7c9f8c..4f6c3e3 100644 --- a/test_type/src/lib.rs +++ b/test_type/src/lib.rs @@ -4,8 +4,19 @@ extern crate serde; #[macro_use] extern crate serde_derive; +use std::fmt::{self, Debug, Formatter}; + #[derive(Debug, Deserialize, Serialize)] pub struct Test { pub a: u32, pub b: u16, } + +#[derive(Deserialize, Serialize)] +pub struct CustomDebug(Test); + +impl Debug for CustomDebug { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + write!(f, "CustomDebug a=0x{:08x}, b=0x{:X}", self.0.a, self.0.b) + } +}