diff --git a/hptp-recv/src/main.rs b/hptp-recv/src/main.rs index 9b9cc03..826047c 100644 --- a/hptp-recv/src/main.rs +++ b/hptp-recv/src/main.rs @@ -171,8 +171,7 @@ where { let idxs = sink.ack_idxs(); - log.debug_msg(format!("send acks {:?}", idxs)) - .await; + log.debug_msg(format!("send acks {:?}", idxs)).await; to_send.push(DownMsg::Ack { idxs: idxs.clone() }); to_send.push(DownMsg::Ack { idxs }); } @@ -186,7 +185,7 @@ where } if sink.is_file_complete() { - break + break; } } diff --git a/hptp-send/src/main.rs b/hptp-send/src/main.rs index 886a544..e358b32 100644 --- a/hptp-send/src/main.rs +++ b/hptp-send/src/main.rs @@ -139,8 +139,12 @@ where let read_size = cmp::min(MAX_SEG_SIZE, self.inp_buffer.len()); let out_vec = self.inp_buffer.drain(0..read_size).collect(); let is_eof = self.inp_buffer.len() == 0; - SegData{bytes: out_vec, is_last_segment: is_eof, is_meow_encoded: self.is_meow, - is_cut: is_eof && self.is_cut} + SegData { + bytes: out_vec, + is_last_segment: is_eof, + is_meow_encoded: self.is_meow, + is_cut: is_eof && self.is_cut, + } } async fn fresh_seg_idx(&mut self) -> Option { diff --git a/hptp/src/encoding.rs b/hptp/src/encoding.rs index 0738050..487a3fd 100644 --- a/hptp/src/encoding.rs +++ b/hptp/src/encoding.rs @@ -6,7 +6,8 @@ use regex::bytes::Regex; // compression on data sent over HPTP lazy_static! { - static ref ENCODING_DETECTOR: Regex = Regex::new(r"^\n?([0-9a-fA-F]{60}\n)*[0-9a-fA-F]{0,60}$").unwrap(); + static ref ENCODING_DETECTOR: Regex = + Regex::new(r"^\n?([0-9a-fA-F]{60}\n)*[0-9a-fA-F]{0,60}$").unwrap(); static ref HEX_DETECTOR: Regex = Regex::new(r"^[0-9a-fA-F]{0,60}$").unwrap(); } @@ -24,21 +25,23 @@ impl MeowCoder { // this takes an index representing the amount of encoded data sent so far, which // determines where to end the next line of hex according to WRAP_SIZE pub fn can_be_encoded(data: &[u8], index: usize) -> bool { - if index % (WRAP_SIZE/2) == 0 { + if index % (WRAP_SIZE / 2) == 0 { ENCODING_DETECTOR.is_match(data) } else { let nl_pos = data.iter().position(|&r| r == '\n' as u8); match nl_pos { - Some(nl_idx) => (nl_idx/2 + index)%(WRAP_SIZE/2) == 0 - && ENCODING_DETECTOR.is_match(&data[nl_idx..]) - && (nl_idx == 0 || HEX_DETECTOR.is_match(&data[..nl_idx - 1])), - None => false + Some(nl_idx) => { + (nl_idx / 2 + index) % (WRAP_SIZE / 2) == 0 + && ENCODING_DETECTOR.is_match(&data[nl_idx..]) + && (nl_idx == 0 || HEX_DETECTOR.is_match(&data[..nl_idx - 1])) + } + None => false, } } } pub fn new() -> MeowCoder { - MeowCoder{line_index: 0} + MeowCoder { line_index: 0 } } pub fn hex_to_nibble(chr: u8) -> u8 { @@ -69,14 +72,14 @@ impl MeowCoder { let mut pair_first = false; for chr in input { if *chr == '\n' as u8 { - continue + continue; } if !pair_first { prev_char = *chr; pair_first = true; } else { - let byte_value: u8 = MeowCoder::hex_to_nibble(prev_char) * 16 - + MeowCoder::hex_to_nibble(*chr); + let byte_value: u8 = + MeowCoder::hex_to_nibble(prev_char) * 16 + MeowCoder::hex_to_nibble(*chr); out.push(byte_value); pair_first = false; } @@ -116,8 +119,20 @@ mod tests { fn test_match() { assert_eq!(MeowCoder::can_be_encoded(b"abcd1234", 0), true); assert_eq!(MeowCoder::can_be_encoded(b"abcXd1234", 0), false); - assert_eq!(MeowCoder::can_be_encoded(b"012345678901234567890123456789012345678901234567890123456789\nabcdef", 0), true); - assert_eq!(MeowCoder::can_be_encoded(b"01234567890123456789012345678901234567890123456789012345678\nabcdef", 0), false); + assert_eq!( + MeowCoder::can_be_encoded( + b"012345678901234567890123456789012345678901234567890123456789\nabcdef", + 0 + ), + true + ); + assert_eq!( + MeowCoder::can_be_encoded( + b"01234567890123456789012345678901234567890123456789012345678\nabcdef", + 0 + ), + false + ); assert_eq!(MeowCoder::can_be_encoded(b"\x12\xab\x45\n", 0), false); assert_eq!(MeowCoder::can_be_encoded(b"abcd1234\n012345678901234567890123456789012345678901234567890123456789\nabcdefabcd", 26), true); assert_eq!(MeowCoder::can_be_encoded(b"abcd123456\nabcd", 25), true); @@ -125,18 +140,24 @@ mod tests { #[test] fn test_encode() { - let vec = vec!['a' as u8, '4' as u8, 'c' as u8, 'd' as u8, - '\n' as u8, 'e' as u8, 'f' as u8]; + let vec = vec![ + 'a' as u8, '4' as u8, 'c' as u8, 'd' as u8, '\n' as u8, 'e' as u8, 'f' as u8, + ]; assert_eq!(MeowCoder::encode(&vec), (vec![0xa4, 0xcd, 0xef], false)); - let vec2 = vec!['a' as u8, '4' as u8, 'c' as u8, 'd' as u8, - '\n' as u8, 'e' as u8, 'f' as u8, '9' as u8]; - assert_eq!(MeowCoder::encode(&vec2), (vec![0xa4, 0xcd, 0xef, 0x90], true)); + let vec2 = vec![ + 'a' as u8, '4' as u8, 'c' as u8, 'd' as u8, '\n' as u8, 'e' as u8, 'f' as u8, '9' as u8, + ]; + assert_eq!( + MeowCoder::encode(&vec2), + (vec![0xa4, 0xcd, 0xef, 0x90], true) + ); } #[test] fn test_encode_decode() { - let hex_str: &[u8] = b"012345678901234567890123456789012345678901234567890123456789\nabcdef"; + let hex_str: &[u8] = + b"012345678901234567890123456789012345678901234567890123456789\nabcdef"; let vec: Vec = Vec::from(hex_str); let (vec2, was_cut) = MeowCoder::encode(&vec); assert_eq!(was_cut, false); @@ -147,7 +168,8 @@ mod tests { #[test] fn test_encode_decode_cut() { - let hex_str: &[u8] = b"012345678901234567890123456789012345678901234567890123456789\nabcdef3"; + let hex_str: &[u8] = + b"012345678901234567890123456789012345678901234567890123456789\nabcdef3"; let vec: Vec = Vec::from(hex_str); let (vec2, was_cut) = MeowCoder::encode(&vec); assert_eq!(was_cut, true); diff --git a/hptp/src/lib.rs b/hptp/src/lib.rs index 8debc76..849a036 100644 --- a/hptp/src/lib.rs +++ b/hptp/src/lib.rs @@ -3,8 +3,8 @@ extern crate thiserror; extern crate byteorder; extern crate chrono; +pub mod encoding; pub mod logger; pub mod msg; pub mod peer; pub mod seg; -pub mod encoding; diff --git a/hptp/src/msg.rs b/hptp/src/msg.rs index d2bf8a2..d87d319 100644 --- a/hptp/src/msg.rs +++ b/hptp/src/msg.rs @@ -4,10 +4,7 @@ use byteorder::ByteOrder; #[derive(Clone)] pub enum UpMsg { - Data { - payload: SegData, - seg_idx: SegIdx, - }, + Data { payload: SegData, seg_idx: SegIdx }, } #[derive(Clone)] @@ -64,7 +61,8 @@ impl SerDes for UpMsg { }, seg_idx, } => { - let hdr = *seg_idx | if *is_last_segment { LAST_SEG_MASK } else { 0 } + let hdr = *seg_idx + | if *is_last_segment { LAST_SEG_MASK } else { 0 } | if *is_meow_encoded { MEOW_CODED_MASK } else { 0 } | if *is_cut { CUT_MASK } else { 0 }; BO::write_u32(&mut buf[0..4], hdr);