chore: fix lint warnings (#330)

This commit is contained in:
Santiago Carmuega 2023-11-13 17:00:00 -03:00 committed by GitHub
parent 4d6a1fe344
commit fdbdcc3a4f
8 changed files with 139 additions and 109 deletions

View file

@ -27,11 +27,13 @@ impl<'b> Decoder<'b> {
/// Decode an integer of any size.
/// This is byte alignment agnostic.
/// First we decode the next 8 bits of the buffer.
/// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
/// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and so on.
/// If the most significant bit was instead 0 we stop decoding any more bits.
/// Finally we use zigzag to convert the unsigned integer back to a signed integer.
/// We take the 7 least significant bits as the 7 least significant bits of
/// the current unsigned integer. If the most significant bit of the 8
/// bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and
/// so on. If the most significant bit was instead 0 we stop decoding
/// any more bits. Finally we use zigzag to convert the unsigned integer
/// back to a signed integer.
pub fn integer(&mut self) -> Result<isize, Error> {
Ok(zigzag::to_isize(self.word()?))
}
@ -39,11 +41,13 @@ impl<'b> Decoder<'b> {
/// Decode an integer of 128 bits size.
/// This is byte alignment agnostic.
/// First we decode the next 8 bits of the buffer.
/// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
/// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and so on.
/// If the most significant bit was instead 0 we stop decoding any more bits.
/// Finally we use zigzag to convert the unsigned integer back to a signed integer.
/// We take the 7 least significant bits as the 7 least significant bits of
/// the current unsigned integer. If the most significant bit of the 8
/// bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and
/// so on. If the most significant bit was instead 0 we stop decoding
/// any more bits. Finally we use zigzag to convert the unsigned integer
/// back to a signed integer.
pub fn big_integer(&mut self) -> Result<i128, Error> {
Ok(zigzag::to_i128(self.big_word()?))
}
@ -70,9 +74,10 @@ impl<'b> Decoder<'b> {
/// Decodes a filler to byte align the buffer,
/// then decodes the next byte to get the array length up to a max of 255.
/// We decode bytes equal to the array length to form the byte array.
/// If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array.
/// We stop once we hit a byte array length of 0.
/// If array length is 0 for first byte array length the we return a empty array.
/// If the following byte for array length is not 0 we decode it and repeat
/// above to continue decoding the byte array. We stop once we hit a
/// byte array length of 0. If array length is 0 for first byte array
/// length the we return a empty array.
pub fn bytes(&mut self) -> Result<Vec<u8>, Error> {
self.filler()?;
self.byte_array()
@ -81,10 +86,12 @@ impl<'b> Decoder<'b> {
/// Decode a 32 bit char.
/// This is byte alignment agnostic.
/// First we decode the next 8 bits of the buffer.
/// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
/// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and so on.
/// If the most significant bit was instead 0 we stop decoding any more bits.
/// We take the 7 least significant bits as the 7 least significant bits of
/// the current unsigned integer. If the most significant bit of the 8
/// bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and
/// so on. If the most significant bit was instead 0 we stop decoding
/// any more bits.
pub fn char(&mut self) -> Result<char, Error> {
let character = self.word()? as u32;
@ -105,9 +112,10 @@ impl<'b> Decoder<'b> {
/// Decodes a filler to byte align the buffer,
/// then decodes the next byte to get the array length up to a max of 255.
/// We decode bytes equal to the array length to form the byte array.
/// If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array.
/// We stop once we hit a byte array length of 0.
/// If array length is 0 for first byte array length the we return a empty array.
/// If the following byte for array length is not 0 we decode it and repeat
/// above to continue decoding the byte array. We stop once we hit a
/// byte array length of 0. If array length is 0 for first byte array
/// length the we return a empty array.
pub fn utf8(&mut self) -> Result<String, Error> {
// TODO: Better Error Handling
String::from_utf8(Vec::<u8>::decode(self)?).map_err(Error::from)
@ -124,10 +132,12 @@ impl<'b> Decoder<'b> {
/// Decode a word of any size.
/// This is byte alignment agnostic.
/// First we decode the next 8 bits of the buffer.
/// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
/// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and so on.
/// If the most significant bit was instead 0 we stop decoding any more bits.
/// We take the 7 least significant bits as the 7 least significant bits of
/// the current unsigned integer. If the most significant bit of the 8
/// bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and
/// so on. If the most significant bit was instead 0 we stop decoding
/// any more bits.
pub fn word(&mut self) -> Result<usize, Error> {
let mut leading_bit = 1;
let mut final_word: usize = 0;
@ -146,10 +156,12 @@ impl<'b> Decoder<'b> {
/// Decode a word of 128 bits size.
/// This is byte alignment agnostic.
/// First we decode the next 8 bits of the buffer.
/// We take the 7 least significant bits as the 7 least significant bits of the current unsigned integer.
/// If the most significant bit of the 8 bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and so on.
/// If the most significant bit was instead 0 we stop decoding any more bits.
/// We take the 7 least significant bits as the 7 least significant bits of
/// the current unsigned integer. If the most significant bit of the 8
/// bits is 1 then we take the next 8 and repeat the process above,
/// filling in the next 7 least significant bits of the unsigned integer and
/// so on. If the most significant bit was instead 0 we stop decoding
/// any more bits.
pub fn big_word(&mut self) -> Result<u128, Error> {
let mut leading_bit = 1;
let mut final_word: u128 = 0;
@ -169,8 +181,8 @@ impl<'b> Decoder<'b> {
/// This is byte alignment agnostic.
/// Decode a bit from the buffer.
/// If 0 then stop.
/// Otherwise we decode an item in the list with the decoder function passed in.
/// Then decode the next bit in the buffer and repeat above.
/// Otherwise we decode an item in the list with the decoder function passed
/// in. Then decode the next bit in the buffer and repeat above.
/// Returns a list of items decoded with the decoder function.
pub fn decode_list_with<T, F>(&mut self, decoder_func: F) -> Result<Vec<T>, Error>
where
@ -228,9 +240,10 @@ impl<'b> Decoder<'b> {
/// Throws a BufferNotByteAligned error if the buffer is not byte aligned
/// Decodes the next byte to get the array length up to a max of 255.
/// We decode bytes equal to the array length to form the byte array.
/// If the following byte for array length is not 0 we decode it and repeat above to continue decoding the byte array.
/// We stop once we hit a byte array length of 0.
/// If array length is 0 for first byte array length the we return a empty array.
/// If the following byte for array length is not 0 we decode it and repeat
/// above to continue decoding the byte array. We stop once we hit a
/// byte array length of 0. If array length is 0 for first byte array
/// length the we return a empty array.
fn byte_array(&mut self) -> Result<Vec<u8>, Error> {
if self.used_bits != 0 {
return Err(Error::BufferNotByteAligned);
@ -265,10 +278,11 @@ impl<'b> Decoder<'b> {
/// This is byte alignment agnostic.
/// If num_bits is greater than the 8 we throw an IncorrectNumBits error.
/// First we decode the next num_bits of bits in the buffer.
/// If there are less unused bits in the current byte in the buffer than num_bits,
/// then we decode the remaining bits from the most significant bits in the next byte in the buffer.
/// Otherwise we decode the unused bits from the current byte.
/// Returns the decoded value up to a byte in size.
/// If there are less unused bits in the current byte in the buffer than
/// num_bits, then we decode the remaining bits from the most
/// significant bits in the next byte in the buffer. Otherwise we decode
/// the unused bits from the current byte. Returns the decoded value up
/// to a byte in size.
pub fn bits8(&mut self, num_bits: usize) -> Result<u8, Error> {
if num_bits > 8 {
return Err(Error::IncorrectNumBits);
@ -292,7 +306,8 @@ impl<'b> Decoder<'b> {
}
/// Ensures the buffer has the required bytes passed in by required_bytes.
/// Throws a NotEnoughBytes error if there are less bytes remaining in the buffer than required_bytes.
/// Throws a NotEnoughBytes error if there are less bytes remaining in the
/// buffer than required_bytes.
fn ensure_bytes(&mut self, required_bytes: usize) -> Result<(), Error> {
if required_bytes as isize > self.buffer.len() as isize - self.pos as isize {
Err(Error::NotEnoughBytes(required_bytes))
@ -302,7 +317,8 @@ impl<'b> Decoder<'b> {
}
/// Ensures the buffer has the required bits passed in by required_bits.
/// Throws a NotEnoughBits error if there are less bits remaining in the buffer than required_bits.
/// Throws a NotEnoughBits error if there are less bits remaining in the
/// buffer than required_bits.
fn ensure_bits(&mut self, required_bits: usize) -> Result<(), Error> {
if required_bits as isize
> (self.buffer.len() as isize - self.pos as isize) * 8 - self.used_bits as isize

View file

@ -34,7 +34,8 @@ impl Encoder {
}
/// Encode 1 unsigned byte.
/// Uses the next 8 bits in the buffer, can be byte aligned or byte unaligned
/// Uses the next 8 bits in the buffer, can be byte aligned or byte
/// unaligned
pub fn u8(&mut self, x: u8) -> Result<&mut Self, Error> {
if self.used_bits == 0 {
self.current_byte = x;
@ -60,10 +61,11 @@ impl Encoder {
}
/// Encode a byte array.
/// Uses filler to byte align the buffer, then writes byte array length up to 255.
/// Following that it writes the next 255 bytes from the array.
/// We repeat writing length up to 255 and the next 255 bytes until we reach the end of the byte array.
/// After reaching the end of the byte array we write a 0 byte. Only write 0 byte if the byte array is empty.
/// Uses filler to byte align the buffer, then writes byte array length up
/// to 255. Following that it writes the next 255 bytes from the array.
/// We repeat writing length up to 255 and the next 255 bytes until we reach
/// the end of the byte array. After reaching the end of the byte array
/// we write a 0 byte. Only write 0 byte if the byte array is empty.
pub fn bytes(&mut self, x: &[u8]) -> Result<&mut Self, Error> {
// use filler to write current buffer so bits used gets reset
self.filler();
@ -71,11 +73,12 @@ impl Encoder {
self.byte_array(x)
}
/// Encode a byte array in a byte aligned buffer. Throws exception if any bits for the current byte were used.
/// Writes byte array length up to 255
/// Following that it writes the next 255 bytes from the array.
/// We repeat writing length up to 255 and the next 255 bytes until we reach the end of the byte array.
/// After reaching the end of the buffer we write a 0 byte. Only write 0 if the byte array is empty.
/// Encode a byte array in a byte aligned buffer. Throws exception if any
/// bits for the current byte were used. Writes byte array length up to
/// 255 Following that it writes the next 255 bytes from the array.
/// We repeat writing length up to 255 and the next 255 bytes until we reach
/// the end of the byte array. After reaching the end of the buffer we
/// write a 0 byte. Only write 0 if the byte array is empty.
pub fn byte_array(&mut self, arr: &[u8]) -> Result<&mut Self, Error> {
if self.used_bits != 0 {
return Err(Error::BufferNotByteAligned);
@ -88,9 +91,11 @@ impl Encoder {
/// Encode an integer of any size.
/// This is byte alignment agnostic.
/// First we use zigzag once to double the number and encode the negative sign as the least significant bit.
/// Next we encode the 7 least significant bits of the unsigned integer. If the number is greater than
/// 127 we encode a leading 1 followed by repeating the encoding above for the next 7 bits and so on.
/// First we use zigzag once to double the number and encode the negative
/// sign as the least significant bit. Next we encode the 7 least
/// significant bits of the unsigned integer. If the number is greater than
/// 127 we encode a leading 1 followed by repeating the encoding above for
/// the next 7 bits and so on.
pub fn integer(&mut self, i: isize) -> &mut Self {
let i = zigzag::to_usize(i);
@ -101,9 +106,11 @@ impl Encoder {
/// Encode an integer of 128 bits size.
/// This is byte alignment agnostic.
/// First we use zigzag once to double the number and encode the negative sign as the least significant bit.
/// Next we encode the 7 least significant bits of the unsigned integer. If the number is greater than
/// 127 we encode a leading 1 followed by repeating the encoding above for the next 7 bits and so on.
/// First we use zigzag once to double the number and encode the negative
/// sign as the least significant bit. Next we encode the 7 least
/// significant bits of the unsigned integer. If the number is greater than
/// 127 we encode a leading 1 followed by repeating the encoding above for
/// the next 7 bits and so on.
pub fn big_integer(&mut self, i: i128) -> &mut Self {
let i = zigzag::to_u128(i);
@ -114,8 +121,9 @@ impl Encoder {
/// Encode a char of 32 bits.
/// This is byte alignment agnostic.
/// We encode the 7 least significant bits of the unsigned byte. If the char value is greater than
/// 127 we encode a leading 1 followed by repeating the above for the next 7 bits and so on.
/// We encode the 7 least significant bits of the unsigned byte. If the char
/// value is greater than 127 we encode a leading 1 followed by
/// repeating the above for the next 7 bits and so on.
pub fn char(&mut self, c: char) -> &mut Self {
self.word(c as usize);
@ -136,17 +144,19 @@ impl Encoder {
/// Encode a string.
/// Convert to byte array and then use byte array encoding.
/// Uses filler to byte align the buffer, then writes byte array length up to 255.
/// Following that it writes the next 255 bytes from the array.
/// After reaching the end of the buffer we write a 0 byte. Only write 0 byte if the byte array is empty.
/// Uses filler to byte align the buffer, then writes byte array length up
/// to 255. Following that it writes the next 255 bytes from the array.
/// After reaching the end of the buffer we write a 0 byte. Only write 0
/// byte if the byte array is empty.
pub fn utf8(&mut self, s: &str) -> Result<&mut Self, Error> {
self.bytes(s.as_bytes())
}
/// Encode a unsigned integer of any size.
/// This is byte alignment agnostic.
/// We encode the 7 least significant bits of the unsigned byte. If the char value is greater than
/// 127 we encode a leading 1 followed by repeating the above for the next 7 bits and so on.
/// We encode the 7 least significant bits of the unsigned byte. If the char
/// value is greater than 127 we encode a leading 1 followed by
/// repeating the above for the next 7 bits and so on.
pub fn word(&mut self, c: usize) -> &mut Self {
let mut d = c;
loop {
@ -168,8 +178,9 @@ impl Encoder {
/// Encode a unsigned integer of 128 bits size.
/// This is byte alignment agnostic.
/// We encode the 7 least significant bits of the unsigned byte. If the char value is greater than
/// 127 we encode a leading 1 followed by repeating the above for the next 7 bits and so on.
/// We encode the 7 least significant bits of the unsigned byte. If the char
/// value is greater than 127 we encode a leading 1 followed by
/// repeating the above for the next 7 bits and so on.
pub fn big_word(&mut self, c: u128) -> &mut Self {
let mut d = c;
loop {
@ -191,8 +202,9 @@ impl Encoder {
/// Encode a list of bytes with a function
/// This is byte alignment agnostic.
/// If there are bytes in a list then write 1 bit followed by the functions encoding.
/// After the last item write a 0 bit. If the list is empty only encode a 0 bit.
/// If there are bytes in a list then write 1 bit followed by the functions
/// encoding. After the last item write a 0 bit. If the list is empty
/// only encode a 0 bit.
pub fn encode_list_with<T>(
&mut self,
list: &[T],
@ -209,10 +221,11 @@ impl Encoder {
}
/// Encodes up to 8 bits of information and is byte alignment agnostic.
/// Uses unused bits in the current byte to write out the passed in byte value.
/// Overflows to the most significant digits of the next byte if number of bits to use is greater than unused bits.
/// Expects that number of bits to use is greater than or equal to required bits by the value.
/// The param num_bits is i64 to match unused_bits type.
/// Uses unused bits in the current byte to write out the passed in byte
/// value. Overflows to the most significant digits of the next byte if
/// number of bits to use is greater than unused bits. Expects that
/// number of bits to use is greater than or equal to required bits by the
/// value. The param num_bits is i64 to match unused_bits type.
pub fn bits(&mut self, num_bits: i64, val: u8) -> &mut Self {
match (num_bits, val) {
(1, 0) => self.zero(),
@ -237,13 +250,13 @@ impl Encoder {
self.used_bits += num_bits;
let unused_bits = 8 - self.used_bits;
match unused_bits {
x if x > 0 => {
self.current_byte |= val << x;
}
x if x == 0 => {
0 => {
self.current_byte |= val;
self.next_word();
}
x if x > 0 => {
self.current_byte |= val << x;
}
x => {
let used = -x;
self.current_byte |= val >> used;
@ -289,8 +302,9 @@ impl Encoder {
}
}
/// Write out byte regardless of current buffer alignment.
/// Write most significant bits in remaining unused bits for the current byte,
/// then write out the remaining bits at the beginning of the next byte.
/// Write most significant bits in remaining unused bits for the current
/// byte, then write out the remaining bits at the beginning of the next
/// byte.
fn byte_unaligned(&mut self, x: u8) {
let x_shift = self.current_byte | (x >> self.used_bits);
self.buffer.push(x_shift);
@ -298,8 +312,9 @@ impl Encoder {
self.current_byte = x << (8 - self.used_bits);
}
/// Write the current byte out to the buffer and begin next byte to write out.
/// Add current byte to the buffer and set current byte and used bits to 0.
/// Write the current byte out to the buffer and begin next byte to write
/// out. Add current byte to the buffer and set current byte and used
/// bits to 0.
fn next_word(&mut self) {
self.buffer.push(self.current_byte);
@ -309,8 +324,8 @@ impl Encoder {
/// Writes byte array length up to 255
/// Following that it writes the next 255 bytes from the array.
/// After reaching the end of the buffer we write a 0 byte. Only write 0 if the byte array is empty.
/// This is byte alignment agnostic.
/// After reaching the end of the buffer we write a 0 byte. Only write 0 if
/// the byte array is empty. This is byte alignment agnostic.
fn write_blk(&mut self, arr: &[u8]) {
let chunks = arr.chunks(255);