Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros one dot one (Do Not Merge) #28

Merged
merged 6 commits into from
Jan 19, 2017
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
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
sudo: false
language: rust
rust:
- stable
- beta
- nightly
env:
global:
Expand All @@ -27,8 +25,7 @@ before_script: |
export PATH=$LOCAL/bin:$PATH
script: |
travis-cargo build &&
travis-cargo test &&
travis-cargo bench &&
(cd derive-builder-test && travis-cargo test) &&
travis-cargo doc
after_success: |
# upload the documentation from the build with stable (automatically only actually
Expand Down
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ version = "0.2.1"
authors = ["Colin Kiegel <[email protected]>",
"Pascal Hertleif <[email protected]>"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this gets merged you should be listed here, @badboy!


description = "Rust macro based on custom_derive to automatically implement the builder pattern for arbitrary structs."
description = "Rust macro to automatically implement the builder pattern for arbitrary structs."
repository = "https://github.com/colin-kiegel/rust-derive-builder"
documentation = "https://docs.rs/derive_builder"

license = "MIT/Apache-2.0"
keywords = ["derive", "macro", "builder", "setter", "struct"]
readme = "README.md"

[dev-dependencies]
custom_derive = "0.1.5"
[lib]
proc-macro = true

[dependencies]
syn = "0.8"
quote = "0.2"
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@

# Builder pattern derive

[Rust][rust] macro (based on [custom_derive][custom_derive]) to automatically implement the **builder pattern** for arbitrary structs. A simple `#[derive(Builder)]` will generate code of public setter-methods for all struct fields.
[Rust][rust] macro to automatically implement the **builder pattern** for arbitrary structs. A simple `#[derive(Builder)]` will generate code of public setter-methods for all struct fields.

**This is a work in progress.** Use it at your own risk.
**This is a work in progress.** Use it at your own risk.
**This currently requires Rust nightly, due to the usage of Macros 1.1**
Copy link
Collaborator

@killercup killercup Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mention which nightly you think works; something like "requires Rust nightly (2016-10-09 or newer)" is good. Also: Period at the end.


And this is how it works:

```rust
#[macro_use] extern crate custom_derive;
#![feature(proc_macro)]
#[macro_use] extern crate derive_builder;

custom_derive! {
#[derive(Default, Builder)]
struct Channel {
token: i32,
special_info: i32,
// .. a whole bunch of other fields ..
}
#[derive(Default, Builder)]
struct Channel {
token: i32,
special_info: i32,
// .. a whole bunch of other fields ..
}

impl Channel {
Expand All @@ -38,7 +37,7 @@ fn main() {
}
```

Note that we did not write any implementation of a method called `special_info`. Instead the [`custom_derive!`][custom_derive] macro scans the `#[derive(..)]` attribute of the struct for non-std identifiers – in our case `#[derive(Builder)]` and delegates the code generation to the `Builder!` macro defined in this crate.
Note that we did not write any implementation of a method called `special_info`. Instead the `derive_builder` crate acts on a `#[derive(Builder)]` and generates the necessary code at compile time.

The automatically generated setter method for the `special_info` field will look like this:

Expand Down Expand Up @@ -70,7 +69,6 @@ The builder pattern is explained [here][builder-pattern], including its variants

[doc]: https://colin-kiegel.github.io/rust-derive-builder
[rust]: https://www.rust-lang.org/
[custom_derive]: https://crates.io/crates/custom_derive
[builder-pattern]: https://aturon.github.io/ownership/builders.html
[into]: https://doc.rust-lang.org/nightly/std/convert/trait.Into.html
Copy link
Collaborator

@killercup killercup Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was fun while it lasted 😢


Expand Down
8 changes: 8 additions & 0 deletions derive-builder-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "derive_builder_test"
version = "0.1.0"

description = "Test derive_builder"

[dependencies]
derive_builder = { path = "../" }
26 changes: 26 additions & 0 deletions derive-builder-test/tests/attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![feature(proc_macro)]
#[macro_use] extern crate derive_builder;

/// This is a doc comment for the struct
#[warn(missing_docs)]
#[allow(non_snake_case, dead_code)]
#[derive(Builder)]
struct Lorem {
/// This is a doc comment for a field
field_with_doc_comment: String,
#[allow(missing_docs)]
undocumented: String,
#[allow(non_snake_case)]
CamelCase: i32,
#[cfg(target_os = "macos")]
mac_only: bool,
#[allow(non_snake_case)]
#[cfg(target_os = "linux")]
LinuxOnly: (),
}

#[test]
fn annotations() {
// this is currently just a compile-test (may switch to token comparisons here)
// https://github.com/colin-kiegel/rust-derive-builder/issues/19
Copy link
Collaborator

@killercup killercup Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still valid? Is there a test story for nicely comparing syn tokens?

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#[macro_use] extern crate custom_derive;
#![feature(proc_macro)]
#[macro_use] extern crate derive_builder;

custom_derive!{
#[derive(Debug, PartialEq, Default, Builder, Clone)]
struct Lorem {
ipsum: String,
pub dolor: Option<String>,
pub sit: i32,
amet: bool,
}
#[derive(Debug, PartialEq, Default, Builder, Clone)]
struct Lorem {
ipsum: String,
pub dolor: Option<String>,
pub sit: i32,
amet: bool,
}

impl Lorem {
Expand Down
13 changes: 13 additions & 0 deletions derive-builder-test/tests/empty_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(proc_macro)]
#[macro_use] extern crate derive_builder;

/// This is a doc comment for the struct
#[warn(missing_docs)]
#[allow(non_snake_case)]
#[derive(Debug, PartialEq, Default, Builder)]
struct IgnoreEmptyStruct { }

#[test]
fn empty_struct() {
// this is just a compile-test - no run time checks required.
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#[macro_use] extern crate custom_derive;
#![feature(proc_macro)]
#[macro_use] extern crate derive_builder;

custom_derive!{
#[derive(Debug, PartialEq, Default, Builder, Clone)]
struct GenLorem<T> {
ipsum: String,
pub dolor: T, // generics are a pain, so this field name is fitting
}
#[derive(Debug, PartialEq, Default, Builder, Clone)]
struct GenLorem<T> {
ipsum: String,
pub dolor: T, // generics are a pain, so this field name is fitting
}

impl<T: Default> GenLorem<T> {
Expand Down
14 changes: 6 additions & 8 deletions examples/channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[macro_use] extern crate custom_derive;
#![feature(proc_macro)]
#[macro_use] extern crate derive_builder;

use std::convert::From;
Expand All @@ -20,13 +20,11 @@ impl From<i32> for Authentication {
}
}

custom_derive!{
#[derive(Debug, Default, Builder)]
struct Channel {
id: Uuid,
token: Authentication,
special_info: i32
}
#[derive(Debug, Default, Builder)]
struct Channel {
id: Uuid,
token: Authentication,
special_info: i32
}

fn main() {
Expand Down
151 changes: 0 additions & 151 deletions src/derive_builder.rs

This file was deleted.

Loading