Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove generic type from write_char_string method #2

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/target
/Cargo.lock
118 changes: 118 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ macros = []
binbuf-derive = { path = "crates/binbuf-derive", version = "0.0.1" }
binbuf-macros = { path = "crates/binbuf-macros", version = "0.0.1" }

num = "0.4.1"
snafu = "0.7.5"
2 changes: 1 addition & 1 deletion crates/binbuf-derive/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn expand_enum(
let enum_attrs = RawContainerAttrs::parse::<EnumReadAttrs>(enum_attrs)?;
let error: ExprPath = enum_attrs.error.parse()?;
let repr: ExprPath = enum_attrs.repr.parse()?;
println!("{enum_attrs:?}");
// println!("{enum_attrs:?}");

let read_inner = quote! {
Self::try_from(#repr::read::<E>(buf)?)
Expand Down
13 changes: 5 additions & 8 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::Endianness;

pub type Result<T = usize, E = WriteError> = std::result::Result<T, E>;

#[derive(Debug, Snafu)]
#[derive(Debug, PartialEq, Snafu)]
pub enum WriteError {
#[snafu(display(
"the length of the character string oveflows the max value encodable using an u8"
Expand Down Expand Up @@ -194,21 +194,18 @@ impl Writer {
/// assert_eq!(b.len(), 5);
/// assert_eq!(b.bytes(), &[4, 88, 65, 77, 80]);
/// ```
// FIXME (@Techassi): Remove the generic max_len because it is broken
pub fn write_char_string<L>(&mut self, s: impl AsRef<[u8]>, max_len: Option<L>) -> Result
where
L: num::Unsigned + num::Bounded + Into<usize> + Write,
{
pub fn write_char_string(&mut self, s: impl AsRef<[u8]>, max_len: Option<u8>) -> Result {
let s = s.as_ref();
let len = s.len();

// Ensure that the length label of the string doesn't exceed the maximum
// value which can be encoded using a u8.
ensure!(len <= L::max_value().into(), LengthLabelOverflowSnafu);
ensure!(len <= u8::MAX.into(), LengthLabelOverflowSnafu);
let len = len as u8;

// Ensure length is smaller than max_len
if let Some(max_len) = max_len {
ensure!(len <= max_len.into(), MaxLengthOverflowSnafu);
ensure!(len <= max_len, MaxLengthOverflowSnafu);
}

let n = len.write_be(self)?;
Expand Down
155 changes: 73 additions & 82 deletions tests/write/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::net::{Ipv4Addr, Ipv6Addr};

use binbuf::{write::Writer, BigEndian, Write};
use binbuf::{
write::{WriteError, Writer},
BigEndian, Write,
};

mod write_buffer;
mod write_derive;
Expand All @@ -10,103 +13,91 @@ mod write_multi;
mod write_span;

#[test]
fn test_write_u8() {
let mut b = Writer::new();

match 69u8.write::<BigEndian>(&mut b) {
Ok(n) => {
assert_eq!(n, 1);
assert_eq!(b.bytes(), &[69]);
}
Err(err) => panic!("{}", err),
}
fn write_u8() {
let mut writer = Writer::new();

let n = 69u8.write::<BigEndian>(&mut writer).unwrap();

assert_eq!(n, 1);
assert_eq!(writer.bytes(), &[69]);
}

#[test]
fn test_write_u16() {
let mut b = Writer::new();

match 17752u16.write::<BigEndian>(&mut b) {
Ok(n) => {
assert_eq!(n, 2);
assert_eq!(b.bytes(), &[69, 88]);
}
Err(err) => panic!("{}", err),
}
fn write_u16() {
let mut writer = Writer::new();

let n = 17752u16.write::<BigEndian>(&mut writer).unwrap();

assert_eq!(n, 2);
assert_eq!(writer.bytes(), &[69, 88]);
}

#[test]
fn test_write_u32() {
let mut b = Writer::new();

match 1163411789u32.write::<BigEndian>(&mut b) {
Ok(n) => {
assert_eq!(n, 4);
assert_eq!(b.bytes(), &[69, 88, 65, 77]);
}
Err(err) => panic!("{}", err),
}
fn write_u32() {
let mut writer = Writer::new();

let n = 1163411789u32.write::<BigEndian>(&mut writer).unwrap();

assert_eq!(n, 4);
assert_eq!(writer.bytes(), &[69, 88, 65, 77]);
}

#[test]
fn test_write_u64() {
let mut b = Writer::new();

match 4996815586883028257u64.write::<BigEndian>(&mut b) {
Ok(n) => {
assert_eq!(n, 8);
assert_eq!(b.bytes(), &[69, 88, 65, 77, 80, 76, 69, 33]);
}
Err(err) => panic!("{}", err),
}
fn write_u64() {
let mut writer = Writer::new();

let n = 4996815586883028257u64
.write::<BigEndian>(&mut writer)
.unwrap();

assert_eq!(n, 8);
assert_eq!(writer.bytes(), &[69, 88, 65, 77, 80, 76, 69, 33]);
}

#[test]
fn test_write_char_string() {
let mut b = Writer::new();

match b.write_char_string::<u8>(&[69, 88, 65, 77, 80, 76, 69, 33], None) {
Ok(n) => {
assert_eq!(n, 9);
assert_eq!(b.bytes(), &[8, 69, 88, 65, 77, 80, 76, 69, 33]);
b.clear()
}
Err(err) => panic!("{}", err),
}

match b.write_char_string::<u8>(String::from("EXAMPLE!").as_bytes(), None) {
Ok(n) => {
assert_eq!(n, 9);
assert_eq!(b.bytes(), &[8, 69, 88, 65, 77, 80, 76, 69, 33])
}
Err(err) => panic!("{}", err),
}
fn write_char_string() {
let mut writer = Writer::new();

let n = writer
.write_char_string(&[69, 88, 65, 77, 80, 76, 69, 33], None)
.unwrap();

assert_eq!(writer.bytes(), &[8, 69, 88, 65, 77, 80, 76, 69, 33]);
assert_eq!(n, 9);
}

#[test]
fn test_write_ipv4addr() {
let mut b = Writer::new();
let i = Ipv4Addr::new(127, 0, 0, 1);

match i.write::<BigEndian>(&mut b) {
Ok(n) => {
assert_eq!(n, 4);
assert_eq!(b.bytes(), &[127, 0, 0, 1])
}
Err(err) => panic!("{}", err),
};
fn write_char_string_max_len() {
let mut writer = Writer::new();

let err = writer
.write_char_string(&[69, 88, 65, 77, 80, 76, 69, 33], Some(3))
.unwrap_err();

assert_eq!(err, WriteError::MaxLengthOverflow);
}

#[test]
fn test_write_ipv6addr() {
let mut b = Writer::new();
let i = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);

match i.write::<BigEndian>(&mut b) {
Ok(n) => {
assert_eq!(n, 16);
assert_eq!(b.bytes(), &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
}
Err(err) => panic!("{}", err),
};
fn write_ipv4addr() {
let mut writer = Writer::new();
let addr = Ipv4Addr::new(127, 0, 0, 1);

let n = addr.write::<BigEndian>(&mut writer).unwrap();

assert_eq!(n, 4);
assert_eq!(writer.bytes(), &[127, 0, 0, 1])
}

#[test]
fn write_ipv6addr() {
let mut writer = Writer::new();
let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);

let n = addr.write::<BigEndian>(&mut writer).unwrap();

assert_eq!(n, 16);
assert_eq!(
writer.bytes(),
&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
)
}