Implement and test parse_byte().
This commit is contained in:
parent
4175aa5bc4
commit
8bff68fccc
24
src/value.rs
24
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<u8>),
|
||||
/// 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::<E>),
|
||||
_ => (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::<BigEndian>(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]>)] = &[
|
||||
|
|
Loading…
Reference in New Issue