cache_diff
Generate clean, human readable diffs between two cache structs
What
The CacheDiff trait provides a way to compare two structs and generate a list of differences between them.
When it returns an empty list, the two structs are identical.
You can manually implement the trait, or you can use the #[derive(CacheDiff)] macro to automatically generate the implementation.
Attributes are:
cache_diff(rename = "<new name>")Specify custom name for the fieldcache_diff(ignore)Ignores the given fieldcache_diff(display = <function>)Specify a function to call to display the field
Why
Cloud Native Buildpacks (CNBs) written in Rust using libcnb.rs use a serializable struct to represent the state of a cache. When that data changes, we need to invalidate the cache, but also report back to the user what changed.
Due to the CNB layer implementation, this struct is often called "metadata".
Install
$ cargo add cache_diff
For ANSI colored output, add the bullet_stream feature:
$ cargo add cache_diff --features bullet_stream
Derive usage
By default a #[derive(CacheDiff)] will generate a diff function that compares each field in the struct.
You can disable this dependency by specifying features = [].
use CacheDiff;
let diff = Metadata
.diff;
assert_eq!;
Struct fields must implement PartialEq and Display. Also note that PartialEq on the top level
cache struct is not used or required. If you want to customize equality logic, you can implement
the CacheDiff trait manually.
Ordering
The order of output will match the struct field definition from top to bottom:
use CacheDiff;
let now = Metadata ;
let diff = now.diff;
assert_eq!;
Rename attributes
If your field name is not descriptive enough, you can rename it:
use CacheDiff;
let now = Metadata ;
let diff = now.diff;
assert_eq!;
Ignore attributes
If the struct contains fields that should not be included in the diff comparison, you can ignore them:
use CacheDiff;
let now = Metadata ;
let diff = now.diff;
assert!;
Handle structs missing display
Not all structs implement the Display trait, for example std::path::PathBuf requires that you call display() on it.
The #[derive(CacheDiff)] macro will automatically handle std::path::PathBuf for you, however if you have a custom struct
that does not implement Display, you can specify a function to call instead:
use CacheDiff;
;
let now = Metadata ;
let diff = now.diff;
assert_eq!;