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 49 50 51
//! Traits for implementing encoding ([`Encodable`]) and decoding ([`Decodable`])
//! for different types and [`ValueType`] trait for specifying the Tdf type of a type
use crate::{error::DecodeResult, reader::TdfReader, tag::TdfType, writer::TdfWriter};
/// Trait for something that can be decoded from a TdfReader
pub trait Decodable: Sized {
/// Function for implementing decoding of Self from
/// the provided Reader. Will return None if self
/// cannot be decoded
///
/// `reader` The reader to decode from
fn decode(reader: &mut TdfReader) -> DecodeResult<Self>;
}
/// Trait for something that can be encoded onto a TdfWriter
pub trait Encodable: Sized {
/// Function for implementing encoding of Self to the
/// provided vec of bytes
///
/// `writer` The output to encode to
fn encode(&self, writer: &mut TdfWriter);
/// Shortcut function for encoding self directly to
/// a Vec of bytes
fn encode_bytes(&self) -> Vec<u8> {
let mut output = TdfWriter::default();
self.encode(&mut output);
output.into()
}
}
/// Trait for a type that conforms to one of the standard TdfTypes
/// used on structures that implement Decodable or Encodable to allow
/// them to be encoded as tag fields
pub trait ValueType {
/// The type of tdf value this is
fn value_type() -> TdfType;
}
/// Macro for generating the ValueType implementation for a type
#[macro_export]
macro_rules! value_type {
($for:ty, $type:expr) => {
impl $crate::codec::ValueType for $for {
fn value_type() -> $crate::tag::TdfType {
$type
}
}
};
}