1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::io;

use crate::alignment::record::data::field::value::array::Subtype;

pub(super) fn parse_subtype(src: &mut &[u8]) -> io::Result<Subtype> {
    let (b, rest) = src
        .split_first()
        .ok_or_else(|| io::Error::from(io::ErrorKind::UnexpectedEof))?;

    let subtype = match *b {
        b'c' => Subtype::Int8,
        b'C' => Subtype::UInt8,
        b's' => Subtype::Int16,
        b'S' => Subtype::UInt16,
        b'i' => Subtype::Int32,
        b'I' => Subtype::UInt32,
        b'f' => Subtype::Float,
        _ => {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "invalid subtype",
            ));
        }
    };

    *src = rest;

    Ok(subtype)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_subtype() -> io::Result<()> {
        fn t(mut src: &[u8], expected: Subtype) -> io::Result<()> {
            assert_eq!(parse_subtype(&mut src)?, expected);
            Ok(())
        }

        t(b"c", Subtype::Int8)?;
        t(b"C", Subtype::UInt8)?;
        t(b"s", Subtype::Int16)?;
        t(b"S", Subtype::UInt16)?;
        t(b"i", Subtype::Int32)?;
        t(b"I", Subtype::UInt32)?;
        t(b"f", Subtype::Float)?;

        let mut src = &b""[..];
        assert!(matches!(
            parse_subtype(&mut src),
            Err(e) if e.kind() == io::ErrorKind::UnexpectedEof
        ));

        let mut src = &b"n"[..];
        assert!(matches!(
            parse_subtype(&mut src),
            Err(e) if e.kind() == io::ErrorKind::InvalidData
        ));

        Ok(())
    }
}