-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(macros): add support for macro register (#2)
- Loading branch information
Showing
16 changed files
with
242 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"aionbot", | ||
"chronos", | ||
"Deque", | ||
"Hasher", | ||
"onebot", | ||
"serde" | ||
] | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
use anyhow::Result; | ||
|
||
pub extern crate aionbot_core; | ||
|
||
pub trait Adapter { | ||
fn reply(&self, message: &str) -> impl std::future::Future<Output = Result<()>> + Send; | ||
} | ||
|
||
impl Adapter for aionbot_core::event::Event { | ||
async fn reply(&self, message: &str) -> Result<()> { | ||
let _ = message; | ||
// let ws = onebot_v11::connect::ws_reverse::ReverseWsConnect::new(config); | ||
unimplemented!() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
pub mod core; | ||
pub mod entry; | ||
pub mod event; | ||
pub mod handler; | ||
pub mod prelude; | ||
pub mod queue; | ||
pub mod router; | ||
pub mod types; |
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,4 @@ | ||
pub use crate::entry::Entry; | ||
pub use crate::event::Event; | ||
pub use crate::router::*; | ||
pub use crate::types::*; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use futures::future::BoxFuture; | ||
|
||
use crate::event::Event; | ||
|
||
pub type Callback = fn(&Event) -> BoxFuture<'static, Result<String>>; | ||
pub type HandlerCallback = BoxFuture<'static, Result<()>>; | ||
pub type Callback = fn(Arc<Event>) -> HandlerCallback; |
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,12 @@ | ||
[package] | ||
name = "aionbot-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.86" | ||
quote = "1.0.37" | ||
syn = { version = "2.0.77", features = ["full"] } | ||
|
||
[lib] | ||
proc-macro = true |
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,105 @@ | ||
use std::hash::{DefaultHasher, Hash, Hasher}; | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{meta::ParseNestedMeta, parse_macro_input, Result}; | ||
|
||
struct HandlerArgs { | ||
priority: syn::LitInt, | ||
router: Option<syn::Expr>, | ||
} | ||
|
||
impl Default for HandlerArgs { | ||
fn default() -> Self { | ||
Self { | ||
priority: syn::LitInt::new("0", proc_macro2::Span::call_site()), | ||
router: None, | ||
} | ||
} | ||
} | ||
|
||
impl HandlerArgs { | ||
fn parse(&mut self, meta: ParseNestedMeta) -> Result<()> { | ||
if let Some(ident) = meta.path.get_ident() { | ||
match ident.to_string().as_str() { | ||
"router" => { | ||
self.router = Some(meta.value()?.parse()?); | ||
Ok(()) | ||
} | ||
"priority" => { | ||
self.priority = meta.value()?.parse()?; | ||
Ok(()) | ||
} | ||
_ => Err(meta.error("msg")), | ||
} | ||
} else { | ||
Err(meta.error("msg")) | ||
} | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.router.is_none() | ||
} | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn register(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(item as syn::ItemFn); | ||
|
||
let mut attrs = HandlerArgs::default(); | ||
let parser = syn::meta::parser(|meta| attrs.parse(meta)); | ||
parse_macro_input!(attr with parser); | ||
|
||
let vis = &input.vis; | ||
match vis { | ||
syn::Visibility::Public(_) => {} | ||
_ => { | ||
return TokenStream::from( | ||
quote! { compile_error!("Only public functions can be registered"); }, | ||
) | ||
} | ||
} | ||
|
||
let origin_ident = &input.sig.ident; | ||
let mut fn_name = input.sig.ident.to_string(); | ||
let mut hasher = DefaultHasher::new(); | ||
fn_name.hash(&mut hasher); | ||
let hash_id = hasher.finish().to_string(); | ||
fn_name.extend("_".chars().chain(hash_id.chars())); | ||
|
||
let fn_name_ident = syn::Ident::new(&fn_name, input.sig.ident.span()); | ||
|
||
let fn_args = &input.sig.inputs; | ||
let fn_body = &input.block; | ||
|
||
let default_router = quote! { "default" }.into(); | ||
let default_router = parse_macro_input!(default_router as syn::Expr); | ||
let router = attrs.router.as_ref().unwrap_or(&default_router); | ||
|
||
if attrs.is_empty() { | ||
return TokenStream::from( | ||
quote! { compile_error!("Missing `#[register(router = \"...\")]` attribute"); }, | ||
); | ||
}; | ||
|
||
let expanded = quote! { | ||
use std::sync::*; | ||
use std::cell::*; | ||
use aionbot_core::prelude::*; | ||
|
||
pub fn #fn_name_ident(#fn_args) -> HandlerCallback { | ||
Box::pin(async move { #fn_body }) | ||
} | ||
|
||
pub fn #origin_ident() -> Entry { | ||
Entry { | ||
id: #hash_id, | ||
priority: 0, | ||
router: Arc::new(Box::new(#router)), | ||
callback: Arc::new(#fn_name_ident), | ||
} | ||
} | ||
}; | ||
|
||
TokenStream::from(expanded) | ||
} |
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,10 @@ | ||
[package] | ||
name = "aionbot" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
aionbot-adapter-onebot = { version = "0.1.0", path = "../aionbot-adapter-onebot" } | ||
aionbot-core = { version = "0.1.0", path = "../aionbot-core" } | ||
aionbot-macros = { version = "0.1.0", path = "../aionbot-macros" } | ||
tokio = { version = "1.40.0", features = ["full"] } |
Oops, something went wrong.