rust-gamedig/src/utils.rs
Cain 1b13d39856
[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>
2023-03-14 10:31:37 +02:00

41 lines
1.1 KiB
Rust

use crate::{
GDError::{PacketOverflow, PacketUnderflow},
GDResult,
};
use std::cmp::Ordering;
pub fn error_by_expected_size(expected: usize, size: usize) -> GDResult<()> {
match size.cmp(&expected) {
Ordering::Greater => Err(PacketOverflow),
Ordering::Less => Err(PacketUnderflow),
Ordering::Equal => Ok(()),
}
}
pub fn address_and_port_as_string(address: &str, port: u16) -> String { format!("{}:{}", address, port) }
pub fn u8_lower_upper(n: u8) -> (u8, u8) { (n & 15, n >> 4) }
#[cfg(test)]
mod tests {
#[test]
fn address_and_port_as_string() {
assert_eq!(
super::address_and_port_as_string("192.168.0.1", 27015),
"192.168.0.1:27015"
);
}
#[test]
fn u8_lower_upper() {
assert_eq!(super::u8_lower_upper(171), (11, 10));
}
#[test]
fn error_by_expected_size() {
assert!(super::error_by_expected_size(69, 69).is_ok());
assert!(super::error_by_expected_size(69, 68).is_err());
assert!(super::error_by_expected_size(69, 70).is_err());
}
}