webparse/
macros.rs

1// Copyright 2022 - 2023 Wenmeng See the COPYRIGHT
2// file at the top-level directory of this distribution.
3// 
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8// 
9// Author: tickbh
10// -----
11// Created Date: 2023/08/15 09:59:05
12
13#[macro_export]
14macro_rules! byte_map {
15    ($($flag:expr,)*) => ([
16        $($flag != 0,)*
17    ])
18}
19
20#[macro_export]
21macro_rules! next {
22    ($bytes:ident) => ({
23        match $bytes.get_next() {
24            Some(b) => Ok(b),
25            None => Err(crate::WebError::from(crate::HttpError::Partial))
26        }
27    })
28}
29
30#[macro_export]
31macro_rules! must_have {
32    ($bytes:ident, $num:expr) => ({
33        if $bytes.remaining() >= $num {
34            Ok(())
35        } else {
36            Err(webparse::WebError::from(webparse::HttpError::Partial))
37        }
38    })
39}
40
41
42#[macro_export]
43macro_rules! peek {
44    ($bytes:ident) => ({
45        match $bytes.peek() {
46            Some(b) => Ok(b),
47            None => Err(WebError::from(crate::HttpError::Partial))
48        }
49    })
50}
51
52
53#[macro_export]
54macro_rules! expect {
55    ($bytes:ident.next() == $pat:pat => $ret:expr) => {
56        expect!(next!($bytes) => $pat |? $ret)
57    };
58    ($e:expr => $pat:pat_param |? $ret:expr) => {
59        match $e {
60            Ok(_v@$pat) => (),
61            Err(e) => return Err(e),
62            _ => return $ret
63        }
64    };
65}
66
67
68#[macro_export]
69macro_rules! try_advance {
70    ($flag:expr) => {
71        if !$flag {
72            return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "not enough"));
73        }
74    };
75}