Skip to content

Commit 5007926

Browse files
committed
Cleanup and bump minor version
1 parent 3b16ab2 commit 5007926

File tree

10 files changed

+29
-98
lines changed

10 files changed

+29
-98
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased]
7+
## 0.2.0 - 2019-02-06
8+
### Changed
9+
- Upgraded `syn` version to support Rust 2018.
10+
- [**BREAKING**] Changed attribute style to `#[builder(...)]`:
11+
- `#[default]` -> `#[builder(default)]`
12+
- `#[default=...]` -> `#[builder(default=...)]`
13+
- [**BREAKING**] `default` no longer needs to be a string.
14+
- But you need to change your code anyways because the attribute style was changed.
815

916
## 0.1.1 - 2018-07-24
17+
### Fixed
1018
- Allow missing docs in structs that derive `TypedBuilder`.
1119

1220
## 0.1.0 - 2017-10-05

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "typed-builder"
33
description = "Compile-time type-checked builder derive"
4-
version = "0.1.1"
4+
version = "0.2.0"
55
authors = ["IdanArye <[email protected]>"]
66
license = "MIT"
77
repository = "https://github.com/idanarye/rust-typed-builder"

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ struct Foo {
1515
// Mandatory Field:
1616
x: i32,
1717

18-
// #[default] without parameter - use the type's default
19-
#[default]
18+
// #[builder(default)] without parameter - use the type's default
19+
#[builder(default)]
2020
y: Option<i32>,
2121

22-
// Or you can set the default(encoded as string)
23-
#[default="20"]
22+
// Or you can set the default
23+
#[builder(default=20)]
2424
z: i32,
2525
}
2626
```
@@ -48,7 +48,7 @@ Foo::builder().x(1).y(2).y(3); // y is specified twice
4848
* All setters are accepting `Into` values.
4949
* Compile time verification that all fields are set before calling `.build()`.
5050
* Compile time verification that no field is set more than once.
51-
* Ability to annotate fields with `#[default]` to make them optional and specify a default value when the user does not set them.
51+
* Ability to annotate fields with `#[builder(default)]` to make them optional and specify a default value when the user does not set them.
5252
* Generates simple documentation for the `.builder()` method.
5353

5454
# Limitations

examples/example.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#[macro_use]
21
extern crate typed_builder;
32

3+
use typed_builder::TypedBuilder;
4+
45
#[derive(PartialEq, TypedBuilder)]
56
struct Foo {
67
// Mandatory Field:

src/builder_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use syn::parse::Error;
44
use syn::spanned::Spanned;
55
use quote::quote;
66

7-
use util::{expr_to_single_string, path_to_single_string};
7+
use crate::util::{expr_to_single_string, path_to_single_string};
88

99
#[derive(Debug, Default)]
1010
pub struct BuilderAttr {

src/field_info.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use syn::spanned::Spanned;
44
use syn::parse::Error;
55
use quote::quote;
66

7-
use util::{make_identifier, map_only_one, path_to_single_string, ident_to_type};
8-
use builder_attr::BuilderAttr;
7+
use crate::util::{make_identifier, map_only_one, path_to_single_string, ident_to_type};
8+
use crate::builder_attr::BuilderAttr;
99

1010
#[derive(Debug)]
1111
pub struct FieldInfo<'a> {
@@ -47,7 +47,6 @@ impl<'a> FieldInfo<'a> {
4747
}
4848

4949
pub fn type_param(&self) -> syn::TypeParam {
50-
// syn::TypeParam::Type(self.generic_ident.clone().into())
5150
self.generic_ident.clone().into()
5251
}
5352

@@ -64,15 +63,4 @@ impl<'a> FieldInfo<'a> {
6463
elems: types,
6564
}.into()
6665
}
67-
68-
pub fn empty_type() -> syn::Type {
69-
syn::TypeTuple {
70-
paren_token: Default::default(),
71-
elems: Default::default(),
72-
}.into()
73-
}
74-
75-
pub fn empty_ty_param() -> syn::TypeParam {
76-
syn::TypeParam::from(syn::Ident::new("x", proc_macro2::Span::call_site()))
77-
}
7866
}

