1#[macro_use]
2extern crate nom;
3extern crate byteorder;
4
5use std::error::Error;
6use std::fmt::{self,Display};
7use std::io;
8
9
10mod packet_writer;
11#[macro_use]
12mod macros;
13pub mod l2;
14pub mod l3;
15pub mod l4;
16
17macro_rules! from_error {
18 ( $name:ident, $( $from_name:path ),* ) => (
19 $(
20 impl From<$from_name> for $name {
21 fn from(v: $from_name) -> Self {
22 ConvError(v.description().to_string())
23 }
24 }
25 )*
26 );
27}
28
29#[derive(Debug)]
30pub struct ConvError(String);
31
32from_error!(ConvError, nom::ErrorKind, io::Error);
33
34impl Display for ConvError {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 write!(f, "{}", self.0)
37 }
38}
39
40impl Error for ConvError {
41 fn description(&self) -> &str {
42 self.0.as_str()
43 }
44}
45
46pub trait ParseOps<'a>: Sized {
47 fn to_bytes(self) -> Result<Vec<u8>, ConvError>;
48 fn from_bytes(&'a [u8]) -> Result<Self, ConvError>;
49 fn strip_header(&[u8]) -> Result<&[u8], ConvError>;
50}