diff options
| author | Max Audron <audron@cocaine.farm> | 2021-06-23 19:09:41 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-06-23 19:10:24 +0200 |
| commit | c34afc3f443d9a0744a45c988cd2ab8d93ee4160 (patch) | |
| tree | b203e12ad7cea74dd53f80ab24118a5acb1bb0fb | |
| parent | publish macros subcrate (diff) | |
add ability to use path and dotted syntax for functions
Diffstat (limited to '')
| -rw-r--r-- | macros/Cargo.toml | 2 | ||||
| -rw-r--r-- | macros/src/macro_types/mod.rs | 45 |
2 files changed, 39 insertions, 8 deletions
diff --git a/macros/Cargo.toml b/macros/Cargo.toml index c4e57a2..b4e8639 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "catinator_macros" -version = "0.1.0" +version = "0.2.0" edition = "2018" [lib] diff --git a/macros/src/macro_types/mod.rs b/macros/src/macro_types/mod.rs index 0e55dc4..ad4c8f0 100644 --- a/macros/src/macro_types/mod.rs +++ b/macros/src/macro_types/mod.rs @@ -1,12 +1,12 @@ use proc_macro2::{Ident, Span}; -use quote::quote; +use quote::{quote, ToTokens}; use syn::{ parenthesized, parse::{Parse, ParseStream}, - Path, + punctuated::Punctuated, + LitStr, Path, Token, }; -use syn::{LitStr, Token}; pub mod privmsg; @@ -63,7 +63,7 @@ impl Parse for Item { pub struct Command { pub name: LitStr, pub description: LitStr, - pub function: Path, + pub function: Function, } impl IrcItem for Command { @@ -113,7 +113,7 @@ pub struct Hook { pub name: LitStr, pub description: LitStr, pub kind: Ident, - pub function: Path, + pub function: Function, } impl IrcItem for Hook { @@ -194,7 +194,7 @@ pub struct Matcher { pub name: LitStr, pub description: LitStr, pub matcher: LitStr, - pub function: Path, + pub function: Function, } impl IrcItem for Matcher { @@ -213,7 +213,12 @@ impl IrcItem for Matcher { } fn help(&self) -> String { - format!(" {} ({}): {}", self.name.value(), self.matcher.value(), self.description.value()) + format!( + " {} ({}): {}", + self.name.value(), + self.matcher.value(), + self.description.value() + ) } } @@ -244,3 +249,29 @@ impl Parse for Matcher { }) } } + +pub enum Function { + Path(Path), + Expr(Punctuated<Ident, Token![.]>), +} + +impl ToTokens for Function { + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { + match self { + Function::Path(v) => *tokens = v.into_token_stream(), + Function::Expr(v) => *tokens = v.into_token_stream(), + } + } +} + +impl Parse for Function { + fn parse(input: ParseStream) -> syn::Result<Self> { + if input.peek2(Token![::]) { + Ok(Function::Path(input.parse()?)) + } else if input.peek2(Token![.]) { + Ok(Function::Expr(input.parse_terminated(Ident::parse)?)) + } else { + Err(input.error("did not find path or dotted")) + } + } +} |
