Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 16, 2023
1 parent c03d872 commit 659aefb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
50 changes: 33 additions & 17 deletions identified_vec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ macro_rules! newtype_identified_vec {
(of: $item_ty: ty, named: $struct_name: ident) => {
use identified_vec::IdentifiedVecIntoIterator;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct $struct_name(IdentifiedVecOf<$item_ty>);

impl ViaMarker for $struct_name {}
Expand All @@ -29,22 +30,37 @@ macro_rules! newtype_identified_vec {
}
}

// impl<I, E> $struct_name<I, E>
// where
// I: Eq + Hash + Clone + Debug,
// {
// pub fn iter(&self) -> IdentifiedVecIterator<I, E> {
// IdentifiedVecIterator::new(self)
// }
// }

// impl IntoIterator for $struct_name {
// type Item = $item_ty;
// type IntoIter = IdentifiedVecIntoIterator<<$item_ty as Identifiable>::ID, $item_ty>;

// fn into_iter(self) -> Self::IntoIter {
// Self::IntoIter::new(self)
// }
// }
impl Serialize for $struct_name
where
$item_ty: Serialize + Identifiable + Debug + Clone,
{
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
Vec::serialize(&self.elements(), serializer)
}
}

impl<'de> Deserialize<'de> for $struct_name
where
$item_ty: Deserialize<'de> + Identifiable + Debug + Clone,
{
#[cfg(not(tarpaulin_include))] // false negative
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)
}
}
};
}
4 changes: 2 additions & 2 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ version = "0.1.0"
edition = "2021"
publish = false

[dev-dependencies]
[dependencies]
identified_vec = { path = "../identified_vec", features = ["id_prim", "serde"] }
identified_vec_macros = { path = "../identified_vec_macros" }
serde = "1.0.193"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
rand = "0.8.5"
maplit = "1.0.2"
Expand Down
35 changes: 32 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use identified_vec::{
ItemsCloned, ViaMarker,
};

use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use identified_vec_macros::newtype_identified_vec;

#[derive(Eq, PartialEq, Clone)]
#[derive(Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct User {
pub id: u16,
pub name: RefCell<String>,
}

impl User {
fn new(id: u16, name: &str) -> Self {
if name.is_empty() {
Expand Down Expand Up @@ -483,7 +484,7 @@ fn remove_at_out_of_bounds() {
}

#[test]
fn serde() {
fn serde_identified_vec_of() {
let identified_vec = SUT::from_iter([1, 2, 3]);
assert_eq!(
serde_json::to_value(identified_vec.clone())
Expand All @@ -506,6 +507,34 @@ fn serde() {
assert!(serde_json::from_str::<SUT>("invalid").is_err(),);
}

#[test]
fn serde_is_identified_vec() {
newtype_identified_vec!(of: u32, named: Ints);

let identified_vec = Ints::from_iter([1, 2, 3]);
let cloned = identified_vec.clone();
assert_eq!(&cloned, &identified_vec);
assert_eq!(
serde_json::to_value(identified_vec.clone())
.and_then(|j| serde_json::from_value::<Ints>(j))
.unwrap(),
identified_vec
);
assert_eq!(
serde_json::from_str::<Ints>("[1,2,3]").unwrap(),
identified_vec
);
assert_eq!(serde_json::to_string(&identified_vec).unwrap(), "[1,2,3]");
assert_eq!(
serde_json::from_str::<Ints>("[1,1,1]")
.expect_err("should fail")
.to_string(),
"Duplicate element at offset 1"
);

assert!(serde_json::from_str::<Ints>("invalid").is_err(),);
}

#[test]
fn serde_via_vec() {
let vec = vec![1, 2, 3];
Expand Down

0 comments on commit 659aefb

Please sign in to comment.