Removed anyhow dependency

This commit is contained in:
Lucas Schumacher 2025-07-21 21:32:08 -04:00
parent b3bf6dcac1
commit c8a5faa1e9
3 changed files with 30 additions and 57 deletions

View File

@ -7,5 +7,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.81"
poppable-derive = { path = "poppable-derive"}

View File

@ -48,7 +48,7 @@ macro_rules! pop_derive_macro {
quote!(
impl $trait_name for #struct_ident {
fn $trait_pop_fn(mut source: &mut &[u8]) -> anyhow::Result<Self>
fn $trait_pop_fn(mut source: &mut &[u8]) -> std::io::Result<Self>
where
Self: Sized,
{
@ -57,7 +57,7 @@ macro_rules! pop_derive_macro {
#(#names),*
})
}
fn $trait_push_fn<T: Pushable>(&self, dest: &mut T) -> anyhow::Result<()> {
fn $trait_push_fn<T: Pushable>(&self, dest: &mut T) -> std::io::Result<()> {
#(#pop)*
Ok(())
}
@ -70,47 +70,3 @@ macro_rules! pop_derive_macro {
pop_derive_macro!(pop_ne_derive_macro, PopFromNE, pop_ne_from, push_ne_into);
pop_derive_macro!(pop_le_derive_macro, PopFromLE, pop_le_from, push_le_into);
pop_derive_macro!(pop_be_derive_macro, PopFromBE, pop_be_from, push_be_into);
/*
#[proc_macro_derive(PopFromNE)]
pub fn pop_ne_derive_macro(item: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(item).unwrap();
let struct_ident = ast.ident;
let struct_data: syn::DataStruct = match ast.data {
syn::Data::Struct(struct_data) => struct_data,
syn::Data::Enum(_) => panic!("Error: Enum support not implemented"),
syn::Data::Union(_) => panic!("Error: Union support not implemented"),
};
let recursive = struct_data.fields.iter().map(|f| {
let name = &f.ident;
let ty = &f.ty;
quote_spanned!(f.span()=> let #name = <#ty>::pop_ne_from(&mut source)?;)
});
let names = struct_data.fields.iter().map(|f| {
let name = &f.ident;
quote_spanned!(f.span()=> #name)
});
let pop = struct_data.fields.iter().map(|f| {
let name = &f.ident;
quote_spanned!(f.span()=> PopFromNE::push_ne_into(&self.#name, dest)?;)
});
quote!(
impl PopFromNE for #struct_ident {
fn pop_ne_from(mut source: &mut &[u8]) -> anyhow::Result<Self>
where
Self: Sized,
{
#(#recursive)*
Ok(#struct_ident{
#(#names),*
})
}
fn push_ne_into<T: Pushable>(&self, dest: &mut T) -> anyhow::Result<()> {
#(#pop)*
Ok(())
}
})
.into()
}*/

View File

@ -1,5 +1,6 @@
use anyhow::{Ok, Result};
//use anyhow::{Ok, Result};
pub use poppable_derive::PushPopParse;
use std::io::{self, Result};
//pub type BytesRef = AsRef<u8>;
@ -79,7 +80,10 @@ impl PopFromNE for u8 {
{
const SIZE: usize = std::mem::size_of::<u8>();
if source.len() < SIZE {
anyhow::bail!("Buffer too small to pop u8");
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"Buffer too small to pop u8",
));
}
let value = source[0];
@ -126,9 +130,12 @@ macro_rules! core_impl_pop_ne {
{
const SIZE: usize = std::mem::size_of::<$int_type>();
if source.len() < SIZE {
anyhow::bail!("Buffer too small to pop {}", stringify!($int_type));
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
stringify!(Buffer too small to pop $int_type),
));
}
let value = $int_type::from_ne_bytes(source[..SIZE].try_into()?);
let value = $int_type::from_ne_bytes(source[..SIZE].try_into().unwrap());
*source = &source[SIZE..];
Ok(value)
}
@ -176,9 +183,12 @@ macro_rules! flt_impl_pop_non_ne {
{
const SIZE: usize = std::mem::size_of::<$flt_type>();
if source.len() < SIZE {
anyhow::bail!("Buffer to small to pop $flt_type");
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
stringify!(Buffer too small to pop $flt_type),
));
}
let value = $flt_type::from_le_bytes(source[..SIZE].try_into()?);
let value = $flt_type::from_le_bytes(source[..SIZE].try_into().unwrap());
*source = &source[SIZE..];
Ok(value)
}
@ -194,9 +204,12 @@ macro_rules! flt_impl_pop_non_ne {
{
const SIZE: usize = std::mem::size_of::<$flt_type>();
if source.len() < SIZE {
anyhow::bail!("Buffer to small to pop $flt_type");
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
stringify!(Buffer too small to pop $flt_type),
));
}
let value = $flt_type::from_be_bytes(source[..SIZE].try_into()?);
let value = $flt_type::from_be_bytes(source[..SIZE].try_into().unwrap());
*source = &source[SIZE..];
Ok(value)
}
@ -242,9 +255,14 @@ impl<const SIZE: usize> PopFromNE for [u8; SIZE] {
Self: Sized,
{
if source.len() < SIZE {
anyhow::bail!("Buffer to small to pop [u8; {SIZE}]");
//anyhow::bail!("Buffer to small to pop [u8; {SIZE}]");
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
format!("Buffer too small to pop [u8; {SIZE}]"),
//stringify!(Buffer too small to pop $flt_type),
));
}
let value = source[..SIZE].try_into()?;
let value = source[..SIZE].try_into().unwrap();
*source = &source[SIZE..];
Ok(value)
}