Skip to content
This repository was archived by the owner on Jun 8, 2025. It is now read-only.

RFC: Better solution than introducing owned types?Β #4

@B-2U

Description

@B-2U

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,

  1. Make Difference an owned type => slow down the crate completely
  2. Change the function signature so it take two serde_json::Value ownership instead => it break the flexibility of the api
  3. Current approach, introduce the owned types variants DifferenceBuf, PathBuf and KeyBuf, like str/String and Path/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
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions