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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::{
    cmp,
    io::{self, Write},
};

use super::value::write_value;
use crate::lazy::record::{
    value::{Array, Int16, Int32, Int8},
    Value,
};

pub fn write_string_map_index<W>(writer: &mut W, i: usize) -> io::Result<()>
where
    W: Write,
{
    if let Ok(j) = i8::try_from(i) {
        write_value(writer, Some(Value::Int8(Some(Int8::Value(j)))))
    } else if let Ok(j) = i16::try_from(i) {
        write_value(writer, Some(Value::Int16(Some(Int16::Value(j)))))
    } else if let Ok(j) = i32::try_from(i) {
        write_value(writer, Some(Value::Int32(Some(Int32::Value(j)))))
    } else {
        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            format!("invalid index: {i}"),
        ))
    }
}

pub fn write_string_map_indices<W>(writer: &mut W, indices: &[usize]) -> io::Result<()>
where
    W: Write,
{
    match indices.len() {
        0 => write_value(writer, None),
        1 => match indices.first().copied() {
            Some(i) => write_string_map_index(writer, i),
            None => unreachable!(),
        },
        _ => {
            let mut max = i32::MIN;

            for &i in indices {
                let j =
                    i32::try_from(i).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
                max = cmp::max(max, j);
            }

            if max <= i32::from(Int8::MAX_VALUE) {
                let is = indices.iter().map(|&i| i as i8).collect();
                write_value(writer, Some(Value::Array(Array::Int8(is))))
            } else if max <= i32::from(Int16::MAX_VALUE) {
                let is = indices.iter().map(|&i| i as i16).collect();
                write_value(writer, Some(Value::Array(Array::Int16(is))))
            } else {
                let is = indices.iter().map(|&i| i as i32).collect();
                write_value(writer, Some(Value::Array(Array::Int32(is))))
            }
        }
    }
}

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

    #[test]
    fn test_write_string_map_index() -> io::Result<()> {
        fn t(buf: &mut Vec<u8>, i: usize, expected: &[u8]) -> io::Result<()> {
            buf.clear();
            write_string_map_index(buf, i)?;
            assert_eq!(buf, expected);
            Ok(())
        }

        let mut buf = Vec::new();

        // Int8
        t(&mut buf, 0, &[0x11, 0x00])?;
        t(&mut buf, 127, &[0x11, 0x7f])?;

        // Int16
        t(&mut buf, 128, &[0x12, 0x80, 0x00])?;
        t(&mut buf, 32767, &[0x12, 0xff, 0x7f])?;

        // Int32
        t(&mut buf, 32768, &[0x13, 0x00, 0x80, 0x00, 0x00])?;
        t(&mut buf, 2147483647, &[0x13, 0xff, 0xff, 0xff, 0x7f])?;

        buf.clear();
        assert!(matches!(
            write_string_map_index(&mut buf, 2147483648),
            Err(ref e) if e.kind() == io::ErrorKind::InvalidInput
        ));

        Ok(())
    }

    #[test]
    fn test_write_string_map_indices() -> io::Result<()> {
        fn t(buf: &mut Vec<u8>, indices: &[usize], expected: &[u8]) -> io::Result<()> {
            buf.clear();
            write_string_map_indices(buf, indices)?;
            assert_eq!(buf, expected);
            Ok(())
        }

        let mut buf = Vec::new();

        t(&mut buf, &[], &[0x00])?;

        t(&mut buf, &[0], &[0x11, 0x00])?;
        t(&mut buf, &[128], &[0x12, 0x80, 0x00])?;
        t(&mut buf, &[32768], &[0x13, 0x00, 0x80, 0x00, 0x00])?;

        t(&mut buf, &[0, 127], &[0x21, 0x00, 0x7f])?;
        t(&mut buf, &[0, 32767], &[0x22, 0x00, 0x00, 0xff, 0x7f])?;
        t(
            &mut buf,
            &[0, 2147483647],
            &[0x23, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x7f],
        )?;

        Ok(())
    }
}