Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ jobs:
command: test
args: --all-features

test_backends:
name: TestBackends
runs-on: ${{ matrix.config.os }}
strategy:
fail-fast: false
matrix:
config:
- { os: ubuntu-latest, target: 'x86_64-unknown-linux-gnu' }
- { os: macos-latest, target: 'x86_64-apple-darwin' }
backends: ["syn", ""]

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.config.target }}
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --features "debug_asserts, rustc_hash, generated_setters, debug_diffs, serde, nanoserde, ${{ matrix.backends.target }}"

test_serde:
name: TestSerde
runs-on: ${{ matrix.config.os }}
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ structdiff-derive = { path = "derive", version = "=0.7.4" }
"default" = []
"nanoserde" = ["dep:nanoserde", "structdiff-derive/nanoserde"]
"serde" = ["dep:serde", "structdiff-derive/serde"]
"syn" = ["structdiff-derive/syn"]
"debug_diffs" = ["structdiff-derive/debug_diffs"]
"generated_setters" = ["structdiff-derive/generated_setters"]
"rustc_hash" = ["dep:rustc-hash"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ For more examples take a look at [integration tests](/tests)
- `debug_diffs` - Derive `Debug` on the generated diff type
- `generated_setters` - Enable generation of setters for struct fields. These setters automatically return a diff if a field's value is changed by the assignment.
- `rustc_hash` - Use the (non-cryptographic) hash implementation from the `rustc-hash` crate instead of the default hasher. Much faster diff generation for collections at the cost of a dependency.
- `syn` - Use a `syn`-based backend for the `Difference` derive macro. This adds the `syn`, `quote`, and `proc-macro2` dependencies, but supports Rust syntax through `syn`'s parser instead of structdiff's zero-dependency derive parser.

### Development status
This is being used actively for my own projects, although it's mostly working now. PRs will be accepted for either more tests or functionality.
4 changes: 4 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ proc-macro = true

[dependencies]
nanoserde = { version = "^0.1.37", optional = true }
proc-macro2 = { version = "1.0", optional = true }
quote = { version = "1.0", optional = true }
serde = { version = "^1.0.0", optional = true, features = ["derive"] }
syn = { version = "2.0", optional = true, features = ["derive", "full", "parsing"] }

[features]
"default" = []
"nanoserde" = ["dep:nanoserde"]
"serde" = ["dep:serde"]
"syn" = ["dep:proc-macro2", "dep:quote", "dep:syn"]
"debug_diffs" = []
"generated_setters" = []

Expand Down
22 changes: 18 additions & 4 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,36 @@ extern crate proc_macro;
mod shared;

mod difference;
#[cfg(feature = "syn")]
mod syn_backend;

#[cfg(not(feature = "syn"))]
use difference::derive_struct_diff_enum;

#[cfg(not(feature = "syn"))]
use crate::difference::derive_struct_diff_struct;

#[cfg_attr(feature = "syn", allow(dead_code))]
mod parse;

/// Derive macro generating an impl of the trait `StructDiff`
#[proc_macro_derive(Difference, attributes(difference))]
pub fn derive_struct_diff(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
derive_struct_diff_impl(input)
}

#[cfg(feature = "syn")]
fn derive_struct_diff_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
syn_backend::derive_struct_diff(input)
}

#[cfg(not(feature = "syn"))]
fn derive_struct_diff_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);

let ts = match &input {
match &input {
parse::Data::Struct(struct_) if struct_.named => derive_struct_diff_struct(struct_),
parse::Data::Enum(enum_) => derive_struct_diff_enum(enum_),
_ => unimplemented!("Only structs and enums are supported"),
};

ts
}
}
Loading
Loading