-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented constructor argument capture shorthands
Closes #2
- Loading branch information
Showing
5 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
proc-macro-definitions/src/component_declaration/constructor_argument.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use quote::quote; | ||
use syn::{ | ||
parse::Parse, parse::ParseStream, Attribute, Error, FnArg, PatType, Result, Token, Visibility, | ||
}; | ||
|
||
pub struct ConstructorArgument { | ||
pub capture: Capture, | ||
pub fn_arg: PatType, | ||
} | ||
|
||
pub enum Capture { | ||
No, | ||
Yes(syn::Visibility), | ||
} | ||
|
||
impl Parse for ConstructorArgument { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
let attrs = input.call(Attribute::parse_outer)?; | ||
let capture = if input.peek(Token![priv]) { | ||
input.parse::<Token![priv]>().unwrap(); | ||
Capture::Yes(Visibility::Inherited) | ||
} else { | ||
match input.parse()? { | ||
Visibility::Inherited => Capture::No, | ||
visibility => Capture::Yes(visibility), | ||
} | ||
}; | ||
Ok(Self { | ||
fn_arg: match input.parse::<FnArg>()? { | ||
FnArg::Receiver(r) => { | ||
return Err(Error::new_spanned( | ||
r, | ||
"Component constructors cannot expect `self` parameters.", | ||
)); | ||
} | ||
FnArg::Typed(pat_type) | ||
if matches!(capture, Capture::No) || pat_type.attrs.is_empty() => | ||
{ | ||
PatType { attrs, ..pat_type } | ||
} | ||
FnArg::Typed(PatType { attrs, .. }) => { | ||
return Err(Error::new_spanned(quote!(#(#attrs)*), "Attributes are currently not available in this position. Place them before the visibility modifier instead.")); | ||
} | ||
}, | ||
capture, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters