]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: add 'bits' combinator to simplify nom bits parsers
authorPierre Chifflier <chifflier@wzdftpd.net>
Tue, 18 Jan 2022 20:56:37 +0000 (21:56 +0100)
committerPierre Chifflier <chifflier@wzdftpd.net>
Wed, 19 Jan 2022 08:13:11 +0000 (09:13 +0100)
Add a specialized version of the 'bits' nom combinator so adding
bits-level parsers does not require type annotations.

rust/src/common.rs

index 71eaad728fa8c5da76f78bb350a4dcd1a31da62a..bbb4a73bfcecd69a24061ea666c6b416dbae39df 100644 (file)
@@ -3,7 +3,8 @@ use std::os::raw::c_char;
 
 pub mod nom7 {
     use nom7::bytes::streaming::{tag, take_until};
-    use nom7::error::ParseError;
+    use nom7::error::{Error, ParseError};
+    use nom7::ErrorConvert;
     use nom7::IResult;
 
     /// Reimplementation of `take_until_and_consume` for nom 7
@@ -20,6 +21,24 @@ pub mod nom7 {
             Ok((i, res))
         }
     }
+
+    /// Specialized version of the nom 7 `bits` combinator
+    ///
+    /// The `bits combinator has trouble inferring the transient error type
+    /// used by the tuple parser, because the function is generic and any
+    /// error type would be valid.
+    /// Use an explicit error type (as described in
+    /// https://docs.rs/nom/7.1.0/nom/bits/fn.bits.html) to solve this problem, and
+    /// specialize this function for `&[u8]`.
+    pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
+    where
+        E: ParseError<&'a [u8]>,
+        Error<(&'a [u8], usize)>: ErrorConvert<E>,
+        P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
+    {
+        // use full path to disambiguate nom `bits` from this current function name
+        nom7::bits::bits(parser)
+    }
 }
 
 #[macro_export]