aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2020-01-14 12:35:46 +0100
committerMax Audron <audron@cocaine.farm>2020-01-17 10:48:27 +0100
commit5d50a5f0c03baf460fee394decce5898812dbd2c (patch)
treefc53a8ea19786be4dc57f9736cd4bf4e76026227 /src/util.rs
parentinitial implementation done (diff)
refactor parse impl
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
index 809287f..48ab55e 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -13,3 +13,30 @@ macro_rules! parse_match {
};
};
}
+
+#[macro_export]
+macro_rules! match_variant {
+ ( $values:expr, $x:path, $field:expr ) => {
+ match &$values[$field] {
+ $x(x) => { Ok(x.clone()) },
+ _ => { Err("") }
+ }.unwrap();
+ }
+}
+
+pub fn prepend_byte_len(buf: &mut Vec<u8>) {
+ use std::convert::TryInto;
+ let len: i32 = buf.len().try_into().unwrap();
+ let ulen: &[u8] = &len.to_be_bytes();
+ buf.insert(0, ulen[3]);
+ buf.insert(0, ulen[2]);
+ buf.insert(0, ulen[1]);
+ buf.insert(0, ulen[0]);
+}
+
+pub fn insert_bytes(pos: usize, buf: &mut Vec<u8>, input: &mut [u8]) {
+ input.reverse();
+ for i in input {
+ buf.insert(pos, *i)
+ }
+}