Initial import of rusticata-macros-3.1.0

Bug: 193833713
Change-Id: I8c64a1f7a387fb4162d80d201b1cfcd7172ed0f3
diff --git a/src/combinator.rs b/src/combinator.rs
new file mode 100644
index 0000000..f5de774
--- /dev/null
+++ b/src/combinator.rs
@@ -0,0 +1,210 @@
+//! General purpose combinators
+
+use nom::bytes::streaming::take;
+use nom::combinator::map_parser;
+pub use nom::error::{make_error, ErrorKind, ParseError};
+pub use nom::{IResult, Needed};
+use nom::{InputIter, InputTake};
+use nom::{InputLength, ToUsize};
+
+/// Read the entire slice as a big endian unsigned integer, up to 8 bytes
+#[inline]
+pub fn be_var_u64<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [u8], u64, E> {
+    if input.is_empty() {
+        return Err(nom::Err::Incomplete(Needed::new(1)));
+    }
+    if input.len() > 8 {
+        return Err(nom::Err::Error(make_error(input, ErrorKind::TooLarge)));
+    }
+    let mut res = 0u64;
+    for byte in input {
+        res = (res << 8) + *byte as u64;
+    }
+
+    Ok((&b""[..], res))
+}
+
+/// Read the entire slice as a little endian unsigned integer, up to 8 bytes
+#[inline]
+pub fn le_var_u64<'a, E: ParseError<&'a [u8]>>(input: &'a [u8]) -> IResult<&'a [u8], u64, E> {
+    if input.is_empty() {
+        return Err(nom::Err::Incomplete(Needed::new(1)));
+    }
+    if input.len() > 8 {
+        return Err(nom::Err::Error(make_error(input, ErrorKind::TooLarge)));
+    }
+    let mut res = 0u64;
+    for byte in input.iter().rev() {
+        res = (res << 8) + *byte as u64;
+    }
+
+    Ok((&b""[..], res))
+}
+
+/// Read a slice as a big-endian value.
+#[inline]
+pub fn parse_hex_to_u64<S>(i: &[u8], size: S) -> IResult<&[u8], u64>
+where
+    S: ToUsize + Copy,
+{
+    map_parser(take(size.to_usize()), be_var_u64)(i)
+}
+
+/// Apply combinator, automatically converts between errors if the underlying type supports it
+pub fn upgrade_error<I, O, E1: ParseError<I>, E2: ParseError<I>, F>(
+    mut f: F,
+) -> impl FnMut(I) -> IResult<I, O, E2>
+where
+    F: FnMut(I) -> IResult<I, O, E1>,
+    E2: From<E1>,
+{
+    move |i| f(i).map_err(nom::Err::convert)
+}
+
+/// Create a combinator that returns the provided value, and input unchanged
+pub fn pure<I, O, E: ParseError<I>>(val: O) -> impl Fn(I) -> IResult<I, O, E>
+where
+    O: Clone,
+{
+    move |input: I| Ok((input, val.clone()))
+}
+
+/// Return a closure that takes `len` bytes from input, and applies `parser`.
+pub fn flat_take<I, C, O, E: ParseError<I>, F>(len: C, parser: F) -> impl Fn(I) -> IResult<I, O, E>
+where
+    I: InputTake + InputLength + InputIter,
+    C: ToUsize + Copy,
+    F: Fn(I) -> IResult<I, O, E>,
+{
+    // Note: this is the same as `map_parser(take(len), parser)`
+    move |input: I| {
+        let (input, o1) = take(len.to_usize())(input)?;
+        let (_, o2) = parser(o1)?;
+        Ok((input, o2))
+    }
+}
+
+/// Take `len` bytes from `input`, and apply `parser`.
+pub fn flat_takec<I: Clone, O, E: ParseError<I>, C, F>(
+    input: I,
+    len: C,
+    parser: F,
+) -> IResult<I, O, E>
+where
+    C: ToUsize + Copy,
+    F: Fn(I) -> IResult<I, O, E>,
+    I: InputTake + InputLength + InputIter,
+    O: InputLength,
+{
+    flat_take(len, parser)(input)
+}
+
+/// Helper macro for nom parsers: run first parser if condition is true, else second parser
+pub fn cond_else<I: Clone, O, E: ParseError<I>, C, F, G>(
+    cond: C,
+    first: F,
+    second: G,
+) -> impl Fn(I) -> IResult<I, O, E>
+where
+    C: Fn() -> bool,
+    F: Fn(I) -> IResult<I, O, E>,
+    G: Fn(I) -> IResult<I, O, E>,
+{
+    move |input: I| {
+        if cond() {
+            first(input)
+        } else {
+            second(input)
+        }
+    }
+}
+
+/// Align input value to the next multiple of n bytes
+/// Valid only if n is a power of 2
+pub const fn align_n2(x: usize, n: usize) -> usize {
+    (x + (n - 1)) & !(n - 1)
+}
+
+/// Align input value to the next multiple of 4 bytes
+pub const fn align32(x: usize) -> usize {
+    (x + 3) & !3
+}
+
+#[cfg(test)]
+mod tests {
+    use super::{align32, be_var_u64, cond_else, flat_take, pure};
+    use nom::bytes::streaming::take;
+    use nom::number::streaming::{be_u16, be_u32, be_u8};
+    use nom::{Err, IResult, Needed};
+
+    #[test]
+    fn test_be_var_u64() {
+        let res: IResult<&[u8], u64> = be_var_u64(b"\x12\x34\x56");
+        let (_, v) = res.expect("be_var_u64 failed");
+        assert_eq!(v, 0x123456);
+    }
+
+    #[test]
+    fn test_flat_take() {
+        let input = &[0x00, 0x01, 0xff];
+        // read first 2 bytes and use correct combinator: OK
+        let res: IResult<&[u8], u16> = flat_take(2u8, be_u16)(input);
+        assert_eq!(res, Ok((&input[2..], 0x0001)));
+        // read 3 bytes and use 2: OK (some input is just lost)
+        let res: IResult<&[u8], u16> = flat_take(3u8, be_u16)(input);
+        assert_eq!(res, Ok((&b""[..], 0x0001)));
+        // read 2 bytes and a combinator requiring more bytes
+        let res: IResult<&[u8], u32> = flat_take(2u8, be_u32)(input);
+        assert_eq!(res, Err(Err::Incomplete(Needed::new(2))));
+    }
+
+    #[test]
+    fn test_flat_take_str() {
+        let input = "abcdef";
+        // read first 2 bytes and use correct combinator: OK
+        let res: IResult<&str, &str> = flat_take(2u8, take(2u8))(input);
+        assert_eq!(res, Ok(("cdef", "ab")));
+        // read 3 bytes and use 2: OK (some input is just lost)
+        let res: IResult<&str, &str> = flat_take(3u8, take(2u8))(input);
+        assert_eq!(res, Ok(("def", "ab")));
+        // read 2 bytes and a use combinator requiring more bytes
+        let res: IResult<&str, &str> = flat_take(2u8, take(4u8))(input);
+        assert_eq!(res, Err(Err::Incomplete(Needed::Unknown)));
+    }
+
+    #[test]
+    fn test_cond_else() {
+        let input = &[0x01][..];
+        let empty = &b""[..];
+        let a = 1;
+        fn parse_u8(i: &[u8]) -> IResult<&[u8], u8> {
+            be_u8(i)
+        }
+        assert_eq!(
+            cond_else(|| a == 1, parse_u8, pure(0x02))(input),
+            Ok((empty, 0x01))
+        );
+        assert_eq!(
+            cond_else(|| a == 1, parse_u8, pure(0x02))(input),
+            Ok((empty, 0x01))
+        );
+        assert_eq!(
+            cond_else(|| a == 2, parse_u8, pure(0x02))(input),
+            Ok((input, 0x02))
+        );
+        assert_eq!(
+            cond_else(|| a == 1, pure(0x02), parse_u8)(input),
+            Ok((input, 0x02))
+        );
+        let res: IResult<&[u8], u8> = cond_else(|| a == 1, parse_u8, parse_u8)(input);
+        assert_eq!(res, Ok((empty, 0x01)));
+    }
+
+    #[test]
+    fn test_align32() {
+        assert_eq!(align32(3), 4);
+        assert_eq!(align32(4), 4);
+        assert_eq!(align32(5), 8);
+        assert_eq!(align32(5usize), 8);
+    }
+}
diff --git a/src/debug.rs b/src/debug.rs
new file mode 100644
index 0000000..eb4e821
--- /dev/null
+++ b/src/debug.rs
@@ -0,0 +1,54 @@
+//! Helper functions and structures for debugging purpose
+
+use std::fmt;
+
+/// Wrapper for printing value as u8 hex data
+pub struct HexU8(pub u8);
+
+impl fmt::Debug for HexU8 {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        write!(fmt, "0x{:02x}", self.0)
+    }
+}
+
+/// Wrapper for printing value as u16 hex data
+pub struct HexU16(pub u16);
+
+impl fmt::Debug for HexU16 {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        write!(fmt, "0x{:04x}", self.0)
+    }
+}
+
+/// Wrapper for printing slice as hex data
+pub struct HexSlice<'a>(pub &'a [u8]);
+
+impl<'a> fmt::Debug for HexSlice<'a> {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        let s: Vec<_> = self.0.iter().map(|&i| format!("{:02x}", i)).collect();
+        write!(fmt, "[{}]", s.join(" "))
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::debug;
+
+    #[test]
+    fn debug_print_hexu8() {
+        assert_eq!(format!("{:?}", debug::HexU8(18)), "0x12");
+    }
+
+    #[test]
+    fn debug_print_hexu16() {
+        assert_eq!(format!("{:?}", debug::HexU16(32769)), "0x8001");
+    }
+
+    #[test]
+    fn debug_print_hexslice() {
+        assert_eq!(
+            format!("{:?}", debug::HexSlice(&[15, 16, 17, 18, 19, 20])),
+            "[0f 10 11 12 13 14]"
+        );
+    }
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..c6f4344
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,51 @@
+//! # Rusticata-macros
+//!
+//! Helper macros for the [rusticata](https://github.com/rusticata) project.
+//!
+//! This crate contains some additions to [nom](https://github.com/Geal/nom).
+//!
+//! For example, the `error_if!` macro allows to test a condition and return an error from the parser if the condition
+//! fails:
+//!
+//! ```rust
+//! # extern crate rusticata_macros;
+//! # extern crate nom;
+//! # use nom::{do_parse, take, IResult};
+//! # use nom::error::ErrorKind;
+//! # use nom::number::streaming::be_u8;
+//! use rusticata_macros::error_if;
+//! # fn parser(s:&[u8]) {
+//! let r : IResult<&[u8],()> = do_parse!(
+//!     s,
+//!     l: be_u8 >>
+//!     error_if!(l < 4, ErrorKind::Verify) >>
+//!     data: take!(l - 4) >>
+//!     (())
+//!     );
+//! # }
+//! ```
+//!
+//! See the documentation for more details and examples.
+
+#![deny(
+    missing_docs,
+    unsafe_code,
+    unstable_features,
+    unused_import_braces,
+    unused_qualifications
+)]
+
+#[macro_use]
+extern crate nom;
+
+extern crate core;
+
+pub mod combinator;
+
+pub use macros::*;
+#[macro_use]
+pub mod macros;
+
+pub mod debug;
+mod traits;
+pub use traits::*;
diff --git a/src/macros.rs b/src/macros.rs
new file mode 100644
index 0000000..c74cb4e
--- /dev/null
+++ b/src/macros.rs
@@ -0,0 +1,406 @@
+//! Helper macros
+
+use nom::bytes::complete::take;
+use nom::combinator::map_res;
+use nom::combinator::rest;
+pub use nom::error::{make_error, ErrorKind, ParseError};
+use nom::HexDisplay;
+pub use nom::{IResult, Needed};
+
+#[doc(hidden)]
+pub mod export {
+    pub use core::{fmt, mem, ptr};
+}
+
+/// Helper macro for newtypes: declare associated constants and implement Display trait
+#[macro_export]
+macro_rules! newtype_enum (
+    (@collect_impl, $name:ident, $($key:ident = $val:expr),* $(,)*) => {
+        $( pub const $key : $name = $name($val); )*
+    };
+
+    (@collect_disp, $name:ident, $f:ident, $m:expr, $($key:ident = $val:expr),* $(,)*) => {
+        match $m {
+            $( $val => write!($f, stringify!{$key}), )*
+            n => write!($f, "{}({} / 0x{:x})", stringify!{$name}, n, n)
+        }
+    };
+
+    // entry
+    (impl $name:ident {$($body:tt)*}) => (
+        #[allow(non_upper_case_globals)]
+        impl $name {
+            newtype_enum!{@collect_impl, $name, $($body)*}
+        }
+    );
+
+    // entry with display
+    (impl display $name:ident {$($body:tt)*}) => (
+        newtype_enum!(impl $name { $($body)* });
+
+        impl $crate::export::fmt::Display for $name {
+            fn fmt(&self, f: &mut $crate::export::fmt::Formatter) -> $crate::export::fmt::Result {
+                newtype_enum!(@collect_disp, $name, f, self.0, $($body)*)
+            }
+        }
+    );
+
+    // entry with display and debug
+    (impl debug $name:ident {$($body:tt)*}) => (
+        newtype_enum!(impl display $name { $($body)* });
+
+        impl $crate::export::fmt::Debug for $name {
+            fn fmt(&self, f: &mut $crate::export::fmt::Formatter) -> $crate::export::fmt::Result {
+                write!(f, "{}", self)
+            }
+        }
+    );
+);
+
+/// Helper macro for nom parsers: raise error if the condition is true
+///
+/// This macro is used when using custom errors
+#[macro_export]
+macro_rules! custom_check (
+  ($i:expr, $cond:expr, $err:expr) => (
+    {
+      if $cond {
+        Err(::nom::Err::Error($err))
+      } else {
+        Ok(($i, ()))
+      }
+    }
+  );
+);
+
+/// Helper macro for nom parsers: raise error if the condition is true
+///
+/// This macro is used when using `ErrorKind`
+#[macro_export]
+macro_rules! error_if (
+  ($i:expr, $cond:expr, $err:expr) => (
+    {
+      use nom::error_position;
+      if $cond {
+        Err(::nom::Err::Error(error_position!($i, $err)))
+      } else {
+        Ok(($i, ()))
+      }
+    }
+  );
+);
+
+/// Helper macro for nom parsers: raise error if input is not empty
+///
+/// Deprecated - use `nom::eof`
+#[macro_export]
+#[deprecated(since = "2.0.0")]
+macro_rules! empty (
+  ($i:expr,) => (
+    {
+      use nom::eof;
+      eof!($i,)
+    }
+  );
+);
+
+/// Helper macro for nom parsers: run first parser if condition is true, else second parser
+#[macro_export]
+macro_rules! cond_else (
+  ($i:expr, $cond:expr, $expr_then:ident!($($args_then:tt)*), $expr_else:ident!($($args_else:tt)*)) => (
+    {
+      if $cond { $expr_then!($i, $($args_then)*) }
+      else { $expr_else!($i, $($args_else)*) }
+    }
+  );
+  ($i:expr, $cond:expr, $expr_then:expr, $expr_else:ident!($($args_else:tt)*)) => (
+      cond_else!($i, $cond, call!($expr_then), $expr_else!($($args_else)*))
+  );
+  ($i:expr, $cond:expr, $expr_then:ident!($($args_then:tt)*), $expr_else:expr) => (
+      cond_else!($i, $cond, $expr_then!($($args_then)*), call!($expr_else))
+  );
+  ($i:expr, $cond:expr, $expr_then:expr, $expr_else:expr) => (
+      cond_else!($i, $cond, call!($expr_then), call!($expr_else))
+  );
+);
+
+/// Dump the remaining bytes to stderr, formatted as hex
+pub fn dbg_dmp_rest(i: &[u8]) -> IResult<&[u8], ()> {
+    map!(i, peek!(call!(rest)), |r| eprintln!("\n{}\n", r.to_hex(16)))
+}
+
+#[deprecated(since = "3.0.1", note = "please use `be_var_u64` instead")]
+/// Read an entire slice as a big-endian value.
+///
+/// Returns the value as `u64`. This function checks for integer overflows, and returns a
+/// `Result::Err` value if the value is too big.
+pub fn bytes_to_u64(s: &[u8]) -> Result<u64, &'static str> {
+    let mut u: u64 = 0;
+
+    if s.is_empty() {
+        return Err("empty");
+    };
+    if s.len() > 8 {
+        return Err("overflow");
+    }
+    for &c in s {
+        let u1 = u << 8;
+        u = u1 | (c as u64);
+    }
+
+    Ok(u)
+}
+
+/// Read a slice as a big-endian value.
+#[macro_export]
+macro_rules! parse_hex_to_u64 (
+    ( $i:expr, $size:expr ) => {
+        map_res(take($size as usize), $crate::combinator::be_var_u64)($i)
+    };
+);
+
+/// Read 3 bytes as an unsigned integer
+#[deprecated(since = "0.5.0", note = "please use `be_u24` instead")]
+#[allow(deprecated)]
+#[inline]
+pub fn parse_uint24(i: &[u8]) -> IResult<&[u8], u64> {
+    map_res(take(3usize), bytes_to_u64)(i)
+}
+
+//named!(parse_hex4<&[u8], u64>, parse_hex_to_u64!(4));
+
+/// Parse a slice and return a fixed-sized array of bytes
+///
+/// This creates a copy of input data
+/// Uses unsafe code
+#[macro_export]
+macro_rules! slice_fixed(
+    ( $i:expr, $count:expr ) => (
+        {
+            let cnt = $count;
+            let ires: IResult<_,_> = if $i.len() < cnt {
+                Err(::nom::Err::Incomplete(Needed::new(cnt)))
+            } else {
+                let mut res: [u8; $count] = unsafe {
+                    $crate::export::mem::MaybeUninit::uninit().assume_init()
+                };
+                unsafe{$crate::export::ptr::copy($i.as_ptr(), res.as_mut_ptr(), cnt)};
+                Ok((&$i[cnt..],res))
+            };
+            ires
+        }
+    );
+);
+
+/// Combination and flat_map! and take! as first combinator
+#[macro_export]
+macro_rules! flat_take (
+    ($i:expr, $len:expr, $f:ident) => ({
+        if $i.len() < $len { Err(::nom::Err::Incomplete(::nom::Needed::new($len))) }
+        else {
+            let taken = &$i[0..$len];
+            let rem = &$i[$len..];
+            match $f(taken) {
+                Ok((_,res)) => Ok((rem,res)),
+                Err(e)      => Err(e)
+            }
+        }
+    });
+    ($i:expr, $len:expr, $submac:ident!( $($args:tt)*)) => ({
+        if $i.len() < $len { Err(::nom::Err::Incomplete(::nom::Needed::new($len))) }
+        else {
+            let taken = &$i[0..$len];
+            let rem = &$i[$len..];
+            match $submac!(taken, $($args)*) {
+                Ok((_,res)) => Ok((rem,res)),
+                Err(e)      => Err(e)
+            }
+        }
+    });
+);
+
+/// Apply combinator, trying to "upgrade" error to next error type (using the `Into` or `From`
+/// traits).
+#[macro_export]
+macro_rules! upgrade_error (
+    ($i:expr, $submac:ident!( $($args:tt)*) ) => ({
+        upgrade_error!( $submac!( $i, $($args)* ) )
+    });
+    ($i:expr, $f:expr) => ({
+        upgrade_error!( call!($i, $f) )
+    });
+    ($e:expr) => ({
+        match $e {
+            Ok(o) => Ok(o),
+            Err(::nom::Err::Error(e)) => Err(::nom::Err::Error(e.into())),
+            Err(::nom::Err::Failure(e)) => Err(::nom::Err::Failure(e.into())),
+            Err(::nom::Err::Incomplete(i)) => Err(::nom::Err::Incomplete(i)),
+        }
+    });
+);
+
+/// Apply combinator, trying to "upgrade" error to next error type (using the `Into` or `From`
+/// traits).
+#[macro_export]
+macro_rules! upgrade_error_to (
+    ($i:expr, $ty:ty, $submac:ident!( $($args:tt)*) ) => ({
+        upgrade_error_to!( $ty, $submac!( $i, $($args)* ) )
+    });
+    ($i:expr, $ty:ty, $f:expr) => ({
+        upgrade_error_to!( $ty, call!($i, $f) )
+    });
+    ($ty:ty, $e:expr) => ({
+        match $e {
+            Ok(o) => Ok(o),
+            Err(::nom::Err::Error(e)) => Err(::nom::Err::Error(e.into::<$ty>())),
+            Err(::nom::Err::Failure(e)) => Err(::nom::Err::Failure(e.into::<$ty>())),
+            Err(::nom::Err::Incomplete(i)) => Err(::nom::Err::Incomplete(i)),
+        }
+    });
+);
+
+/// Nom combinator that returns the given expression unchanged
+#[macro_export]
+macro_rules! q {
+    ($i:expr, $x:expr) => {{
+        Ok(($i, $x))
+    }};
+}
+
+/// Align input value to the next multiple of n bytes
+/// Valid only if n is a power of 2
+#[macro_export]
+macro_rules! align_n2 {
+    ($x:expr, $n:expr) => {
+        ($x + ($n - 1)) & !($n - 1)
+    };
+}
+
+/// Align input value to the next multiple of 4 bytes
+#[macro_export]
+macro_rules! align32 {
+    ($x:expr) => {
+        $crate::align_n2!($x, 4)
+    };
+}
+
+#[cfg(test)]
+mod tests {
+    use nom::error::ErrorKind;
+    use nom::number::streaming::{be_u16, be_u32, be_u8};
+    use nom::{Err, IResult, Needed};
+
+    #[test]
+    #[allow(unsafe_code)]
+    fn test_slice_fixed() {
+        let empty = &b""[..];
+        let b = &[0x01, 0x02, 0x03, 0x04, 0x05];
+
+        let res = slice_fixed!(b, 4);
+        assert_eq!(res, Ok((&b[4..], [1, 2, 3, 4])));
+
+        // can we still use the result ?
+        match res {
+            Ok((rem, _)) => {
+                let res2: IResult<&[u8], u8> = be_u8(rem);
+                assert_eq!(res2, Ok((empty, 5)));
+            }
+            _ => (),
+        }
+    }
+
+    #[test]
+    #[allow(unsafe_code)]
+    fn test_slice_fixed_incomplete() {
+        let b = &[0x01, 0x02, 0x03, 0x04, 0x05];
+        let res = slice_fixed!(b, 8);
+        assert_eq!(res, Err(Err::Incomplete(Needed::new(8))));
+    }
+
+    #[test]
+    fn test_error_if() {
+        let empty = &b""[..];
+        let res: IResult<&[u8], ()> = error_if!(empty, true, ErrorKind::Tag);
+        assert_eq!(res, Err(Err::Error(error_position!(empty, ErrorKind::Tag))));
+    }
+
+    #[test]
+    fn test_cond_else() {
+        let input = &[0x01][..];
+        let empty = &b""[..];
+        let a = 1;
+        fn parse_u8(i: &[u8]) -> IResult<&[u8], u8> {
+            be_u8(i)
+        }
+        assert_eq!(
+            cond_else!(input, a == 1, call!(parse_u8), value!(0x02)),
+            Ok((empty, 0x01))
+        );
+        assert_eq!(
+            cond_else!(input, a == 1, parse_u8, value!(0x02)),
+            Ok((empty, 0x01))
+        );
+        assert_eq!(
+            cond_else!(input, a == 2, parse_u8, value!(0x02)),
+            Ok((input, 0x02))
+        );
+        assert_eq!(
+            cond_else!(input, a == 1, value!(0x02), parse_u8),
+            Ok((input, 0x02))
+        );
+        let res: IResult<&[u8], u8> = cond_else!(input, a == 1, parse_u8, parse_u8);
+        assert_eq!(res, Ok((empty, 0x01)));
+    }
+
+    #[test]
+    fn test_newtype_enum() {
+        #[derive(Debug, PartialEq, Eq)]
+        struct MyType(pub u8);
+
+        newtype_enum! {
+            impl display MyType {
+                Val1 = 0,
+                Val2 = 1
+            }
+        }
+
+        assert_eq!(MyType(0), MyType::Val1);
+        assert_eq!(MyType(1), MyType::Val2);
+
+        assert_eq!(format!("{}", MyType(0)), "Val1");
+        assert_eq!(format!("{}", MyType(4)), "MyType(4 / 0x4)");
+    }
+    #[test]
+    fn test_flat_take() {
+        let input = &[0x00, 0x01, 0xff];
+        // read first 2 bytes and use correct combinator: OK
+        let res: IResult<&[u8], u16> = flat_take!(input, 2, be_u16);
+        assert_eq!(res, Ok((&input[2..], 0x0001)));
+        // read 3 bytes and use 2: OK (some input is just lost)
+        let res: IResult<&[u8], u16> = flat_take!(input, 3, be_u16);
+        assert_eq!(res, Ok((&b""[..], 0x0001)));
+        // read 2 bytes and a combinator requiring more bytes
+        let res: IResult<&[u8], u32> = flat_take!(input, 2, be_u32);
+        assert_eq!(res, Err(Err::Incomplete(Needed::new(2))));
+        // test with macro as sub-combinator
+        let res: IResult<&[u8], u16> = flat_take!(input, 2, be_u16);
+        assert_eq!(res, Ok((&input[2..], 0x0001)));
+    }
+
+    #[test]
+    fn test_q() {
+        let empty = &b""[..];
+        let res: IResult<&[u8], &str, ErrorKind> = q!(empty, "test");
+        assert_eq!(res, Ok((empty, "test")));
+    }
+
+    #[test]
+    fn test_align32() {
+        assert_eq!(align32!(3), 4);
+        assert_eq!(align32!(4), 4);
+        assert_eq!(align32!(5), 8);
+        assert_eq!(align32!(5u32), 8);
+        assert_eq!(align32!(5i32), 8);
+        assert_eq!(align32!(5usize), 8);
+    }
+}
diff --git a/src/traits.rs b/src/traits.rs
new file mode 100644
index 0000000..38f1402
--- /dev/null
+++ b/src/traits.rs
@@ -0,0 +1,9 @@
+//! Common traits
+
+/// Common trait for structures serialization
+pub trait Serialize<O = Vec<u8>> {
+    /// Type of serialization error
+    type Error;
+    /// Try to serialize object
+    fn serialize(&self) -> Result<O, Self::Error>;
+}