| Crates.io | assert-struct |
| lib.rs | assert-struct |
| version | 0.2.0 |
| created_at | 2025-08-21 19:42:58.028887+00 |
| updated_at | 2025-08-23 02:36:40.606635+00 |
| description | A procedural macro for ergonomic structural assertions in tests |
| homepage | https://github.com/carllerche/assert-struct |
| repository | https://github.com/carllerche/assert-struct |
| max_upload_size | |
| id | 1805257 |
| size | 100,203 |
Ergonomic structural assertions for Rust tests with helpful error messages.
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.
The Problem: Testing complex data structures in Rust is tedious and verbose:
// Verbose and hard to maintain
assert_eq!(response.user.profile.age, 25);
assert!(response.user.profile.verified);
assert_eq!(response.status.code, 200);
The Solution: Clean, structural assertions:
assert_struct!(response, Response {
user: User {
profile: Profile {
age: 25,
verified: true,
..
},
..
},
status: Status { code: 200 },
});
.. - check only the fields you care about#{ "key": pattern } for HashMap, BTreeMap, and custom map types> 18), ranges (0..100), regex (=~ r"pattern")field.len(): 5, field.is_some(): trueVec fieldsOption, Result, and custom typesBox<T>, Rc<T>, Arc<T> with *fieldAdd to your Cargo.toml:
[dev-dependencies]
assert-struct = "0.2"
Basic usage:
use assert_struct::assert_struct;
#[derive(Debug)]
struct User {
name: String,
age: u32,
active: bool,
}
let user = User {
name: "Alice".to_string(),
age: 30,
active: true,
};
// Exact match
assert_struct!(user, User {
name: "Alice",
age: 30,
active: true,
});
// Partial match with comparisons
assert_struct!(user, User {
name: "Alice",
age: >= 18, // Adult check
.. // Ignore other fields
});
Common patterns:
// Method calls
assert_struct!(data, Data {
items.len(): > 0,
text.contains("hello"): true,
..
});
// Nested field access
assert_struct!(company, Company {
info.name: "TechCorp",
info.address.city: "San Francisco",
info.address.zip: > 90000,
..
});
// Collections
assert_struct!(response, Response {
scores: [> 80.0, >= 90.0, < 100.0],
..
});
// Map patterns (HashMap, BTreeMap, custom maps)
assert_struct!(api_response, ApiResponse {
headers: #{
"content-type": "application/json",
"status": == 200,
.. // Ignore other headers
},
metadata: #{}, // Exactly empty map
..
});
// Enums and Options
assert_struct!(result, Result {
user_data: Some(User {
age: >= 18,
verified: true,
..
}),
..
});
This project is licensed under the MIT License - see the LICENSE file for details.