about summary refs log tree commit diff
path: root/users/Profpatsch/netencode/netencode.rs
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-02-07T08·16+0100
committerProfpatsch <mail@profpatsch.de>2021-02-09T01·36+0000
commit3226e6243f453d2171787e5a5bfbecda9ab469fb (patch)
tree8647437e945e463681ea3e4d87490c2e52e19376 /users/Profpatsch/netencode/netencode.rs
parent3faf5b6f0927714d3bf1f3eaacd62189be7b6db2 (diff)
feat(users/Profpatsch/netencode): add `dec::AnyU` as id r/2189
Change-Id: I3037882dff15243bd7a5c1c78331f8e2ffdbda84
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2493
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/Profpatsch/netencode/netencode.rs')
-rw-r--r--users/Profpatsch/netencode/netencode.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/users/Profpatsch/netencode/netencode.rs b/users/Profpatsch/netencode/netencode.rs
index c8c631689a..e15cd8b383 100644
--- a/users/Profpatsch/netencode/netencode.rs
+++ b/users/Profpatsch/netencode/netencode.rs
@@ -604,6 +604,24 @@ pub mod dec {
         fn dec(u: U<'a>) -> Result<Self::A, DecodeError>;
     }
 
+    pub struct AnyT;
+    pub struct AnyU;
+
+    // impl Decoder for AnyT {
+    //     type A = T;
+    //     fn dec(u: U) -> Result<Self::A, DecodeError> {
+    //         // TODO: implement
+    //         parse::u_into_t(u)
+    //     }
+    // }
+
+    impl<'a> Decoder<'a> for AnyU {
+        type A = U<'a>;
+        fn dec(u: U<'a>) -> Result<Self::A, DecodeError> {
+            Ok(u)
+        }
+    }
+
     pub struct ScalarAsBytes;
 
     impl<'a> Decoder<'a> for ScalarAsBytes {
@@ -637,4 +655,12 @@ pub mod dec {
             }
         }
     }
+
+    fn dec_u(b: &[u8]) -> Result<U, DecodeError> {
+        match parse::u_u(b) {
+            Ok((b"", u)) => Ok(u),
+            Ok((rest, _)) => Err(DecodeError(format!("Cannot decode nested U, it contains trailing bytes"))),
+            Err(err) => Err(DecodeError(format!("Cannot decode nested U bytes: {:?}", err))),
+        }
+    }
 }