From 8bff68fccc0fb91ac96d6352d54b2536824bfa7c Mon Sep 17 00:00:00 2001 From: KAMADA Ken'ichi Date: Thu, 3 Nov 2016 19:49:10 +0900 Subject: [PATCH] Implement and test parse_byte(). --- src/value.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/value.rs b/src/value.rs index 8d5cc45..cf5e8e8 100644 --- a/src/value.rs +++ b/src/value.rs @@ -29,6 +29,8 @@ use endian::Endian; /// Types and values of TIFF fields (for Exif attributes). #[derive(Debug)] pub enum Value<'a> { + /// Vector of 8-bit unsigned integers. + Byte(Vec), /// Slice of 8-bit bytes containing 7-bit ASCII characters. /// The trailing null character is not included. Note that /// the absence of the 8th bits is not guaranteed. @@ -47,12 +49,18 @@ type Parser<'a> = fn(&'a [u8], usize, usize) -> Value<'a>; pub fn get_type_info<'a, E>(typecode: u16) -> (usize, Parser<'a>) where E: Endian { match typecode { + 1 => (1, parse_byte), 2 => (1, parse_ascii), 3 => (2, parse_short::), _ => (0, parse_unknown), } } +fn parse_byte<'a>(data: &'a [u8], offset: usize, count: usize) + -> Value<'a> { + Value::Byte(data[offset .. offset + count].to_vec()) +} + fn parse_ascii<'a>(data: &'a [u8], offset: usize, count: usize) -> Value<'a> { // Any ASCII field can contain multiple strings [TIFF6 Image File @@ -83,9 +91,25 @@ fn parse_unknown<'a>(data: &'a [u8], offset: usize, count: usize) #[cfg(test)] mod tests { + use endian::BigEndian; use super::*; use super::parse_ascii; + #[test] + fn byte() { + let sets: &[(&[u8], &[u8])] = &[ + (b"x", b""), + (b"x\xbe\xad", b"\xbe\xad"), + ]; + let (unitlen, parser) = get_type_info::(1); + for &(data, ans) in sets { + match parser(data, 1, (data.len() - 1) / unitlen) { + Value::Byte(v) => assert_eq!(v, ans), + v => panic!("wrong variant {:?}", v), + } + } + } + #[test] fn ascii() { let sets: &[(&[u8], Vec<&[u8]>)] = &[