This repository was archived by the owner on Jun 8, 2025. It is now read-only.
forked from davidpdrsn/assert-json-diff
-
Couldn't load subscription status.
- Fork 0
RFC: Better solution than introducing owned types?Β #4
Copy link
Copy link
Open
Description
So here's the original code I tried to modified
pub fn assert_json_matches_no_panic<Lhs, Rhs>(
lhs: &Lhs,
rhs: &Rhs,
config: Config,
) -> Result<(), String>
where
Lhs: Serialize,
Rhs: Serialize,
{
let lhs = serde_json::to_value(lhs).unwrap_or_else(|err| {
panic!(
"Couldn't convert left hand side value to JSON. Serde error: {}",
err
)
});
let rhs = serde_json::to_value(rhs).unwrap_or_else(|err| {
panic!(
"Couldn't convert right hand side value to JSON. Serde error: {}",
err
)
});
let diffs = diff(&lhs, &rhs, config);
if diffs.is_empty() {
Ok(())
} else {
let msg = diffs
.into_iter()
.map(|d| d.to_string())
.collect::<Vec<_>>()
.join("\n\n");
Err(msg)
}
}When I was trying to expose and return Difference<'_> instead of formatted string,
I realised it relies on the serde_json::Value created in the function, and the lifetime issue arose.
I got two ideas came up at the moment,
- Make
Differencean owned type => slow down the crate completely - Change the function signature so it take two
serde_json::Valueownership instead => it break the flexibility of the api - Current approach, introduce the owned types variants
DifferenceBuf,PathBufandKeyBuf, likestr/StringandPath/PathBuf
So I chose the 3rd one from them
All PR is welcome, so please share if you have a better idea! If you think this is a good way, please let me know, too π.
Metadata
Metadata
Assignees
Labels
No labels