src/lib.rs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
//!
1010
//! Trying to set the same fields twice will generate a compile-time error. Trying to build without
1111
//! setting one of the fields will also generate a compile-time error - unless that field is marked
12-
//! as `#[default]`, in which case the `::default()` value of it's type will be picked. If you want
13-
//! to set a different default, use `#[default="..."]` - note that it has to be encoded in a
14-
//! string, so `1` is `#[default="1"]` and `"hello"` is `#[default="\"hello\""]`.
12+
//! as `#[builder(default)]`, in which case the `::default()` value of it's type will be picked. If
13+
//! you want to set a different default, use `#[builder(default=...)]`.
1514
//!
1615
//! # Examples
1716
//!
@@ -28,7 +27,7 @@
2827
//! #[builder(default)]
2928
//! y: Option<i32>,
3029
//!
31-
//! // Or you can set the default(encoded as string)
30+
//! // Or you can set the default
3231
//! #[builder(default=20)]
3332
//! z: i32,
3433
//! }
@@ -99,20 +98,11 @@ fn impl_my_derive(ast: &syn::DeriveInput) -> Result<TokenStream, Error> {
9998
syn::Fields::Named(fields) => {
10099
let struct_info = struct_info::StructInfo::new(&ast, fields.named.iter())?;
101100
let builder_creation = struct_info.builder_creation_impl()?;
102-
// println!("builder_creation\n==============\n{}\n==============", builder_creation);
103101
let conversion_helper = struct_info.conversion_helper_impl()?;
104-
// println!("conversion_helper\n==============\n{}\n==============", conversion_helper);
105102
let fields = struct_info.fields.iter().map(|f| struct_info.field_impl(f).unwrap());
106-
// for field in fields {
107-
// println!("field\n==============\n{}\n==============", quote!(#field));
108-
// }
109103
let fields = quote!(#(#fields)*);
110-
// println!("Fields be {}", fields);
111-
// println!("fields\n==============\n{}\n==============", fields);
112104
let build_method = struct_info.build_method_impl();
113105

114-
// eprintln!("===\n{}\n===", builder_creation);
115-
// println!("{}", builder_creation);
116106
quote!{
117107
#builder_creation
118108
#conversion_helper
@@ -126,45 +116,6 @@ fn impl_my_derive(ast: &syn::DeriveInput) -> Result<TokenStream, Error> {
126116
}
127117
syn::Data::Enum(_) => return Err(Error::new(ast.span(), "SmartBuilder is not supported for enums")),
128118
syn::Data::Union(_) => return Err(Error::new(ast.span(), "SmartBuilder is not supported for unions")),
129-
// syn::Data::Struct(syn::VariantData::Struct(ref body)) => {
130-
// let struct_info = struct_info::StructInfo::new(&ast, body);
131-
// let builder_creation = struct_info.builder_creation_impl();
132-
// let conversion_helper = struct_info.conversion_helper_impl();
133-
// let fields = struct_info.fields.iter().map(|f| struct_info.field_impl(f));
134-
// let build_method = struct_info.build_method_impl();
135-
// quote!{
136-
// #builder_creation
137-
// #conversion_helper
138-
// #( #fields )*
139-
// #build_method
140-
// }
141-
// },
142-
// syn::Data::Struct(syn::VariantData::Unit) => panic!("SmartBuilder is not supported for unit types"),
143-
// syn::Data::Struct(syn::VariantData::Tuple(_)) => panic!("SmartBuilder is not supported for tuples"),
144-
// syn::Data::Enum(_) => panic!("SmartBuilder is not supported for enums"),
145119
};
146-
// println!("{}", data);
147120
Ok(data)
148-
// Ok(quote!())
149121
}
150-
// fn impl_my_derive(ast: &syn::DeriveInput) -> TokenStream {
151-
152-
// match ast.body {
153-
// syn::Body::Struct(syn::VariantData::Struct(ref body)) => {
154-
// let struct_info = struct_info::StructInfo::new(&ast, body);
155-
// let builder_creation = struct_info.builder_creation_impl();
156-
// let conversion_helper = struct_info.conversion_helper_impl();
157-
// let fields = struct_info.fields.iter().map(|f| struct_info.field_impl(f));
158-
// let build_method = struct_info.build_method_impl();
159-
// quote!{
160-
// #builder_creation
161-
// #conversion_helper
162-
// #( #fields )*
163-
// #build_method
164-
// }
165-
// },
166-
// syn::Body::Struct(syn::VariantData::Unit) => panic!("SmartBuilder is not supported for unit types"),
167-
// syn::Body::Struct(syn::VariantData::Tuple(_)) => panic!("SmartBuilder is not supported for tuples"),
168-
// syn::Body::Enum(_) => panic!("SmartBuilder is not supported for enums"),
169-
// }
170-
// }

src/struct_info.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use syn::parse::Error;
55
use syn::spanned::Spanned;
66
use quote::{quote, ToTokens};
77

8-
use field_info::FieldInfo;
9-
use util::{make_identifier, empty_type, make_punctuated_single, modify_types_generics_hack};
8+
use crate::field_info::FieldInfo;
9+
use crate::util::{make_identifier, empty_type, make_punctuated_single, modify_types_generics_hack};
1010

1111
#[derive(Debug)]
1212
pub struct StructInfo<'a> {
@@ -41,18 +41,15 @@ impl<'a> StructInfo<'a> {
4141

4242
pub fn builder_creation_impl(&self) -> Result<TokenStream, Error> {
4343
let modif = self.modify_generics(|g| g.params.push(self.fields[0].generic_ty_param()));
44-
// println!("modif {:?}", modif);
4544
let init_empties = {
4645
let names = self.fields.iter().map(|f| f.name);
4746
quote!(#( #names: () ),*)
4847
};
49-
// println!("empties {}", init_empties);
5048
let builder_generics = {
5149
let names = self.fields.iter().map(|f| f.name);
5250
let generic_idents = self.fields.iter().map(|f| &f.generic_ident);
5351
quote!(#( #names: #generic_idents ),*)
5452
};
55-
// println!("builder_generics {}", builder_generics);
5653
let StructInfo { ref vis, ref name, ref builder_name, .. } = *self;
5754
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
5855
let b_generics = self.modify_generics(|g| {
@@ -62,17 +59,9 @@ impl<'a> StructInfo<'a> {
6259
});
6360
let generics_with_empty = modify_types_generics_hack(&ty_generics, |args| {
6461
for _ in self.fields.iter() {
65-
args.push(syn::GenericArgument::Type(FieldInfo::empty_type()));
62+
args.push(syn::GenericArgument::Type(empty_type()));
6663
}
6764
});
68-
// println!("b_generics {:?}", b_generics);
69-
// let generics_with_empty = self.modify_generics(|g| {
70-
// for _ in self.fields.iter() {
71-
// g.params.push(FieldInfo::empty_ty_param().into());
72-
// }
73-
// });
74-
// let (_, generics_with_empty, _) = generics_with_empty.split_for_impl();
75-
// println!("generics_with_empty {:?}", generics_with_empty);
7665
let phantom_generics = self.generics.params.iter().map(|param| {
7766
match param {
7867
syn::GenericParam::Lifetime(lifetime) => quote!(&#lifetime ()),
@@ -85,12 +74,6 @@ impl<'a> StructInfo<'a> {
8574
quote!(#cnst)
8675
},
8776
}
88-
// let lifetimes = self.generics.lifetimes().map(|l| &l.lifetime);
89-
// let types = self.generics.params.clone();//.iter().map(|t| &t.ident);
90-
// quote!{
91-
// #( ::std::marker::PhantomData<&#lifetimes ()>, )*
92-
// #( ::std::marker::PhantomData<#types>, )*
93-
// }
9477
});
9578
let doc = self.builder_doc();
9679
Ok(quote! {
@@ -233,7 +216,6 @@ impl<'a> StructInfo<'a> {
233216
ident: self.conversion_helper_trait_name.clone(),
234217
arguments: syn::PathArguments::AngleBracketed(
235218
syn::AngleBracketedGenericArguments{
236-
// colon2_token: Some(Default::default()),
237219
colon2_token: None,
238220
lt_token: Default::default(),
239221
args: make_punctuated_single(syn::GenericArgument::Type(field.ty.clone())),

src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn make_identifier(kind: &str, name: &syn::Ident) -> syn::Ident {
88
syn::Ident::new(&format!("TypedBuilder_{}_{}", kind, name), proc_macro2::Span::call_site())
99
}
1010

11-
// Panic if there is more than one.
11+
// Returns error if there is more than one.
1212
pub fn map_only_one<S, T, F>(iter: &[S], dlg: F) -> Result<Option<T>, Error>
1313
where F: Fn(&S) -> Result<Option<T>, Error>,
1414
S: Spanned,

tests/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#[macro_use]
21
extern crate typed_builder;
32

3+
use typed_builder::TypedBuilder;
4+
45
#[test]
56
fn test_simple() {
67
#[derive(PartialEq, TypedBuilder)]

0 commit comments

Comments
 (0)