[Crate] Add formatting (#22)

* chore: add standard for formatting

* chore: manually tidy up imports and format

* chore: remove vscode and add to gitignore

* chore: alphabetically order and fix

* chore: format

* chore: fix format issue with payload

* chore: format as merge had unformatted code

* [format] Fix comments, change max width and binop operator

---------

Co-authored-by: CosminPerRam <cosmin.p@live.com>
This commit is contained in:
Cain 2023-03-14 09:31:37 +01:00 committed by GitHub
parent e023e13236
commit 1b13d39856
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
71 changed files with 3165 additions and 2593 deletions

View file

@ -1,15 +1,19 @@
use crate::GDResult;
use crate::GDError::{PacketBad, PacketUnderflow};
use byteorder::{ByteOrder, LittleEndian, BigEndian};
use crate::{
GDError::{PacketBad, PacketUnderflow},
GDResult,
};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
pub enum Endianess {
Little, Big
Little,
Big,
}
pub struct Bufferer {
data: Vec<u8>,
endianess: Endianess,
position: usize
position: usize,
}
impl Bufferer {
@ -17,13 +21,11 @@ impl Bufferer {
Bufferer {
data: data.to_vec(),
endianess,
position: 0
position: 0,
}
}
fn check_size(&self, by: usize) -> bool {
by > self.remaining_length()
}
fn check_size(&self, by: usize) -> bool { by > self.remaining_length() }
pub fn get_u8(&mut self) -> GDResult<u8> {
if self.check_size(1) {
@ -70,7 +72,7 @@ impl Bufferer {
let value = match self.endianess {
Endianess::Little => LittleEndian::read_f32(self.remaining_data()),
Endianess::Big => BigEndian::read_f32(self.remaining_data())
Endianess::Big => BigEndian::read_f32(self.remaining_data()),
};
self.move_position_ahead(4);
@ -84,7 +86,7 @@ impl Bufferer {
let value = match self.endianess {
Endianess::Little => LittleEndian::read_u64(self.remaining_data()),
Endianess::Big => BigEndian::read_u64(self.remaining_data())
Endianess::Big => BigEndian::read_u64(self.remaining_data()),
};
self.move_position_ahead(8);
@ -97,10 +99,10 @@ impl Bufferer {
return Err(PacketUnderflow);
}
let first_null_position = sub_buf.iter().position(|&x| x == 0)
.ok_or(PacketBad)?;
let value = std::str::from_utf8(&sub_buf[..first_null_position])
.map_err(|_| PacketBad)?.to_string();
let first_null_position = sub_buf.iter().position(|&x| x == 0).ok_or(PacketBad)?;
let value = std::str::from_utf8(&sub_buf[.. first_null_position])
.map_err(|_| PacketBad)?
.to_string();
self.move_position_ahead(value.len() + 1);
Ok(value)
@ -113,7 +115,8 @@ impl Bufferer {
}
let value = std::str::from_utf8(sub_buf)
.map_err(|_| PacketBad)?.to_string();
.map_err(|_| PacketBad)?
.to_string();
self.move_position_ahead(value.len());
Ok(value)
@ -127,9 +130,11 @@ impl Bufferer {
let paired_buf: Vec<u16> = sub_buf
.chunks_exact(2)
.map(|pair| match self.endianess {
Endianess::Little => LittleEndian::read_u16(pair),
Endianess::Big => BigEndian::read_u16(pair),
.map(|pair| {
match self.endianess {
Endianess::Little => LittleEndian::read_u16(pair),
Endianess::Big => BigEndian::read_u16(pair),
}
})
.collect();
@ -139,29 +144,17 @@ impl Bufferer {
Ok(value)
}
pub fn move_position_ahead(&mut self, by: usize) {
self.position += by;
}
pub fn move_position_ahead(&mut self, by: usize) { self.position += by; }
pub fn move_position_backward(&mut self, by: usize) {
self.position -= by;
}
pub fn move_position_backward(&mut self, by: usize) { self.position -= by; }
pub fn data_length(&self) -> usize {
self.data.len()
}
pub fn data_length(&self) -> usize { self.data.len() }
pub fn remaining_data(&self) -> &[u8] {
&self.data[self.position..]
}
pub fn remaining_data(&self) -> &[u8] { &self.data[self.position ..] }
pub fn remaining_data_vec(&self) -> Vec<u8> {
self.remaining_data().to_vec()
}
pub fn remaining_data_vec(&self) -> Vec<u8> { self.remaining_data().to_vec() }
pub fn remaining_length(&self) -> usize {
self.data_length() - self.position
}
pub fn remaining_length(&self) -> usize { self.data_length() - self.position }
pub fn as_endianess(&self, endianess: Endianess) -> Self {
Bufferer {
@ -277,7 +270,10 @@ mod tests {
#[test]
fn get_string_utf16_le() {
let mut buffer = Bufferer::new_with_data(Endianess::Little, &[0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00]);
let mut buffer = Bufferer::new_with_data(
Endianess::Little,
&[0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00],
);
assert_eq!(buffer.get_string_utf16().unwrap(), "Hello");
assert_eq!(buffer.remaining_length(), 0);
@ -286,7 +282,10 @@ mod tests {
#[test]
fn get_string_utf16_be() {
let mut buffer = Bufferer::new_with_data(Endianess::Big, &[0x00, 0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f]);
let mut buffer = Bufferer::new_with_data(
Endianess::Big,
&[0x00, 0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f],
);
assert_eq!(buffer.get_string_utf16().unwrap(), "Hello");
assert_eq!(buffer.remaining_length(), 0);