1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
//! Orio is a fairly small library that works on top of little-endian byteorder.
//! It's intended to automate the boilerplate of serializing types like packets
//! and still give the user control of the data's representation.
//!
//! # Io trait
//! The [Io] trait allows types to be written and read generically to byte vecs.
//! See its documentation for usage and manual implementation.
//!
//! # Derive macro
//! The derive macro `#[derive(Io)]` can be used to trivially implement `Io` for your types.
//! All serialized fields must implement the `Io` trait for it to work.
//! Fields can have the `#[io(ignore)]` attribute added which will:
//! 1. Ignore the field when writing the full struct/enum.
//! 2. Use `Default` for reading as there is nothing to read from.
//! Some types like `String` or `Vec` have a length field which can be changed.
//! To use these, add the `#[io(len(TYPE))]` attribute, where TYPE is an int like u8, u16, etc.
//! This lets you customize how much data you want a field to support, e.g. for a filename 255 bytes
//! might be fine but you'd want u64 for the file contents.
//!
//! # Std types
//! Currently a few types from std are supported:
//! - All numbers
//! - `String`
//! - `Box` `HashMap` `Option` and `Vec` of types that implement `Io`
//! - `Duration` and `SystemTime`. These are both stored as milliseconds.
//! Duration uses `#[io(len)]` to change the lengths of times you want to support.
//! Since u16 is a little over a minute, you'l usually want u32 or u64.
mod io;
mod impls;
#[cfg(test)]
mod tests;
mod tuple;
#[cfg(feature = "derive")]
pub use orio_derive::*;
pub use crate::io::*;
use std::io::{Error, ErrorKind};
pub(crate) use byteorder::LittleEndian as LE;
/// Helper for enum [Io::read] implementations to use.
#[cfg(feature = "derive")]
pub fn derive_enum_variant_error(variant: usize, ident: &str) -> Error {
Error::new(ErrorKind::Other, format!("Invalid enum variant {variant} for {ident}"))
}