-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19cf127
commit f9c07c2
Showing
2 changed files
with
38 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "hello_world_macro" | ||
version = "0.0.1" | ||
edition = "2018" | ||
authors = ["HugoByte <[email protected]>"] | ||
repository = "https://github.com/HugoByte/internal-research-and-sample-code/tree/hello-world-workflow-macro" | ||
license = "Apache-2.0" | ||
description = "Derive macro for hello world workflow" | ||
|
||
[dependencies] | ||
quote = "1.0.33" | ||
syn = "2.0.29" | ||
|
||
[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,23 @@ | ||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use syn::DeriveInput; | ||
|
||
#[proc_macro_derive(HelloWorldDerive)] | ||
pub fn hello_world_derive_macro(item: TokenStream) -> TokenStream { | ||
|
||
let ast: DeriveInput = syn::parse(item).unwrap(); | ||
let ident = ast.ident; | ||
|
||
// implement the function and return as TokenStream object | ||
quote::quote! { | ||
impl #ident{ | ||
pub fn run(&mut self) -> Result<(), String>{ | ||
self.output = serde_json::to_value(format!("Hello {}", self.input.name)) | ||
.map_err(|e|e.to_string())?; | ||
Ok(()) | ||
} | ||
} | ||
} | ||
.into() | ||
} |