Began implementing fully isomorphic macros
Like Rust's Proc macros. Now we have preprocessor recursion to worry about. I also made a cool macro for enums
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use itertools::{Chunk, Itertools};
|
||||
|
||||
use crate::Encode;
|
||||
|
||||
pub fn encode_enum<W: Write + ?Sized>(write: &mut W, id: u8, f: impl FnOnce(&mut W)) {
|
||||
@@ -11,10 +13,20 @@ pub fn write_exact<W: Write + ?Sized>(write: &mut W, bytes: &'static [u8]) {
|
||||
write.write_all(bytes).expect("Failed to write exact bytes")
|
||||
}
|
||||
|
||||
pub fn print_bytes(b: &[u8]) -> String {
|
||||
(b.iter().map(|b| format!("{b:02x}")))
|
||||
.chunks(4)
|
||||
.into_iter()
|
||||
.map(|mut c: Chunk<_>| c.join(" "))
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
pub fn read_exact<R: Read + ?Sized>(read: &mut R, bytes: &'static [u8]) {
|
||||
let mut data = vec![0u8; bytes.len()];
|
||||
read.read_exact(&mut data).expect("Failed to read bytes");
|
||||
assert_eq!(&data, bytes, "Wrong bytes")
|
||||
if data != bytes {
|
||||
panic!("Wrong bytes!\nExpected: {}\nFound: {}", print_bytes(bytes), print_bytes(&data));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enc_vec(enc: &impl Encode) -> Vec<u8> {
|
||||
|
||||
Reference in New Issue
Block a user