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:
Kasper Juul Hermansen 2024-10-27 12:05:30 +01:00
parent c94f6b72b8
commit 892fdc9420
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
9 changed files with 161 additions and 4 deletions

73
Cargo.lock generated
View File

@ -7,7 +7,7 @@ name = "action"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cuddle-actions-sdk 0.2.0 (git+ssh://git@git.front.kjuulh.io/kjuulh/cuddle-v2)", "cuddle-actions-sdk 0.2.0 (git+https://git.front.kjuulh.io/kjuulh/cuddle-v2)",
"tokio", "tokio",
] ]
@ -277,6 +277,7 @@ version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"cuddle-actions-sdk-derive",
"pretty_assertions", "pretty_assertions",
"serde", "serde",
"serde_json", "serde_json",
@ -285,7 +286,7 @@ dependencies = [
[[package]] [[package]]
name = "cuddle-actions-sdk" name = "cuddle-actions-sdk"
version = "0.2.0" version = "0.2.0"
source = "git+ssh://git@git.front.kjuulh.io/kjuulh/cuddle-v2#350a3669b04e3ae37fbb31f842f69e1e8afa1721" source = "git+https://git.front.kjuulh.io/kjuulh/cuddle-v2#c94f6b72b8b17d1e94e63211155e93a4b28bcc80"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@ -293,6 +294,18 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "cuddle-actions-sdk-derive"
version = "0.2.0"
dependencies = [
"anyhow",
"darling",
"proc-macro-crate",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "cuddle-file" name = "cuddle-file"
version = "0.2.0" version = "0.2.0"
@ -354,6 +367,41 @@ dependencies = [
"toml", "toml",
] ]
[[package]]
name = "darling"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.20.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]] [[package]]
name = "diff" name = "diff"
version = "0.1.13" version = "0.1.13"
@ -403,6 +451,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]] [[package]]
name = "fs_extra" name = "fs_extra"
version = "1.3.0" version = "1.3.0"
@ -460,6 +514,12 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.6.0" version = "2.6.0"
@ -623,6 +683,15 @@ dependencies = [
"yansi", "yansi",
] ]
[[package]]
name = "proc-macro-crate"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b"
dependencies = [
"toml_edit",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.89" version = "1.0.89"

View File

@ -12,6 +12,8 @@ cuddle-rust-actions = { path = "crates/cuddle-rust-actions" }
cuddle-lazy = { path = "crates/cuddle-lazy" } cuddle-lazy = { path = "crates/cuddle-lazy" }
cuddle-actions-api = { path = "crates/cuddle-actions-api" } cuddle-actions-api = { path = "crates/cuddle-actions-api" }
cuddle-value = { path = "crates/cuddle-value" } cuddle-value = { path = "crates/cuddle-value" }
cuddle-actions-sdk = { path = "crates/cuddle-actions-sdk" }
cuddle-actions-sdk-derive = { path = "crates/cuddle-actions-sdk-derive" }
anyhow = { version = "1" } anyhow = { version = "1" }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }

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 version.workspace = true
[dependencies] [dependencies]
cuddle-actions-sdk-derive.workspace = true
anyhow.workspace = true anyhow.workspace = true
clap = { workspace = true, features = ["string"] } clap = { workspace = true, features = ["string"] }
serde.workspace = true serde.workspace = true

View File

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

View File

@ -5,5 +5,5 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.91" 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"] } tokio = { version = "1.41.0", features = ["full"] }

View File

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

View File

@ -1 +0,0 @@