1use std::result;
2use lzf;
3use std::io;
4use std::convert::From;
5use std::string::FromUtf8Error;
6use std::num::ParseFloatError;
7
8use byteorder::{BigEndian, LittleEndian, ByteOrder};
9
10pub trait Shift {
11 #[inline]
12 fn shift(&self) -> usize;
13}
14
15pub trait FromBuf
16 where Self: Sized
17{
18 fn from_buf(src: &[u8]) -> Result<Self>;
19}
20
21
22macro_rules! more{
23 ($e: expr) => {
24 if $e {
25 return Err(Error::More);
26 }
27 }
28 }
29
30macro_rules! other{
31 ($e: expr) => {
32 if $e {
33 return Err(Error::Other);
34 }
35 }
36 }
37
38macro_rules! faild{
39 ($e: expr, $situation: expr) => {
40 if $e {
41 return Err(Error::Faild($situation));
42 }
43 }
44 }
45
46macro_rules! choice {
47 ($e: expr) => {
48 match $e {
49 Ok(lp) => return Ok(lp),
50 Err(Error::Other) => {}
51 Err(err) => return Err(err),
52 };
53 }
54 }
55
56
57impl Shift for u8 {
58 #[inline]
59 fn shift(&self) -> usize {
60 1
61 }
62}
63
64impl Shift for u16 {
65 #[inline]
66 fn shift(&self) -> usize {
67 2
68 }
69}
70
71impl Shift for u32 {
72 #[inline]
73 fn shift(&self) -> usize {
74 4
75 }
76}
77
78impl FromBuf for u32 {
79 fn from_buf(src: &[u8]) -> Result<u32> {
80 more!(src.len() < 4);
81 Ok(buf_to_u32(src))
82 }
83}
84
85impl FromBuf for u16 {
86 fn from_buf(src: &[u8]) -> Result<Self> {
87 more!(src.len() < 4);
88 Ok(buf_to_u16(src))
89 }
90}
91
92
93impl FromBuf for u8
94 where Self: Sized
95{
96 fn from_buf(src: &[u8]) -> Result<Self> {
97 more!(src.len() < 1);
98 Ok(src[0])
99 }
100}
101
102pub type Result<T> = result::Result<T, Error>;
103
104#[derive(Debug)]
105pub enum Error {
106 ParserError(String),
107 More,
108 Faild(&'static str),
109 Other,
110 LzfError(lzf::LzfError),
111 IoError(io::Error),
112 FromUtf8Error(FromUtf8Error),
113 ParseFloatError(ParseFloatError),
114}
115
116impl From<io::Error> for Error {
117 fn from(oe: io::Error) -> Error {
118 Error::IoError(oe)
119 }
120}
121
122impl From<ParseFloatError> for Error {
123 fn from(oe: ParseFloatError) -> Error {
124 Error::ParseFloatError(oe)
125 }
126}
127
128impl From<FromUtf8Error> for Error {
129 fn from(oe: FromUtf8Error) -> Error {
130 Error::FromUtf8Error(oe)
131 }
132}
133
134impl From<lzf::LzfError> for Error {
135 fn from(oe: lzf::LzfError) -> Error {
136 Error::LzfError(oe)
137 }
138}
139
140#[inline]
141pub fn buf_to_i32(src: &[u8]) -> i32 {
142 LittleEndian::read_i32(src)
143}
144
145#[inline]
146pub fn buf_to_i32_trim(src: &[u8]) -> i32 {
147 let mut vi32 = 0i32;
148 vi32 |= (src[0] as i32) << 0;
149 vi32 |= (src[1] as i32) << 8;
150 vi32 |= (src[2] as i32) << 16;
151 vi32
152}
153
154#[inline]
155pub fn buf_to_u32(src: &[u8]) -> u32 {
156 LittleEndian::read_u32(src)
157}
158
159#[inline]
160pub fn buf_to_u32_big(src: &[u8]) -> u32 {
161 BigEndian::read_u32(src)
162}
163
164
165#[inline]
166pub fn buf_to_u64(src: &[u8]) -> u64 {
167 LittleEndian::read_u64(src)
168}
169
170#[inline]
171pub fn buf_to_i64(src: &[u8]) -> i64 {
172 LittleEndian::read_i64(src)
173}
174
175#[inline]
176pub fn buf_to_u16(src: &[u8]) -> u16 {
177 LittleEndian::read_u16(src)
178}
179
180#[inline]
181pub fn buf_to_u16_big(src: &[u8]) -> u16 {
182 BigEndian::read_u16(src)
183}
184
185#[inline]
186pub fn buf_to_i16(src: &[u8]) -> i16 {
187 LittleEndian::read_i16(src)
188}
189
190#[inline]
191pub fn min<T: PartialOrd + Copy>(lhs: T, rhs: T) -> T {
192 if lhs > rhs { rhs } else { lhs }
193}