arrow attempt compiles, nothing tested yet
This commit is contained in:
parent
ec659b559d
commit
4a7bc8090a
1 changed files with 640 additions and 59 deletions
108
src/record.rs
108
src/record.rs
|
|
@ -8,7 +8,7 @@ use byteorder::{ByteOrder, BigEndian};
|
|||
|
||||
use crate::basic::{pack_datetime, pack_bitarray, pack_ascii, pack_int2, pack_int4, pack_real8}; #[warn(unused_imports)]
|
||||
use crate::basic::{parse_datetime, parse_bitarray, parse_ascii, parse_int2, parse_int4, parse_real8}; #[warn(unused_imports)]
|
||||
use crate::basic::{OResult, IResult, fail, parse_u16, take_bytes};
|
||||
use crate::basic::{OResult, IResult, fail, parse_u16, take_bytes}; //ErrType,
|
||||
use crate::records;
|
||||
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ pub struct RecordHeader {
|
|||
}
|
||||
|
||||
impl RecordHeader {
|
||||
pub fn read(input: &[u8]) -> IResult<RecordHeader> {
|
||||
pub fn read<'a>(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}))
|
||||
|
|
@ -45,13 +45,13 @@ impl RecordHeader {
|
|||
|
||||
|
||||
pub trait RecordData {
|
||||
type BareData;
|
||||
type BareData<'a>;
|
||||
type InData : ?Sized;
|
||||
type ByteData : AsRef<[u8]>;
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData>;
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>>;
|
||||
fn pack_into(buf: &mut [u8], data: &Self::InData);
|
||||
//fn size(data: &Self::BareData) -> u16;
|
||||
//fn size(data: &Self::BareData<'_>) -> u16;
|
||||
fn pack(data: &Self::InData) -> Self::ByteData;
|
||||
}
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ pub trait Record<RData: RecordData> {
|
|||
RecordHeader{tag: Self::tag(), data_size: data_size}.write(ww)
|
||||
}
|
||||
|
||||
fn read_data(input: &[u8], size: u16) -> IResult<RData::BareData> {
|
||||
fn read_data(input: &[u8], size: u16) -> IResult<RData::BareData<'_>> {
|
||||
RData::read(input, size)
|
||||
}
|
||||
|
||||
|
|
@ -108,7 +108,7 @@ pub trait Record<RData: RecordData> {
|
|||
Ok((input, true))
|
||||
}
|
||||
|
||||
fn skip_and_read(input: &[u8]) -> IResult<RData::BareData> {
|
||||
fn skip_and_read(input: &[u8]) -> IResult<RData::BareData<'_>> {
|
||||
let (mut input, mut header) = RecordHeader::read(input)?;
|
||||
while header.tag != Self::tag() {
|
||||
(input, _) = take_bytes(input, header.data_size)?;
|
||||
|
|
@ -127,7 +127,7 @@ pub trait Record<RData: RecordData> {
|
|||
}
|
||||
}
|
||||
|
||||
fn read(input: &[u8]) -> IResult<RData::BareData> {
|
||||
fn read<'a>(input: &'a [u8]) -> IResult<'a, RData::BareData<'a>> {
|
||||
let (input, size) = Self::expect_header(input)?;
|
||||
Self::check_size(size).unwrap();
|
||||
let (input, data) = Self::read_data(input, size)?;
|
||||
|
|
@ -147,11 +147,11 @@ pub trait Record<RData: RecordData> {
|
|||
|
||||
pub struct BitArray;
|
||||
impl RecordData for BitArray {
|
||||
type BareData = [bool; 16];
|
||||
type BareData<'a> = [bool; 16];
|
||||
type InData = [bool; 16];
|
||||
type ByteData = [u8; 2];
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size == 2);
|
||||
parse_bitarray(input)
|
||||
}
|
||||
|
|
@ -170,11 +170,11 @@ impl RecordData for BitArray {
|
|||
|
||||
pub struct Int2;
|
||||
impl RecordData for Int2 {
|
||||
type BareData = i16;
|
||||
type BareData<'a> = i16;
|
||||
type InData = i16;
|
||||
type ByteData = [u8; 2];
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size == 2);
|
||||
parse_int2(input)
|
||||
}
|
||||
|
|
@ -192,11 +192,11 @@ impl RecordData for Int2 {
|
|||
|
||||
pub struct Int4;
|
||||
impl RecordData for Int4 {
|
||||
type BareData = i32;
|
||||
type BareData<'a> = i32;
|
||||
type InData = i32;
|
||||
type ByteData = [u8; 4];
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size == 4);
|
||||
parse_int4(input)
|
||||
}
|
||||
|
|
@ -215,18 +215,15 @@ impl RecordData for Int4 {
|
|||
|
||||
pub struct Int2Array;
|
||||
impl RecordData for Int2Array {
|
||||
type BareData = Vec<i16>;
|
||||
type BareData<'a> = Int2ArrayReader<'a>;
|
||||
type InData = [i16];
|
||||
type ByteData = Vec<u8>;
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size % 2 == 0, "Record must contain an integer quantity of integers");
|
||||
let mut buf = Vec::with_capacity(size as usize / 2);
|
||||
let mut input = input;
|
||||
for ii in 0..buf.len() {
|
||||
(input, buf[ii]) = parse_int2(input)?;
|
||||
}
|
||||
Ok((input, buf))
|
||||
//let mut input = input;
|
||||
let (input, bytes) = take_bytes(input, size)?;
|
||||
Ok((input, Int2ArrayReader{bytes: bytes}))
|
||||
}
|
||||
|
||||
fn pack_into(buf: &mut [u8], data: &Self::InData) {
|
||||
|
|
@ -240,20 +237,34 @@ impl RecordData for Int2Array {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Int2ArrayReader<'a> {
|
||||
bytes: &'a [u8],
|
||||
}
|
||||
impl Iterator for Int2ArrayReader<'_> {
|
||||
type Item = i16;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.bytes.len() < 2 {
|
||||
None
|
||||
} else {
|
||||
let (remaining, val) = parse_int2(self.bytes).unwrap();
|
||||
self.bytes = remaining;
|
||||
Some(val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Int4Array;
|
||||
impl RecordData for Int4Array {
|
||||
type BareData = Vec<i32>;
|
||||
type BareData<'a> = Int4ArrayReader<'a>;
|
||||
type InData = [i32];
|
||||
type ByteData = Vec<u8>;
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size % 4 == 0, "Record must contain an integer quantity of integers");
|
||||
let mut buf = Vec::with_capacity(size as usize / 4);
|
||||
let mut input = input;
|
||||
for ii in 0..buf.len() {
|
||||
(input, buf[ii]) = parse_int4(input)?;
|
||||
}
|
||||
Ok((input, buf))
|
||||
//let mut input = input;
|
||||
let (input, bytes) = take_bytes(input, size)?;
|
||||
Ok((input, Int4ArrayReader{bytes: bytes}))
|
||||
}
|
||||
|
||||
fn pack_into(buf: &mut [u8], data: &Self::InData) {
|
||||
|
|
@ -267,13 +278,30 @@ impl RecordData for Int4Array {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Int4ArrayReader<'a> {
|
||||
bytes: &'a [u8],
|
||||
}
|
||||
impl Iterator for Int4ArrayReader<'_> {
|
||||
type Item = i32;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.bytes.len() < 4 {
|
||||
None
|
||||
} else {
|
||||
let (remaining, val) = parse_int4(self.bytes).unwrap();
|
||||
self.bytes = remaining;
|
||||
Some(val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Real8;
|
||||
impl RecordData for Real8 {
|
||||
type BareData = f64;
|
||||
type BareData<'a> = f64;
|
||||
type InData = f64;
|
||||
type ByteData = [u8; 8];
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size == 8);
|
||||
parse_real8(input)
|
||||
}
|
||||
|
|
@ -291,11 +319,11 @@ impl RecordData for Real8 {
|
|||
|
||||
pub struct Real8Pair;
|
||||
impl RecordData for Real8Pair {
|
||||
type BareData = (f64, f64);
|
||||
type BareData<'a> = (f64, f64);
|
||||
type InData = (f64, f64);
|
||||
type ByteData = [u8; 2 * 8];
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size == 2 * 8);
|
||||
let (input, data0) = parse_real8(input)?;
|
||||
let (input, data1) = parse_real8(input)?;
|
||||
|
|
@ -321,11 +349,11 @@ impl RecordData for Real8Pair {
|
|||
|
||||
pub struct ASCII;
|
||||
impl RecordData for ASCII {
|
||||
type BareData = Vec<u8>;
|
||||
type BareData<'a> = Vec<u8>;
|
||||
type InData = [u8];
|
||||
type ByteData = Vec<u8>;
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
parse_ascii(input, size)
|
||||
}
|
||||
|
||||
|
|
@ -343,11 +371,11 @@ impl RecordData for ASCII {
|
|||
|
||||
pub struct DateTimePair;
|
||||
impl RecordData for DateTimePair {
|
||||
type BareData = [[i16; 6]; 2];
|
||||
type BareData<'a> = [[i16; 6]; 2];
|
||||
type InData = [[i16; 6]; 2];
|
||||
type ByteData = [u8; 2 * 6 * 2];
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size == 2 * 6 * 2);
|
||||
let (input, data0) = parse_datetime(input)?;
|
||||
let (input, data1) = parse_datetime(input)?;
|
||||
|
|
@ -373,11 +401,11 @@ impl RecordData for DateTimePair {
|
|||
|
||||
pub struct Empty;
|
||||
impl RecordData for Empty {
|
||||
type BareData = ();
|
||||
type BareData<'a> = ();
|
||||
type InData = ();
|
||||
type ByteData = [u8; 0];
|
||||
|
||||
fn read(input: &[u8], size: u16) -> IResult<Self::BareData> {
|
||||
fn read<'a>(input: &'a [u8], size: u16) -> IResult<'a, Self::BareData<'a>> {
|
||||
assert!(size == 0);
|
||||
Ok((input, ()))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue