Skip to content

Commit

Permalink
rfc: Change name because binary-utils is taken...
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bv committed May 1, 2023
1 parent 330e1d8 commit 90db2bb
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 92 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "binary-utils"
name = "binary-util"
version = "0.3.0"
authors = ["NetrexMC"]
edition = "2021"
include = ["src/**/*", "README.md"]
description = "A panic-free way to read/write binary streams in rust."
license = "Apache-2.0"
repository = "https://github.com/NetrexMC/binary-util"

[dependencies]
codegen = { path = "./codegen", version = "0.1.0", optional = true }
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# binary_utils
# binary_util
A panic-free way to read/write binary streams in rust.

[Documentation](https://docs.rs/binary_utils/) |
[Documentation](https://docs.rs/binary_util/) |
[Discord](https://discord.gg/y4aWA5MQxK)

## Generic Usage
```rust
use binary_utils::{BinaryIo, BinaryReader, BinaryWriter};
use binary_util::{BinaryIo, BinaryReader, BinaryWriter};

#[derive(BinaryIo)]
pub struct MyStruct {
Expand All @@ -30,4 +30,4 @@ fn main() {
}
```

For more examples and usage, please refer to the [Documentation](https://docs.rs/binary_utils/).
For more examples and usage, please refer to the [Documentation](https://docs.rs/binary_util/).
8 changes: 4 additions & 4 deletions codegen/src/io/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ pub(crate) fn derive_enum(
let read_streams = variants.iter().map(|variant| variant.read_content.clone()).collect::<Vec<TokenStream2>>();

quote! {
impl ::binary_utils::interfaces::Writer for #enum_name {
fn write(&self, _binary_writew: &mut ::binary_utils::io::ByteWriter) -> ::std::result::Result<(), ::std::io::Error> {
impl ::binary_util::interfaces::Writer for #enum_name {
fn write(&self, _binary_writew: &mut ::binary_util::io::ByteWriter) -> ::std::result::Result<(), ::std::io::Error> {
match self {
#(#write_streams)*
};
Expand All @@ -251,8 +251,8 @@ pub(crate) fn derive_enum(
}
}

impl ::binary_utils::interfaces::Reader<#enum_name> for #enum_name {
fn read(_binary_readerr: &mut ::binary_utils::io::ByteReader) -> ::std::result::Result<#enum_name, ::std::io::Error> {
impl ::binary_util::interfaces::Reader<#enum_name> for #enum_name {
fn read(_binary_readerr: &mut ::binary_util::io::ByteReader) -> ::std::result::Result<#enum_name, ::std::io::Error> {
match <#repr_type>::read(_binary_readerr)? {
#(#read_streams)*
_ => Err(::std::io::Error::new(::std::io::ErrorKind::InvalidData, "Invalid enum discriminant."))
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) type AstContext<'a> = (
&'a syn::Visibility,
);

// BinaryEncoder is a derive macro that implements `::binary_utils::interfaces::Reader<T>` and `::binary_utils::interfaces::Writer<T>`
// BinaryEncoder is a derive macro that implements `::binary_util::interfaces::Reader<T>` and `::binary_util::interfaces::Writer<T>`
pub(crate) fn binary_encoder(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let ctx: AstContext = (&input.ident, &input.attrs, &input.generics, &input.vis);
Expand Down
22 changes: 11 additions & 11 deletions codegen/src/io/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) fn derive_struct(
error_stream.append_all(
syn::Error::new_spanned(
field,
"Cannot have more than one binary_utils Attribute on a single field!",
"Cannot have more than one binary_util Attribute on a single field!",
)
.to_compile_error(),
);
Expand Down Expand Up @@ -117,14 +117,14 @@ pub(crate) fn derive_struct(
}
}
quote! {
impl ::binary_utils::interfaces::Writer for #struct_name {
fn write(&self, _binary_writew: &mut ::binary_utils::io::ByteWriter) -> Result<(), ::std::io::Error> {
impl ::binary_util::interfaces::Writer for #struct_name {
fn write(&self, _binary_writew: &mut ::binary_util::io::ByteWriter) -> Result<(), ::std::io::Error> {
#writer
Ok(())
}
}
impl ::binary_utils::interfaces::Reader<#struct_name> for #struct_name {
fn read(_binary_readerr: &mut ::binary_utils::io::ByteReader) -> Result<#struct_name, ::std::io::Error> {
impl ::binary_util::interfaces::Reader<#struct_name> for #struct_name {
fn read(_binary_readerr: &mut ::binary_util::io::ByteReader) -> Result<#struct_name, ::std::io::Error> {
// println!("impl Reader for {} called!\n-> {}", stringify!(#struct_name), stringify!(#reader));
#reader
Ok(Self {
Expand Down Expand Up @@ -153,7 +153,7 @@ pub(crate) fn derive_struct(
error_stream.append_all(
syn::Error::new_spanned(
field,
"Cannot have more than one binary_utils Attribute on a field!",
"Cannot have more than one binary_util Attribute on a field!",
)
.to_compile_error(),
);
Expand Down Expand Up @@ -198,14 +198,14 @@ pub(crate) fn derive_struct(
// .map(|i| syn::Ident::new(&format!("__unnamed_{}", i), proc_macro2::Span::call_site()))
// .collect();
quote! {
impl ::binary_utils::interfaces::Writer for #struct_name {
fn write(&self, _binary_writew: &mut ::binary_utils::io::ByteWriter) -> ::std::result::Result<(), ::std::io::Error> {
impl ::binary_util::interfaces::Writer for #struct_name {
fn write(&self, _binary_writew: &mut ::binary_util::io::ByteWriter) -> ::std::result::Result<(), ::std::io::Error> {
#writer
Ok(())
}
}
impl ::binary_utils::interfaces::Reader<#struct_name> for #struct_name {
fn read(_binary_readerr: &mut ::binary_utils::io::ByteReader) -> ::std::result::Result<#struct_name, ::std::io::Error> {
impl ::binary_util::interfaces::Reader<#struct_name> for #struct_name {
fn read(_binary_readerr: &mut ::binary_util::io::ByteReader) -> ::std::result::Result<#struct_name, ::std::io::Error> {
// println!("impl Reader for {} called!\n-> {}", stringify!(#struct_name), stringify!(#reader));
#reader
Ok(Self(
Expand All @@ -218,7 +218,7 @@ pub(crate) fn derive_struct(
Fields::Unit => {
error_stream.append_all(syn::Error::new_spanned(
ast_ctx.0,
"Unit structs are not supported by binary_utils because they have no fields to parse or write.\nThis may change in the future, but for now, please use the skip attribute."
"Unit structs are not supported by binary_util because they have no fields to parse or write.\nThis may change in the future, but for now, please use the skip attribute."
).to_compile_error());
return quote!().into();
}
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/io/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use super::AstContext;
pub(crate) fn derive_union(ast_ctx: AstContext, _: DataUnion, _: &mut TokenStream2) -> TokenStream {
syn::Error::new_spanned(
ast_ctx.0,
"Unions are not supported by binary_utils, there is currently no way to implement the BinaryReader and BinaryWriter traits for unions."
"Unions are not supported by binary_util, there is currently no way to implement the BinaryReader and BinaryWriter traits for unions."
).to_compile_error().into()
}
48 changes: 24 additions & 24 deletions codegen/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@ pub fn stream_parse(input: DeriveInput) -> Result<TokenStream> {
Ok(quote! {
#[automatically_derived]
impl Streamable<#name> for #name {
fn parse(&self) -> Result<Vec<u8>, ::binary_utils::error::BinaryError> {
use ::binary_utils::interfaces::{Reader, Writer};
use ::binary_utils::io::ByteWriter;
fn parse(&self) -> Result<Vec<u8>, ::binary_util::error::BinaryError> {
use ::binary_util::interfaces::{Reader, Writer};
use ::binary_util::io::ByteWriter;
let mut writer = ByteWriter::new();
#writes
Ok(writer.as_slice().to_vec())
}

fn compose(s: &[u8], position: &mut usize) -> Result<Self, ::binary_utils::error::BinaryError> {
use ::binary_utils::interfaces::{Reader, Writer};
fn compose(s: &[u8], position: &mut usize) -> Result<Self, ::binary_util::error::BinaryError> {
use ::binary_util::interfaces::{Reader, Writer};
use ::std::io::Read;
let mut source = ::binary_utils::io::ByteReader::from(s);
let mut source = ::binary_util::io::ByteReader::from(s);
Ok(Self {
#reads
})
}
}

impl ::binary_utils::interfaces::Writer for #name {
fn write(&self, writer: &mut ::binary_utils::io::ByteWriter) -> Result<(), ::std::io::Error> {
use ::binary_utils::interfaces::{Reader, Writer};
impl ::binary_util::interfaces::Writer for #name {
fn write(&self, writer: &mut ::binary_util::io::ByteWriter) -> Result<(), ::std::io::Error> {
use ::binary_util::interfaces::{Reader, Writer};
#writes
Ok(())
}
}

impl ::binary_utils::interfaces::Reader<#name> for #name {
fn read(source: &mut ::binary_utils::io::ByteReader) -> Result<Self, ::std::io::Error> {
use ::binary_utils::interfaces::{Reader, Writer};
impl ::binary_util::interfaces::Reader<#name> for #name {
fn read(source: &mut ::binary_util::io::ByteReader) -> Result<Self, ::std::io::Error> {
use ::binary_util::interfaces::{Reader, Writer};
// get the repr type and read it
Ok(Self {
#new_reads
Expand Down Expand Up @@ -202,18 +202,18 @@ pub fn stream_parse(input: DeriveInput) -> Result<TokenStream> {

Ok(quote! {
#[automatically_derived]
impl ::binary_utils::interfaces::Streamable<#name> for #name {
fn parse(&self) -> Result<Vec<u8>, ::binary_utils::error::BinaryError> {
use ::binary_utils::interfaces::{Reader, Writer};
impl ::binary_util::interfaces::Streamable<#name> for #name {
fn parse(&self) -> Result<Vec<u8>, ::binary_util::error::BinaryError> {
use ::binary_util::interfaces::{Reader, Writer};
match self {
#(#writers)*
}
}

fn compose(source: &[u8], offset: &mut usize) -> Result<Self, ::binary_utils::error::BinaryError> {
use ::binary_utils::interfaces::{Reader, Writer};
fn compose(source: &[u8], offset: &mut usize) -> Result<Self, ::binary_util::error::BinaryError> {
use ::binary_util::interfaces::{Reader, Writer};
// get the repr type and read it
let v = <#enum_ty>::read(&mut ::binary_utils::io::ByteReader::from(source))?;
let v = <#enum_ty>::read(&mut ::binary_util::io::ByteReader::from(source))?;

match v {
#(#readers)*
Expand All @@ -222,18 +222,18 @@ pub fn stream_parse(input: DeriveInput) -> Result<TokenStream> {
}
}

impl ::binary_utils::interfaces::Writer for #name {
fn write(&self, source: &mut ::binary_utils::io::ByteWriter) -> Result<(), ::std::io::Error> {
use ::binary_utils::interfaces::{Reader, Writer};
impl ::binary_util::interfaces::Writer for #name {
fn write(&self, source: &mut ::binary_util::io::ByteWriter) -> Result<(), ::std::io::Error> {
use ::binary_util::interfaces::{Reader, Writer};
match self {
#(#new_writers)*
}
}
}

impl ::binary_utils::interfaces::Reader<#name> for #name {
fn read(source: &mut ::binary_utils::io::ByteReader) -> Result<Self, ::std::io::Error> {
use ::binary_utils::interfaces::{Reader, Writer};
impl ::binary_util::interfaces::Reader<#name> for #name {
fn read(source: &mut ::binary_util::io::ByteReader) -> Result<Self, ::std::io::Error> {
use ::binary_util::interfaces::{Reader, Writer};
// get the repr type and read it
let v = <#enum_ty>::read(source)?;

Expand Down
32 changes: 16 additions & 16 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ mod legacy;
/// **DEPRECATED**.
/// This is a legacy proc-macro that is used to generate a BufferStream.
/// It provides an easy way to implement the `Streamable` trait.
/// > ⚠️ This proc-macro has been deprecated since `0.3.0` in favor of `binary_utils::interfaces::Reader` and `binary_utils::interfaces::Writer` and will be removed in `0.4.0`.
/// > ⚠️ This proc-macro has been deprecated since `0.3.0` in favor of `binary_util::interfaces::Reader` and `binary_util::interfaces::Writer` and will be removed in `0.4.0`.
///
/// This proc-macro automatically implements the `Streamable` trait for the struct or enum it is applied to.
///
/// Example:
/// ```ignore
/// use binary_utils::BinaryStream;
/// use binary_util::BinaryStream;
///
/// #[derive(BinaryStream)]
/// struct Test {
Expand All @@ -29,7 +29,7 @@ mod legacy;
///
/// Please note that this proc-macro does not support unit structs or named enum variants, meaning a code sample like the following will not work:
/// ```warn
/// use binary_utils::BinaryStream;
/// use binary_util::BinaryStream;
///
/// // Error: Unit structs are not supported.
/// #[derive(BinaryStream)]
Expand All @@ -53,7 +53,7 @@ pub fn derive_stream(input: TokenStream) -> TokenStream {
.into()
}

/// This proc-macro implements both the `Reader` and `Writer` traits from `binary_utils::interfaces`.
/// This proc-macro implements both the `Reader` and `Writer` traits from `binary_util::interfaces`.
/// It is important to note that not all attributes can be used on all types, and some attributes are exclusive to certain variants.
///
/// ## Structs
Expand All @@ -65,8 +65,8 @@ pub fn derive_stream(input: TokenStream) -> TokenStream {
/// The following example will provide both a `Reader` and `Writer` implementation for the struct `ABC`, where each field is encoded as it's respective
/// type to the `Bytewriter`/`Bytereader`.
/// ```ignore
/// use binary_utils::interfaces::{Reader, Writer};
/// use binary_utils::BinaryIo;
/// use binary_util::interfaces::{Reader, Writer};
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo, Debug)]
/// struct ABC {
Expand All @@ -79,8 +79,8 @@ pub fn derive_stream(input: TokenStream) -> TokenStream {
/// Sometimes it can be more optimal to use Unnamed fields, if you do not care about the field names, and only want to encode/decode the fields in the order they are defined.
/// The behavior of this macro is the same as the previous example, except the fields are unnamed.
/// ```ignore
/// use binary_utils::interfaces::{Reader, Writer};
/// use binary_utils::BinaryIo;
/// use binary_util::interfaces::{Reader, Writer};
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo, Debug)]
/// struct ABC(u8, Option<u8>, u8);
Expand All @@ -99,8 +99,8 @@ pub fn derive_stream(input: TokenStream) -> TokenStream {
/// The following example will encode the `ProtcolEnum` enum as a `u8`, where each variant is encoded, by default, starting from 0.
///
/// ```ignore
/// use binary_utils::BinaryIo;
/// use binary_utils::{Reader, Writer};
/// use binary_util::BinaryIo;
/// use binary_util::{Reader, Writer};
///
/// #[derive(BinaryIo, Debug)]
/// #[repr(u8)]
Expand All @@ -120,8 +120,8 @@ pub fn derive_stream(input: TokenStream) -> TokenStream {
/// The following example makes use of Unnamed variants, in this case `A` to encode both `B` and `C` retrospectively.
/// Where `A::JustC` will be encoded as `0x02` with the binary data of struct `B`.
/// ```ignore
/// use binary_utils::BinaryIo;
/// use binary_utils::{Reader, Writer};
/// use binary_util::BinaryIo;
/// use binary_util::{Reader, Writer};
///
/// #[derive(BinaryIo, Debug)]
/// pub struct B {
Expand Down Expand Up @@ -172,8 +172,8 @@ pub fn derive_stream(input: TokenStream) -> TokenStream {
///
/// **Example:**
/// ```ignore
/// use binary_utils::interfaces::{Reader, Writer};
/// use binary_utils::BinaryIo;
/// use binary_util::interfaces::{Reader, Writer};
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo, Debug)]
/// struct ABC {
Expand Down Expand Up @@ -201,8 +201,8 @@ pub fn derive_stream(input: TokenStream) -> TokenStream {
/// **Example:**
/// In the following example, `b` is explicitly required to be present when encoding, or decoding `ABC`, and it's value is not allowed to be `None`.
/// ```ignore
/// use binary_utils::interfaces::{Reader, Writer};
/// use binary_utils::BinaryIo;
/// use binary_util::interfaces::{Reader, Writer};
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo, Debug)]
/// struct ABC {
Expand Down
10 changes: 5 additions & 5 deletions src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ macro_rules! impl_streamable {
/// Allows you to read from a `ByteReader` without needing to know the type.
///
/// ```ignore
/// use binary_utils::io::{ByteReader, Reader};
/// use binary_util::io::{ByteReader, Reader};
///
/// pub struct MyStruct {
/// pub a: u8,
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Reader<SocketAddr> for SocketAddr {
/// Allows you to write to a `ByteWriter` without needing to know the type.
///
/// ```ignore
/// use binary_utils::io::{ByteWriter, Writer};
/// use binary_util::io::{ByteWriter, Writer};
///
/// pub struct MyStruct {
/// pub a: u8,
Expand Down Expand Up @@ -324,8 +324,8 @@ impl Writer for SocketAddr {
///
/// ### New Implementation Example
/// ```ignore
/// use binary_utils::io::{ByteReader, ByteWriter};
/// use binary_utils::interfaces::{Reader, Writer};
/// use binary_util::io::{ByteReader, ByteWriter};
/// use binary_util::interfaces::{Reader, Writer};
///
/// pub struct MyStruct;
///
Expand All @@ -337,7 +337,7 @@ impl Writer for SocketAddr {
/// A trait to parse and unparse header structs from a given buffer.
///
/// ```ignore
/// use binary_utils::{Streamable, error::BinaryError};
/// use binary_util::{Streamable, error::BinaryError};
///
/// struct Foo {
/// bar: u8,
Expand Down
Loading

0 comments on commit 90db2bb

Please sign in to comment.