types: add string multimap
@@ -244,3 +244,47 @@ fn type_string_list() {
write_string_list(&val, &mut buf).unwrap();
assert_eq!(read_string_list(&mut &buf[..]).unwrap(), val);
}
+
+pub fn read_string_multimap(buf: &mut &[u8]) -> Result<HashMap<String, Vec<String>>> {
+ let mut v = HashMap::new();
+ let len = read_short(buf)?;
+ for _ in 0..len {
+ let key = read_string(buf)?.to_owned();
+ let val = read_string_list(buf)?;
+ v.insert(key, val);
+ }
+ Ok(v)
+}
+
+pub fn write_string_multimap(
+ v: &HashMap<String, Vec<String>>,
+ buf: &mut impl BufMut,
+) -> Result<()> {
+ let len = v.len();
+ if v.len() > i16::MAX as usize {
+ return Err(anyhow!(
+ "String map has too many entries for 16-bits: {}",
+ len
+ ));
+ }
+ write_short(len as i16, buf);
+ for (key, val) in v.iter() {
+ write_string(key, buf)?;
+ write_string_list(val, buf)?;
+ }
+ Ok(())
+}
+
+#[test]
+fn type_string_multimap() {
+ let mut val = HashMap::new();
+ val.insert(String::from(""), vec![String::from("")]);
+ val.insert(
+ String::from("versions"),
+ vec![String::from("3.0.0"), String::from("4.2.0")],
+ );
+ val.insert(String::from("empty"), vec![]);
+ let mut buf = Vec::new();
+ write_string_multimap(&val, &mut buf).unwrap();
+ assert_eq!(read_string_multimap(&mut &buf[..]).unwrap(), val);
+}
