assert-struct
Ergonomic structural assertions for Rust tests with helpful error messages.
What is assert-struct?
assert-struct
is a procedural macro that enables clean, readable assertions for complex data structures. Instead of verbose field-by-field comparisons, you can assert on nested structures with clear, maintainable syntax. When assertions fail, it provides precise error messages showing exactly what went wrong and where.
Why use assert-struct?
The Problem: Testing complex data structures in Rust is tedious and verbose:
// Verbose and hard to maintain
assert_eq!;
assert!;
assert_eq!;
The Solution: Clean, structural assertions:
assert_struct!;
When to use assert-struct?
- API Response Testing - Validate JSON deserialization results
- Database Query Testing - Check returned records match expectations
- Complex State Validation - Assert on deeply nested application state
- Partial Data Matching - Focus on relevant fields, ignore the rest
Key Features
- Partial matching with
..
- check only the fields you care about - Deep nesting - assert on nested structs without field access chains
- Advanced matchers - comparisons (
> 18
), ranges (0..100
), regex (=~ r"pattern"
) - Method calls -
field.len(): 5
,field.is_some(): true
- Collections - element-wise patterns for
Vec
fields - Enums & tuples - full support for
Option
,Result
, and custom types - Smart pointers - dereference
Box<T>
,Rc<T>
,Arc<T>
with*field
- Helpful errors - precise error messages with field paths and context
Quick Start
Add to your Cargo.toml
:
[]
= "0.1"
Basic usage:
use assert_struct;
let user = User ;
// Exact match
assert_struct!;
// Partial match with comparisons
assert_struct!;
Examples
Common patterns:
// Method calls
assert_struct!;
// Nested field access
assert_struct!;
// Collections
assert_struct!;
// Enums and Options
assert_struct!;
Documentation
- API Documentation - Complete API reference with examples
- Examples Directory - Real-world usage examples
- Getting Started Guide - See the main crate documentation
License
This project is licensed under the MIT License - see the LICENSE file for details.