various cleanup and add some example interfaces

This commit is contained in:
jan 2025-04-11 11:18:56 -07:00
commit 4aa14c4914

View file

@ -23,10 +23,10 @@ pub struct RecordHeader {
}
impl RecordHeader {
pub fn read<'a>(input: &[u8]) -> IResult<RecordHeader> {
pub fn read(input: &[u8]) -> IResult<RecordHeader> {
let (input, size) = parse_u16(input)?;
let (input, tag) = parse_u16(input)?;
Ok((input, RecordHeader{tag:tag, data_size:size - 4}))
Ok((input, RecordHeader{tag, data_size:size - 4}))
}
pub fn pack_into(&self) -> [u8; 4] {
@ -49,7 +49,7 @@ pub trait RecordData {
type InData : ?Sized;
type ByteData : AsRef<[u8]>;
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>>;
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>>;
fn pack_into(buf: &mut [u8], data: &Self::InData);
//fn size(data: &Self::BareData<'_>) -> u16;
fn pack(data: &Self::InData) -> Self::ByteData;
@ -76,7 +76,7 @@ pub trait Record<RData: RecordData> {
}
fn write_header<W: Write>(ww: &mut W, data_size: u16) -> OResult {
RecordHeader{tag: Self::tag(), data_size: data_size}.write(ww)
RecordHeader{tag: Self::tag(), data_size}.write(ww)
}
fn read_data(input: &[u8], size: u16) -> IResult<RData::BareData<'_>> {
@ -127,7 +127,7 @@ pub trait Record<RData: RecordData> {
}
}
fn read<'a>(input: &'a [u8]) -> IResult<'a, RData::BareData<'a>> {
fn read(input: &[u8]) -> IResult<RData::BareData<'_>> {
let (input, size) = Self::expect_header(input)?;
Self::check_size(size).unwrap();
let (input, data) = Self::read_data(input, size)?;
@ -151,7 +151,7 @@ impl RecordData for BitArray {
type InData = [bool; 16];
type ByteData = [u8; 2];
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size == 2);
parse_bitarray(input)
}
@ -174,7 +174,7 @@ impl RecordData for Int2 {
type InData = i16;
type ByteData = [u8; 2];
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size == 2);
parse_int2(input)
}
@ -196,7 +196,7 @@ impl RecordData for Int4 {
type InData = i32;
type ByteData = [u8; 4];
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size == 4);
parse_int4(input)
}
@ -219,15 +219,15 @@ impl RecordData for Int2Array {
type InData = [i16];
type ByteData = Vec<u8>;
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size % 2 == 0, "Record must contain an integer quantity of integers");
//let mut input = input;
let (input, bytes) = take_bytes(input, size)?;
Ok((input, Int2ArrayReader{bytes: bytes}))
Ok((input, Int2ArrayReader{bytes}))
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
BigEndian::write_i16_into(&data, buf)
BigEndian::write_i16_into(data, buf)
}
fn pack(data: &Self::InData) -> Self::ByteData {
@ -260,15 +260,15 @@ impl RecordData for Int4Array {
type InData = [i32];
type ByteData = Vec<u8>;
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size % 4 == 0, "Record must contain an integer quantity of integers");
//let mut input = input;
let (input, bytes) = take_bytes(input, size)?;
Ok((input, Int4ArrayReader{bytes: bytes}))
Ok((input, Int4ArrayReader{bytes}))
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
BigEndian::write_i32_into(&data, buf)
BigEndian::write_i32_into(data, buf)
}
fn pack(data: &Self::InData) -> Self::ByteData {
@ -301,13 +301,13 @@ impl RecordData for Real8 {
type InData = f64;
type ByteData = [u8; 8];
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size == 8);
parse_real8(input)
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_real8(buf, *data).expect(&format!("Float {0} too big for Real8", data))
pack_real8(buf, *data).unwrap_or_else(|_| panic!("Float {0} too big for Real8", data))
}
fn pack(data: &Self::InData) -> Self::ByteData {
@ -323,7 +323,7 @@ impl RecordData for Real8Pair {
type InData = (f64, f64);
type ByteData = [u8; 2 * 8];
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size == 2 * 8);
let (input, data0) = parse_real8(input)?;
let (input, data1) = parse_real8(input)?;
@ -331,8 +331,8 @@ impl RecordData for Real8Pair {
}
fn pack_into(buf: &mut [u8], data: &Self::InData) {
pack_real8(&mut buf[8 * 0..], data.0).expect(&format!("Float.0 {0} too big for Real8", data.0));
pack_real8(&mut buf[8 * 1..], data.1).expect(&format!("Float.1 {0} too big for Real8", data.1));
pack_real8(&mut buf[8 * 0..], data.0).unwrap_or_else(|_| panic!("Float.0 {0} too big for Real8", data.0));
pack_real8(&mut buf[8 * 1..], data.1).unwrap_or_else(|_| panic!("Float.1 {0} too big for Real8", data.1));
}
fn pack(data: &Self::InData) -> Self::ByteData {
@ -353,7 +353,7 @@ impl RecordData for ASCII {
type InData = [u8];
type ByteData = Vec<u8>;
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
parse_ascii(input, size)
}
@ -375,7 +375,7 @@ impl RecordData for DateTimePair {
type InData = [[i16; 6]; 2];
type ByteData = [u8; 2 * 6 * 2];
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size == 2 * 6 * 2);
let (input, data0) = parse_datetime(input)?;
let (input, data1) = parse_datetime(input)?;
@ -405,7 +405,7 @@ impl RecordData for Empty {
type InData = ();
type ByteData = [u8; 0];
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
fn read(input: &[u8], size: u16) -> IResult<Self::BareData<'_>> {
assert!(size == 0);
Ok((input, ()))
}