Skip to content

Commit

Permalink
Move BuilderFieldType out from in the middle of BuilderField
Browse files Browse the repository at this point in the history
  • Loading branch information
ijackson committed Apr 14, 2022
1 parent a8b3285 commit 59e8e14
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions derive_builder_core/src/builder_field.rs
Expand Up @@ -39,37 +39,6 @@ pub struct BuilderField<'a> {
pub attrs: &'a [syn::Attribute],
}

/// The type of a field in the builder struct
#[derive(Debug, Clone)]
pub enum BuilderFieldType<'a> {
/// The corresonding builder field will be `Option<field_type>`.
Optional(&'a syn::Type),
/// The corresponding builder field will be just this type
Precise(&'a syn::Type),
/// The corresponding builder field will be a PhantomData
///
/// We do this if if the field is disabled
/// to hack around issues with unused generic type parameters - at
/// least for now.
Phantom(&'a syn::Type),
}

impl<'a> BuilderFieldType<'a> {
/// Obtain type information for the builder field setter
///
/// Return value:
/// * `.0`: type of the argument to the setter function
/// (before application of `strip_option`, `generic_into`)
/// * `.1`: whether the builder field is `Option<type>` rather than just `type`
pub fn setter_type_info(&'a self) -> (&'a syn::Type, bool) {
match self {
BuilderFieldType::Optional(ty) => (ty, true),
BuilderFieldType::Precise(ty) => (ty, false),
BuilderFieldType::Phantom(_ty) => panic!("phantom fields should never have setters"),
}
}
}

impl<'a> ToTokens for BuilderFieldType<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Expand Down Expand Up @@ -121,6 +90,37 @@ macro_rules! default_builder_field {
}};
}

/// The type of a field in the builder struct
#[derive(Debug, Clone)]
pub enum BuilderFieldType<'a> {
/// The corresonding builder field will be `Option<field_type>`.
Optional(&'a syn::Type),
/// The corresponding builder field will be just this type
Precise(&'a syn::Type),
/// The corresponding builder field will be a PhantomData
///
/// We do this if if the field is disabled
/// to hack around issues with unused generic type parameters - at
/// least for now.
Phantom(&'a syn::Type),
}

impl<'a> BuilderFieldType<'a> {
/// Obtain type information for the builder field setter
///
/// Return value:
/// * `.0`: type of the argument to the setter function
/// (before application of `strip_option`, `generic_into`)
/// * `.1`: whether the builder field is `Option<type>` rather than just `type`
pub fn setter_type_info(&'a self) -> (&'a syn::Type, bool) {
match self {
BuilderFieldType::Optional(ty) => (ty, true),
BuilderFieldType::Precise(ty) => (ty, false),
BuilderFieldType::Phantom(_ty) => panic!("phantom fields should never have setters"),
}
}
}

#[cfg(test)]
mod tests {
#[allow(unused_imports)]
Expand Down

0 comments on commit 59e8e14

Please sign in to comment.