Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize/deserialize Report #90

Merged
merged 2 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion garde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default = [
"regex",
"unicode",
]
serde = ["dep:serde", "compact_str/serde"]
serde = ["dep:serde", "compact_str/serde", "smallvec/serde"]
derive = ["dep:garde_derive"]
url = ["dep:url"]
unicode = ["dep:unicode-segmentation"]
Expand Down Expand Up @@ -59,6 +59,7 @@ trybuild = { version = "1.0" }
insta = { version = "1.29" }
owo-colors = { version = "3.5.0" }
glob = "0.3.1"
serde_json = { version = "1.0.112", features = ["preserve_order"] }

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies]
wasm-bindgen-test = "0.3.38"
57 changes: 50 additions & 7 deletions garde/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use self::rc_list::List;
/// A single field or list item may have any number of errors attached to it.
///
/// It is possible to extract all errors for specific field using the [`select`][`crate::select`] macro.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Report {
errors: Vec<(Path, Error)>,
}
Expand Down Expand Up @@ -58,8 +58,8 @@ impl std::fmt::Display for Report {

impl std::error::Error for Report {}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Error {
message: CompactString,
}
Expand Down Expand Up @@ -91,6 +91,8 @@ pub struct Path {

#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Kind {
None,
Key,
Expand Down Expand Up @@ -167,7 +169,9 @@ impl Path {
}

#[doc(hidden)]
pub fn __iter(&self) -> impl DoubleEndedIterator<Item = (Kind, &CompactString)> {
pub fn __iter(
&self,
) -> impl DoubleEndedIterator<Item = (Kind, &CompactString)> + ExactSizeIterator {
let mut components = TempComponents::with_capacity(self.components.len());
for (kind, component) in self.components.iter() {
components.push((*kind, component));
Expand Down Expand Up @@ -230,8 +234,28 @@ impl serde::Serialize for Path {
where
S: serde::Serializer,
{
let str = self.to_compact_string();
serializer.serialize_str(str.as_str())
use serde::ser::SerializeSeq as _;

let components = self.__iter().rev();
let mut seq = serializer.serialize_seq(Some(components.len()))?;
for component in components {
seq.serialize_element(&component)?;
}
seq.end()
}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Path {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut components = List::new();
for v in SmallVec::<[(Kind, CompactString); 8]>::deserialize(deserializer)? {
components = components.append(v);
}
Ok(Path { components })
}
}

Expand Down Expand Up @@ -271,4 +295,23 @@ mod tests {
[&Error::new("pog")]
);
}

#[cfg(feature = "serde")]
mod serde {
use super::*;

#[test]
fn roundtrip_serde() {
let mut report = Report::new();
report.append(Path::new("a").join(0), Error::new("lorem"));
report.append(Path::new("a").join(1), Error::new("ispum"));
report.append(Path::new("a").join(2), Error::new("dolor"));
report.append(Path::new("b").join("c"), Error::new("dolor"));

let de: Report =
serde_json::from_str(&serde_json::to_string(&report).unwrap()).unwrap();

assert_eq!(report.errors, de.errors);
}
}
}