// Copyright 2018 Kyle Mayes // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use tarrasque::{Endianness, Extract, Span, Stream, View, extract}; use tarrasque::Endianness::*; extract! { /// A 2D point. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct PointA { /// The x-coordinate of this point. pub x: u16 = [], /// The y-coordinate of this point. pub y: u16 = [], } } extract! { /// A 2D point. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct PointB[4] { /// The x-coordinate of this point. pub x: u16 = [], /// The y-coordinate of this point. pub y: u16 = [], } } extract! { /// A 2D point. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct PointC(endianness: Endianness) { /// The x-coordinate of this point. pub x: u16 = [endianness], /// The y-coordinate of this point. pub y: u16 = [endianness], } } extract! { /// A 2D point. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct PointD[4](endianness: Endianness) { /// The x-coordinate of this point. pub x: u16 = [endianness], /// The y-coordinate of this point. pub y: u16 = [endianness], } } #[test] fn test_point() { let mut stream = Stream(&[1, 2, 3, 4, 5, 6, 7, 8]); assert_eq!(stream.extract::(()), Ok(PointA { x: 258, y: 772 })); assert_eq!(stream.extract::(()), Ok(PointA { x: 1286, y: 1800 })); let mut stream = Stream(&[1, 2, 3, 4, 5, 6, 7, 8]); assert_eq!(stream.extract::(()), Ok(PointB { x: 258, y: 772 })); assert_eq!(stream.extract::(()), Ok(PointB { x: 1286, y: 1800 })); assert_eq!(PointB::SPAN, 4); let mut stream = Stream(&[1, 2, 3, 4, 5, 6, 7, 8]); assert_eq!(stream.extract::(Big), Ok(PointC { x: 258, y: 772 })); assert_eq!(stream.extract::(Little), Ok(PointC { x: 1541, y: 2055 })); let mut stream = Stream(&[1, 2, 3, 4, 5, 6, 7, 8]); assert_eq!(stream.extract::(Big), Ok(PointD { x: 258, y: 772 })); assert_eq!(stream.extract::(Little), Ok(PointD { x: 1541, y: 2055 })); assert_eq!(PointD::SPAN, 4); } extract! { /// A list of values. #[derive(Debug, PartialEq, Eq)] pub struct List<'s, T, P>(parameter: P) where T: Extract<'s, P> + Span, P: Copy { /// The bytes in this list. pub bytes: &'s [u8] = &stream[..], /// The number of values in this list. pub size: u8 = [], /// The values in this list. pub values: View<'s, T, P> = [(size as usize, parameter)], } } #[test] fn test_list() { let mut stream = Stream(&[2, 1, 2, 3, 4, 5, 6, 7, 8]); let list = stream.extract::, _>(()).unwrap(); assert_eq!(list.bytes, &[2, 1, 2, 3, 4, 5, 6, 7, 8]); assert_eq!(list.size, 2); assert_eq!(list.values.iter().collect::>(), &[ PointB { x: 258, y: 772 }, PointB { x: 1286, y: 1800 }, ]); let mut stream = Stream(&[2, 1, 2, 3, 4, 5, 6, 7, 8]); let list = stream.extract::, _>(Little).unwrap(); assert_eq!(list.bytes, &[2, 1, 2, 3, 4, 5, 6, 7, 8]); assert_eq!(list.size, 2); assert_eq!(list.values.iter().collect::>(), &[ PointD { x: 513, y: 1027 }, PointD { x: 1541, y: 2055 }, ]); } extract! { /// An array of four bytes. #[derive(Debug, PartialEq, Eq)] pub struct Array<'s>[4] { /// The bytes in this array. pub bytes: &'s [u8] = [4], /// The first byte in this array. pub first: u8 = bytes[0], } } #[test] fn test_array() { let mut stream = Stream(&[1, 2, 3, 4]); let array = stream.extract::(()).unwrap(); assert_eq!(array.bytes, &[1, 2, 3, 4]); assert_eq!(array.first, 1); }