Writing a tree matcher

I have some code that produces a tree data structure with many different nested node types (i.e. nodes on level 1 are of a different type from nodes on level 2). I would like to write tests for this code; most of the data in the tree must match exactly, but I have several data that I don’t care about most of the time. For example, given this type:

enum Node<S> {
    A(Vec<i32>, S),
    B(Vec<(i32, S)>),
}

I would like to test that the return value of some function is Node::B(vec![(0, _), (32, "something")]), meaning the enum variant must be Node::B, the nested vector must have size 2, its first element is a tuple whose first element is 0 (but I don’t care about the second one) and its second element is a tuple whose first element is 32 and whose second element equals "something".
I thought about creating a type like this:

enum Matcher<S> {
    Exact(S),
    Wildcard,
}

and implementing PartialEq<Matcher<S>> for S where S: PartialEq and then comparing a Node<S> with a Node<Matcher<S>>, but this is where the plethora of types in my tree begins to cause problems: I would have to implement something analogue to impl<S, T: PartialEq<S>> PartialEq<Node<S>> for Node<T> for every type, and it’s really cumbersome. Is there some crate that already does that?

The insta library (in particular, redactions feature), might be useful here: https://github.com/mitsuhiko/insta/#redactions

1 Like

Thanks for the recommendation! I was hoping for a more lightweight solution though; I’d rather avoid pulling in serialisation and other heavy machinery as long as my needs are fairly simple.