Skip to content

Commit

Permalink
Merge pull request #5 from koenichiwa/develop
Browse files Browse the repository at this point in the history
Create v0.1.1
  • Loading branch information
koenichiwa committed Sep 13, 2023
2 parents 7ce2430 + 8c7d061 commit 09998c5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
- name: Check formatting
run: cargo fmt --check --verbose
- name: Run Clippy
run: cargo clippy --verbose
run: cargo clippy --verbose -- -D warnings
13 changes: 0 additions & 13 deletions const_typed_builder_derive/src/generator/generics_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,6 @@ impl<'a> GenericsGenerator<'a> {
res
}

fn add_const_generics_valued_to_impl(
&self,
tokens: &mut dyn Iterator<Item = TokenStream>,
) -> TokenStream {
let syn::Generics { params, .. } = self.target_generics;
if params.is_empty() {
quote!(<#(#tokens),*>)
} else {
let impl_generics = params.iter();
quote!(< #(#impl_generics),*, #(#tokens),* >)
}
}

fn add_const_generics_valued_for_type(
&self,
constants: &mut dyn Iterator<Item = Either<syn::Ident, syn::LitBool>>,
Expand Down
53 changes: 53 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@
/// .build();
/// ```
///
/// Or you can assume everything is mandatory altogether with `assume_mandatory` and `optional`.
///
/// ```
/// # use const_typed_builder::Builder;
/// #[derive(Debug, Builder)]
/// #[builder(assume_mandatory)]
/// pub struct Foo {
/// bar: Option<String>,
/// baz: Option<String>,
/// #[builder(optional)]
/// quz: Option<String>,
/// }
/// let foo = Foo::builder().bar("Hello world!".to_string()).baz("Hello world!".to_string()).build();
/// ```
///
/// ## 3. Grouping Fields
///
/// Fields can be grouped together, and constraints can be applied to these groups. Groups allow you to ensure that a certain combination of fields is provided together.
Expand Down Expand Up @@ -422,6 +437,44 @@ mod test {
assert_eq!(expected, foo);
}

#[test]
fn assume_mandatory() {
#[derive(Debug, Default, PartialEq, Eq, Builder)]
#[builder(assume_mandatory)]
pub struct Foo {
bar: Option<String>,
}

let expected = Foo {
bar: Some("Hello world!".to_string()),
};
let foo = Foo::builder().bar("Hello world!".to_string()).build();
assert_eq!(expected, foo);
}

#[test]
fn assume_mandatory_explicit_optional() {
#[derive(Debug, Default, PartialEq, Eq, Builder)]
#[builder(assume_mandatory)]
pub struct Foo {
bar: Option<String>,
baz: Option<String>,
#[builder(optional)]
quz: Option<String>,
}

let expected = Foo {
bar: Some("Hello world!".to_string()),
baz: Some("Hello world!".to_string()),
quz: None,
};
let foo = Foo::builder()
.bar("Hello world!".to_string())
.baz("Hello world!".to_string())
.build();
assert_eq!(expected, foo);
}

#[test]
fn group() {
#[derive(Debug, Default, PartialEq, Eq, Builder)]
Expand Down

0 comments on commit 09998c5

Please sign in to comment.