feat: use derive
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-10-27 12:05:30 +01:00
parent c94f6b72b8
commit 892fdc9420
9 changed files with 161 additions and 4 deletions

View File

@@ -0,0 +1,16 @@
[package]
name = "cuddle-actions-sdk-derive"
edition = "2021"
version.workspace = true
[lib]
proc-macro = true
[dependencies]
anyhow.workspace = true
darling = "0.20.10"
proc-macro-crate = "3.2.0"
proc-macro2 = "1.0.89"
quote = "1.0.37"
syn = { version = "2.0.85", features = ["extra-traits", "visit", "visit-mut"] }

View File

@@ -0,0 +1,58 @@
use darling::FromMeta;
use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemImpl};
extern crate proc_macro;
mod args {
use darling::FromMeta;
#[derive(FromMeta, Default)]
#[darling(default)]
pub struct Object {}
}
mod actions {
use proc_macro::TokenStream;
use syn::ItemImpl;
use crate::{args, utils::GeneratorResult};
pub fn generate(
object_args: &args::Object,
item_impl: &mut ItemImpl,
) -> GeneratorResult<TokenStream> {
todo!()
}
}
mod utils {
pub type GeneratorResult<T> = std::result::Result<T, syn::Error>;
}
macro_rules! parse_nested_meta {
($ty:ty, $args:expr) => {{
let meta = match darling::ast::NestedMeta::parse_meta_list(proc_macro2::TokenStream::from(
$args,
)) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(darling::Error::from(e).write_errors());
}
};
match <$ty>::from_list(&meta) {
Ok(object_args) => object_args,
Err(err) => return TokenStream::from(err.write_errors()),
}
}};
}
#[proc_macro_attribute]
#[allow(non_snake_case)]
pub fn Actions(args: TokenStream, input: TokenStream) -> TokenStream {
let object_args = parse_nested_meta!(args::Object, args);
let mut item_impl = parse_macro_input!(input as ItemImpl);
match actions::generate(&object_args, &mut item_impl) {
Ok(expanded) => expanded,
Err(err) => err.to_compile_error().into(),
}
}

View File

@@ -4,6 +4,8 @@ edition = "2021"
version.workspace = true
[dependencies]
cuddle-actions-sdk-derive.workspace = true
anyhow.workspace = true
clap = { workspace = true, features = ["string"] }
serde.workspace = true

View File

@@ -4,6 +4,8 @@ use std::{collections::BTreeMap, ffi::OsString, io::Write};
use serde::Serialize;
pub use cuddle_actions_sdk_derive::Actions;
// Fix design make it so that it works like axum!
type ActionFn = dyn Fn() -> anyhow::Result<()> + 'static;

View File

@@ -5,5 +5,5 @@ edition = "2021"
[dependencies]
anyhow = "1.0.91"
cuddle-actions-sdk = { git = "ssh://git@git.front.kjuulh.io/kjuulh/cuddle-v2", version = "0.2.0" }
cuddle-actions-sdk = { git = "https://git.front.kjuulh.io/kjuulh/cuddle-v2", version = "0.2.0" }
tokio = { version = "1.41.0", features = ["full"] }

View File

@@ -1,5 +1,14 @@
use cuddle_actions_sdk::AddActionOptions;
struct Registry;
#[Actions]
impl Registry {
pub async fn something() -> anyhow::Result<()> {
Ok(())
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
cuddle_actions_sdk::CuddleActions::default()

View File

@@ -1 +0,0 @@