commands: verify key uniqueness for ResponseAttributes -> HashMap
This commit is contained in:
parent
bc1a4f11a5
commit
2e0732e579
@ -1,4 +1,7 @@
|
||||
use std::{collections::HashMap, str::SplitWhitespace};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
str::SplitWhitespace,
|
||||
};
|
||||
|
||||
use crate::Request;
|
||||
|
||||
@ -114,7 +117,7 @@ pub type GenericResponseResult<'a> = Result<GenericResponse<'a>, &'a str>;
|
||||
|
||||
pub type GenericResponse<'a> = HashMap<&'a str, GenericResponseValue<'a>>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum GenericResponseValue<'a> {
|
||||
Text(&'a str),
|
||||
Binary(&'a [u8]),
|
||||
@ -144,6 +147,11 @@ impl<'a> From<HashMap<&'a str, GenericResponseValue<'a>>> for ResponseAttributes
|
||||
|
||||
impl<'a> From<ResponseAttributes<'a>> for HashMap<&'a str, GenericResponseValue<'a>> {
|
||||
fn from(val: ResponseAttributes<'a>) -> Self {
|
||||
debug_assert!({
|
||||
let mut uniq = HashSet::new();
|
||||
val.0.iter().all(move |x| uniq.insert(*x))
|
||||
});
|
||||
|
||||
val.0.into_iter().collect()
|
||||
}
|
||||
}
|
||||
@ -258,3 +266,39 @@ pub(crate) use get_and_parse_optional_property;
|
||||
pub(crate) use get_and_parse_property;
|
||||
pub(crate) use get_optional_property;
|
||||
pub(crate) use get_property;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[cfg(debug_assertions)]
|
||||
fn test_valid_hashmap_uniqueness_assert() {
|
||||
let valid_maplike_attrs: ResponseAttributes = vec![
|
||||
("a", GenericResponseValue::Text("1")),
|
||||
("A", GenericResponseValue::Text("2")),
|
||||
("A ", GenericResponseValue::Text("3")),
|
||||
("b", GenericResponseValue::Text("4")),
|
||||
]
|
||||
.into();
|
||||
|
||||
let map: HashMap<_, _> = valid_maplike_attrs.into();
|
||||
assert_eq!(map.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(debug_assertions)]
|
||||
#[should_panic]
|
||||
fn test_invalid_hashmap_uniqueness_assert() {
|
||||
let invalid_maplike_attrs: ResponseAttributes = vec![
|
||||
("a", GenericResponseValue::Text("1")),
|
||||
("b", GenericResponseValue::Text("2")),
|
||||
("c", GenericResponseValue::Text("3")),
|
||||
("a", GenericResponseValue::Text("4")),
|
||||
]
|
||||
.into();
|
||||
|
||||
let map: HashMap<_, _> = invalid_maplike_attrs.into();
|
||||
assert_eq!(map.len(), 4);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user