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

support serde values #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@

use diff::diff;
use serde::Serialize;
use serde_json::Value;

mod core_ext;
mod diff;
Expand Down Expand Up @@ -287,6 +288,21 @@ where
)
});

assert_json_value_matches_no_panic(&lhs, &rhs, config)
}


/// Compares two JSON values without panicking.
///
/// Instead it returns a `Result` where the error is the message that would be passed to `panic!`.
/// This is might be useful if you want to control how failures are reported and don't want to deal
/// with panics.
pub fn assert_json_value_matches_no_panic(
lhs: &Value,
rhs: &Value,
config: Config,
) -> Result<(), String>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if we could use something like Any::downcast_ref instead. Then we wouldn't have to add a whole new function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

I'm trying this:

pub fn assert_json_matches_no_panic<Lhs, Rhs>(
    lhs: &Lhs,
    rhs: &Rhs,
    config: Config,
) -> Result<(), String>
where
    Lhs: Serialize + Any,
    Rhs: Serialize + Any,

In that case, the lifetime of lhs and rhs is 'static. (https://doc.rust-lang.org/std/any/index.html)

Do you have another idea?

{
let diffs = diff(&lhs, &rhs, config);

if diffs.is_empty() {
Expand Down