decodable trait impl in progress
This commit is contained in:
parent
7e287755ca
commit
0de75185aa
@ -1,15 +1,43 @@
|
||||
use std::fmt;
|
||||
use std::error::Error as StdError;
|
||||
use bytes::FromBytesError;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum DecoderError {
|
||||
FromBytesError(FromBytesError),
|
||||
RlpIsTooShort,
|
||||
RlpExpectedToBeList,
|
||||
RlpExpectedToBeData,
|
||||
}
|
||||
|
||||
impl StdError for DecoderError {
|
||||
fn description(&self) -> &str {
|
||||
"builder error"
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DecoderError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FromBytesError> for DecoderError {
|
||||
fn from(err: FromBytesError) -> DecoderError {
|
||||
DecoderError::FromBytesError(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Decoder {
|
||||
type Error;
|
||||
fn read_value<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[u8]) -> Result<T, DecoderError>;
|
||||
|
||||
fn read_value<T, F>(&self, f: F) -> Result<T, Self::Error>
|
||||
where F: FnOnce(&[u8]) -> Result<T, Self::Error>;
|
||||
|
||||
fn read_list<T, F>(&self, f: F) -> Result<T, Self::Error>
|
||||
where F: FnOnce(&[Self]) -> Result<T, Self::Error>;
|
||||
fn read_list<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[Self]) -> Result<T, DecoderError>;
|
||||
}
|
||||
|
||||
pub trait Decodable: Sized {
|
||||
fn decode<T, D>(decoder: &D) -> Result<T, D::Error> where D: Decoder;
|
||||
fn decode<T, D>(decoder: &D) -> Result<T, DecoderError> where D: Decoder;
|
||||
}
|
||||
|
||||
pub trait View<'a, 'view>: Sized {
|
||||
@ -18,7 +46,6 @@ pub trait View<'a, 'view>: Sized {
|
||||
type Data;
|
||||
type Item;
|
||||
type Iter;
|
||||
type Error;
|
||||
|
||||
/// Creates a new instance of `Rlp` reader
|
||||
fn new(bytes: &'a [u8]) -> Self;
|
||||
@ -179,7 +206,7 @@ pub trait View<'a, 'view>: Sized {
|
||||
/// ```
|
||||
fn iter(&'view self) -> Self::Iter;
|
||||
|
||||
fn as_val<T>(&self) -> Result<T, Self::Error> where T: Decodable;
|
||||
fn as_val<T>(&self) -> Result<T, DecoderError> where T: Decodable;
|
||||
}
|
||||
|
||||
pub trait Encoder {
|
||||
|
@ -1,8 +1,40 @@
|
||||
//! Rlp serialization module
|
||||
//!
|
||||
//! Allows encoding, decoding, and view onto rlp-slice
|
||||
//!
|
||||
//!# What should you use when?
|
||||
//!
|
||||
//!### Use `encode` function when:
|
||||
//! * You want to encode something inline.
|
||||
//! * You do not work on big set of data.
|
||||
//! * You want to encode whole data structure at once.
|
||||
//!
|
||||
//!### Use `decode` function when:
|
||||
//! * You want to decode something inline.
|
||||
//! * You do not work on big set of data.
|
||||
//! * You want to decode whole rlp at once.
|
||||
//!
|
||||
//!### Use `RlpStream` when:
|
||||
//! * You want to encode something in portions.
|
||||
//! * You encode a big set of data.
|
||||
//!
|
||||
//!### Use `Rlp` when:
|
||||
//! * You are working on trusted data (not corrupted).
|
||||
//! * You want to get view onto rlp-slice.
|
||||
//! * You don't want to decode whole rlp at once.
|
||||
//!
|
||||
//!### Use `UntrustedRlp` when:
|
||||
//! * You are working on untrusted data (~corrupted).
|
||||
//! * You need to handle data corruption errors.
|
||||
//! * You are working on input data.
|
||||
//! * You want to get view onto rlp-slice.
|
||||
//! * You don't want to decode whole rlp at once.
|
||||
|
||||
pub mod old;
|
||||
|
||||
pub mod faces;
|
||||
pub mod coders;
|
||||
pub mod rlp;
|
||||
pub mod untrusted_rlp;
|
||||
pub mod rlpstream;
|
||||
|
||||
pub use self::old::*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use super::faces::{View, Decodable};
|
||||
use super::faces::{View, Decodable, DecoderError};
|
||||
use super::untrusted_rlp::*;
|
||||
|
||||
impl<'a> From<UntrustedRlp<'a>> for Rlp<'a> {
|
||||
@ -22,7 +22,6 @@ impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view {
|
||||
type Data = &'a [u8];
|
||||
type Item = Rlp<'a>;
|
||||
type Iter = RlpIterator<'a, 'view>;
|
||||
type Error = DecoderError;
|
||||
|
||||
/// Create a new instance of `Rlp`
|
||||
fn new(bytes: &'a [u8]) -> Rlp<'a> {
|
||||
@ -83,14 +82,14 @@ impl<'a, 'view> View<'a, 'view> for Rlp<'a> where 'a: 'view {
|
||||
self.into_iter()
|
||||
}
|
||||
|
||||
fn as_val<T>(&self) -> Result<T, Self::Error> where T: Decodable {
|
||||
fn as_val<T>(&self) -> Result<T, DecoderError> where T: Decodable {
|
||||
self.rlp.as_val()
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a, 'view> Rlp<'a> where 'a: 'view {
|
||||
fn reader_as_val<T, R>(r: &R) -> T where R: View<'a, 'view>, T: Decodable {
|
||||
let res: Result<T, R::Error> = r.as_val();
|
||||
let res: Result<T, DecoderError> = r.as_val();
|
||||
res.unwrap_or_else(|_| panic!())
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
use std::fmt;
|
||||
use std::cell::Cell;
|
||||
use std::error::Error as StdError;
|
||||
use bytes::{FromBytes, FromBytesError};
|
||||
use super::faces::{View, Decoder, Decodable};
|
||||
use bytes::{FromBytes};
|
||||
use super::faces::{View, Decoder, Decodable, DecoderError};
|
||||
|
||||
/// rlp offset
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
@ -42,31 +40,6 @@ impl PayloadInfo {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum DecoderError {
|
||||
FromBytesError(FromBytesError),
|
||||
RlpIsTooShort,
|
||||
RlpExpectedToBeList,
|
||||
RlpExpectedToBeData,
|
||||
}
|
||||
impl StdError for DecoderError {
|
||||
fn description(&self) -> &str {
|
||||
"builder error"
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DecoderError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FromBytesError> for DecoderError {
|
||||
fn from(err: FromBytesError) -> DecoderError {
|
||||
DecoderError::FromBytesError(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// Data-oriented view onto rlp-slice.
|
||||
///
|
||||
/// This is immutable structere. No operations change it.
|
||||
@ -94,7 +67,6 @@ impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view {
|
||||
type Data = Result<&'a [u8], DecoderError>;
|
||||
type Item = Result<UntrustedRlp<'a>, DecoderError>;
|
||||
type Iter = UntrustedRlpIterator<'a, 'view>;
|
||||
type Error = DecoderError;
|
||||
|
||||
//returns new instance of `UntrustedRlp`
|
||||
fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> {
|
||||
@ -200,7 +172,7 @@ impl<'a, 'view> View<'a, 'view> for UntrustedRlp<'a> where 'a: 'view {
|
||||
self.into_iter()
|
||||
}
|
||||
|
||||
fn as_val<T>(&self) -> Result<T, Self::Error> where T: Decodable {
|
||||
fn as_val<T>(&self) -> Result<T, DecoderError> where T: Decodable {
|
||||
// optimize, so it doesn't use clone (although This clone is cheap)
|
||||
T::decode(&BasicDecoder::new(self.clone()))
|
||||
}
|
||||
@ -305,10 +277,8 @@ impl<'a> BasicDecoder<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Decoder for BasicDecoder<'a> {
|
||||
type Error = DecoderError;
|
||||
|
||||
fn read_value<T, F>(&self, f: F) -> Result<T, Self::Error>
|
||||
where F: FnOnce(&[u8]) -> Result<T, Self::Error> {
|
||||
fn read_value<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[u8]) -> Result<T, DecoderError> {
|
||||
|
||||
let bytes = self.rlp.raw();
|
||||
|
||||
@ -331,8 +301,8 @@ impl<'a> Decoder for BasicDecoder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_list<T, F>(&self, f: F) -> Result<T, Self::Error>
|
||||
where F: FnOnce(&[Self]) -> Result<T, Self::Error> {
|
||||
fn read_list<T, F>(&self, f: F) -> Result<T, DecoderError>
|
||||
where F: FnOnce(&[Self]) -> Result<T, DecoderError> {
|
||||
|
||||
let v: Vec<BasicDecoder<'a>> = self.rlp.iter()
|
||||
.map(| i | BasicDecoder::new(i))
|
||||
@ -341,3 +311,20 @@ impl<'a> Decoder for BasicDecoder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for T where T: FromBytes {
|
||||
fn decode<R, D>(decoder: &D) -> Result<R, DecoderError> where D: Decoder {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for Vec<T> where T: Decodable {
|
||||
fn decode<R, D>(decoder: &D) -> Result<R, DecoderError> where D: Decoder {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for Vec<u8> {
|
||||
fn decode<R, D>(decoder: &D) -> Result<R, DecoderError> where D: Decoder {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user