-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib project added; stub replacement for impl created added; parsing o…
…f attrs and mod working; code compiles
- Loading branch information
Showing
6 changed files
with
155 additions
and
33 deletions.
There are no files selected for viewing
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
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,11 @@ | ||
[package] | ||
name = "psibase-macros-lib" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
darling = "0.20.10" | ||
proc-macro2 = "1.0.36" | ||
syn = { version = "1.0.86" } | ||
quote = "1.0.15" | ||
proc-macro-error = "1.0.4" |
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 @@ | ||
pub mod service_macro; |
93 changes: 93 additions & 0 deletions
93
rust/psibase_macros/psibase-macros-lib/src/service_macro.rs
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,93 @@ | ||
use std::str::FromStr; | ||
|
||
use darling::ast::NestedMeta; | ||
use darling::{Error, FromMeta}; | ||
use proc_macro2::TokenStream; | ||
use proc_macro_error::abort; | ||
use quote::quote; | ||
use syn::{Item, ItemMod}; | ||
|
||
#[derive(Debug, FromMeta)] | ||
#[darling(default)] | ||
pub struct Options { | ||
name: String, | ||
recursive: bool, | ||
constant: String, | ||
actions: String, | ||
wrapper: String, | ||
structs: String, | ||
history_events: String, | ||
ui_events: String, | ||
merkle_events: String, | ||
event_structs: String, | ||
dispatch: Option<bool>, | ||
pub_constant: bool, | ||
psibase_mod: String, | ||
gql: bool, | ||
} | ||
|
||
impl Default for Options { | ||
fn default() -> Self { | ||
Options { | ||
name: "".into(), | ||
recursive: false, | ||
constant: "SERVICE".into(), | ||
actions: "Actions".into(), | ||
wrapper: "Wrapper".into(), | ||
structs: "action_structs".into(), | ||
history_events: "HistoryEvents".into(), | ||
ui_events: "UiEvents".into(), | ||
merkle_events: "MerkleEvents".into(), | ||
event_structs: "event_structs".into(), | ||
dispatch: None, | ||
pub_constant: true, | ||
psibase_mod: "psibase".into(), | ||
gql: true, | ||
} | ||
} | ||
} | ||
|
||
pub fn service_macro_impl(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let attr_args = match NestedMeta::parse_meta_list(attr) { | ||
Ok(v) => v, | ||
Err(e) => { | ||
return TokenStream::from(Error::from(e).write_errors()); | ||
} | ||
}; | ||
|
||
let mut options: Options = match Options::from_list(&attr_args) { | ||
Ok(val) => val, | ||
Err(err) => { | ||
return err.write_errors().into(); | ||
} | ||
}; | ||
|
||
if options.name.is_empty() { | ||
options.name = std::env::var("CARGO_PKG_NAME").unwrap().replace('_', "-"); | ||
} | ||
if options.dispatch.is_none() { | ||
options.dispatch = Some(std::env::var_os("CARGO_PRIMARY_PACKAGE").is_some()); | ||
} | ||
if std::env::var_os("CARGO_PSIBASE_TEST").is_some() { | ||
options.dispatch = Some(false); | ||
} | ||
let psibase_mod = proc_macro2::TokenStream::from_str(&options.psibase_mod).unwrap(); | ||
let item = syn::parse2::<syn::Item>(item).unwrap(); | ||
match item { | ||
Item::Mod(impl_mod) => process_mod(&options, &psibase_mod, impl_mod), | ||
_ => { | ||
abort!(item, "service attribute may only be used on a module") | ||
} | ||
} | ||
} | ||
|
||
fn process_mod( | ||
_options: &Options, | ||
_psibase_mod: &proc_macro2::TokenStream, | ||
mut _impl_mod: ItemMod, | ||
) -> TokenStream { | ||
quote! { | ||
42 | ||
} | ||
.into() | ||
} |