Skip to content

Commit

Permalink
100% code coverage again, now with 'newtype_identified_vec' macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 16, 2023
1 parent 338fa15 commit 0064f3e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
13 changes: 4 additions & 9 deletions identified_vec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro_rules! newtype_identified_vec {

impl std::fmt::Display for $struct_name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.elements().fmt(f)
std::fmt::Display::fmt(&self.0, f)
}
}

Expand All @@ -47,7 +47,7 @@ macro_rules! newtype_identified_vec {
where
S: Serializer,
{
Vec::serialize(&self.elements(), serializer)
IdentifiedVecOf::serialize(&self.0, serializer)
}
}

Expand All @@ -60,13 +60,8 @@ macro_rules! newtype_identified_vec {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<$struct_name, D::Error> {
let elements = Vec::<$item_ty>::deserialize(deserializer)?;
IdentifiedVecOf::<$item_ty>::try_from_iter_select_unique_with(
elements,
|(idx, _, _)| Err(IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(idx)),
)
.map(|id_vec_of| Self::from_identified_vec_of(id_vec_of))
.map_err(de::Error::custom)
let id_vec_of = IdentifiedVecOf::<$item_ty>::deserialize(deserializer)?;
return Ok(Self::from_identified_vec_of(id_vec_of));
}
}
};
Expand Down
32 changes: 22 additions & 10 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ fn into_iter() {
}
}

#[test]
fn into_iter_identified_vec() {
type Users = IdentifiedVecOf<User>;
let vec = vec![User::blob(), User::blob_jr(), User::blob_sr()];
let identified_vec = Users::from_iter(vec.clone());
for (idx, element) in identified_vec.into_iter().enumerate() {
assert_eq!(vec[idx], element)
}
}

#[test]
fn iter() {
let vec = vec![User::blob(), User::blob_jr(), User::blob_sr()];
Expand Down Expand Up @@ -395,6 +405,7 @@ fn update_with() {
sut.append(User::new(2, "Blob, Jr."));
sut.update_with(&2, |u| u.name.borrow_mut().make_ascii_uppercase());
assert_eq!(sut.items(), [User::new(2, "BLOB, JR.")]);
assert_eq!(sut.update_with(&999, |_| panic!("not called")), false);
}

#[test]
Expand Down Expand Up @@ -544,8 +555,8 @@ fn serde_using_vec() {

#[test]
fn eq() {
#[derive(Eq, PartialEq, Clone, Hash, Debug)]
struct Foo {
#[derive(Eq, PartialEq, Clone, Hash, Debug, Serialize, Deserialize)]
pub struct Foo {
id: &'static str,
value: String,
}
Expand All @@ -566,16 +577,17 @@ fn eq() {
}

// Create `IdentifiedVec` using all of the initializers
let mut vecs: Vec<IdentifiedVecOf<Foo>> = vec![
IdentifiedVecOf::new(),
IdentifiedVecOf::new_identifying_element(|e| e.id()),
IdentifiedVecOf::from_iter_select_unique_with([], |_| ConflictResolutionChoice::ChooseLast),
IdentifiedVecOf::from_iter_select_unique_ids_with(
newtype_identified_vec!(of: Foo, named: SUT);
let mut vecs: Vec<SUT> = vec![
SUT::new(),
SUT::new_identifying_element(|e| e.id()),
SUT::from_iter_select_unique_with([], |_| ConflictResolutionChoice::ChooseLast),
SUT::from_iter_select_unique_ids_with(
[],
|e| e.id(),
|_| ConflictResolutionChoice::ChooseLast,
),
IdentifiedVecOf::try_from_iter_select_unique_ids_with(
SUT::try_from_iter_select_unique_ids_with(
[],
|e: &Foo| e.id(),
|_| Result::<_, ()>::Ok(ConflictResolutionChoice::ChooseLast),
Expand All @@ -584,7 +596,7 @@ fn eq() {
];

assert_eq!(
IdentifiedVecOf::try_from_iter_select_unique_ids_with(
SUT::try_from_iter_select_unique_ids_with(
[Foo::new(), Foo::new()],
|e: &Foo| e.id(),
|_| Err(IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1)),
Expand All @@ -593,7 +605,7 @@ fn eq() {
);

assert_eq!(
IdentifiedVecOf::try_from_iter_select_unique_with([Foo::new(), Foo::new()], |_| Err(
SUT::try_from_iter_select_unique_with([Foo::new(), Foo::new()], |_| Err(
IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1)
),),
Err(IdentifiedVecOfSerdeFailure::DuplicateElementsAtIndex(1))
Expand Down

0 comments on commit 0064f3e

Please sign in to comment.