display trait implemented for rlp

This commit is contained in:
debris 2016-01-15 21:03:38 +01:00
parent e096d75448
commit e3f1d70353
2 changed files with 36 additions and 0 deletions

View File

@ -1,3 +1,4 @@
use std::fmt;
use rlp::{View, Decodable, DecoderError, UntrustedRlp, PayloadInfo, Prototype}; use rlp::{View, Decodable, DecoderError, UntrustedRlp, PayloadInfo, Prototype};
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> { impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
@ -15,6 +16,12 @@ pub struct Rlp<'a> {
rlp: UntrustedRlp<'a> rlp: UntrustedRlp<'a>
} }
impl<'a> fmt::Display for Rlp<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.rlp)
}
}
impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view { impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view {
type Prototype = Prototype; type Prototype = Prototype;
type PayloadInfo = PayloadInfo; type PayloadInfo = PayloadInfo;

View File

@ -1,4 +1,6 @@
use std::cell::Cell; use std::cell::Cell;
use std::fmt;
use rustc_serialize::hex::ToHex;
use bytes::{FromBytes}; use bytes::{FromBytes};
use rlp::{View, Decoder, Decodable, DecoderError}; use rlp::{View, Decoder, Decodable, DecoderError};
@ -63,6 +65,24 @@ impl<'a> Clone for UntrustedRlp<'a> {
} }
} }
impl<'a> fmt::Display for UntrustedRlp<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self.prototype() {
Ok(Prototype::Null) => write!(f, "null"),
Ok(Prototype::Data(_)) => write!(f, "\"0x{}\"", self.data().unwrap().to_hex()),
Ok(Prototype::List(len)) => {
try!(write!(f, "["));
for i in 0..len-1 {
try!(write!(f, "{}, ", self.at(i).unwrap()));
}
try!(write!(f, "{}", self.at(len - 1).unwrap()));
write!(f, "]")
},
Err(err) => write!(f, "{:?}", err)
}
}
}
impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view { impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view {
type Prototype = Result<Prototype, DecoderError>; type Prototype = Result<Prototype, DecoderError>;
type PayloadInfo = Result<PayloadInfo, DecoderError>; type PayloadInfo = Result<PayloadInfo, DecoderError>;
@ -410,3 +430,12 @@ impl_array_decodable_recursive!(
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 40, 48, 56, 64, 72, 96, 128, 160, 192, 224, 32, 40, 48, 56, 64, 72, 96, 128, 160, 192, 224,
); );
#[test]
fn test_rlp_display() {
use rustc_serialize::hex::FromHex;
let data = "f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".from_hex().unwrap();
let rlp = UntrustedRlp::new(&data);
println!("{}", rlp);
}