Skip to content

Commit

Permalink
feat: add FromIterator impl for ObjectAsVec
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Oct 29, 2024
1 parent 18efa93 commit 23ef00b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/object_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ pub struct ObjectAsVec<'ctx>(pub(crate) Vec<(KeyStrType<'ctx>, Value<'ctx>)>);

impl<'ctx> From<Vec<(&'ctx str, Value<'ctx>)>> for ObjectAsVec<'ctx> {
fn from(vec: Vec<(&'ctx str, Value<'ctx>)>) -> Self {
Self(vec.into_iter().map(|(k, v)| (k.into(), v)).collect())
Self::from_iter(vec)
}
}

impl<'ctx> FromIterator<(&'ctx str, Value<'ctx>)> for ObjectAsVec<'ctx> {
fn from_iter<T: IntoIterator<Item = (&'ctx str, Value<'ctx>)>>(iter: T) -> Self {
Self(iter.into_iter().map(|(k, v)| (k.into(), v)).collect())
}
}

Expand Down Expand Up @@ -207,6 +213,8 @@ impl<'ctx> From<&ObjectAsVec<'ctx>> for serde_json::Map<String, serde_json::Valu
mod tests {
use std::borrow::Cow;

use crate::value::Number;

use super::*;

#[test]
Expand All @@ -216,6 +224,23 @@ mod tests {
assert_eq!(obj.len(), 0);
}

#[test]
fn test_initialization_from_iter() {
let names = "abcde";
let iter = (0usize..)
.take(5)
.map(|i| (&names[i..i + 1], Value::Number(Number::from(i as u64))));

let obj = ObjectAsVec::from_iter(iter);

assert_eq!(obj.len(), 5);
assert_eq!(obj.get("a"), Some(&Value::Number(0u64.into())));
assert_eq!(obj.get("b"), Some(&Value::Number(1u64.into())));
assert_eq!(obj.get("c"), Some(&Value::Number(2u64.into())));
assert_eq!(obj.get("d"), Some(&Value::Number(3u64.into())));
assert_eq!(obj.get("e"), Some(&Value::Number(4u64.into())));
}

#[test]
fn test_non_empty_initialization() {
let obj = ObjectAsVec(vec![("key".into(), Value::Null)]);
Expand Down

0 comments on commit 23ef00b

Please sign in to comment.