Skip to content

Commit

Permalink
lib project added; stub replacement for impl created added; parsing o…
Browse files Browse the repository at this point in the history
…f attrs and mod working; code compiles
  • Loading branch information
cool-ant committed Sep 26, 2024
1 parent a6a75d6 commit 2cfe582
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 33 deletions.
74 changes: 43 additions & 31 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/psibase_macros/psibase-macros-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ proc-macro2 = "1.0.36"
psibase_names = { version = "0.12.0", path = "../../psibase_names" }
quote = "1.0.15"
syn = { version = "1.0.86", features = ["full", "visit-mut"] }
psibase-macros-lib = { path = "../psibase-macros-lib" }
8 changes: 6 additions & 2 deletions rust/psibase_macros/psibase-macros-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use graphql_macro::{queries_macro_impl, table_query_macro_impl, table_query_subi
use number_macro::{account_macro_impl, method_macro_impl};
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use psibase_macros_lib::service_macro::service_macro_impl;
use schema_macro::schema_derive_macro;
use service_macro::service_macro_impl;
use test_case_macro::test_case_macro_impl;
use to_key_macro::to_key_macro_impl;

Expand Down Expand Up @@ -289,7 +289,11 @@ pub fn to_schema(input: TokenStream) -> TokenStream {
#[proc_macro_error]
#[proc_macro_attribute]
pub fn service(attr: TokenStream, item: TokenStream) -> TokenStream {
service_macro_impl(attr, item)
service_macro_impl(
proc_macro2::TokenStream::from(attr),
proc_macro2::TokenStream::from(item),
)
.into()
}

/// Define a [psibase](https://psibase.io) test case.
Expand Down
11 changes: 11 additions & 0 deletions rust/psibase_macros/psibase-macros-lib/Cargo.toml
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"
1 change: 1 addition & 0 deletions rust/psibase_macros/psibase-macros-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod service_macro;
93 changes: 93 additions & 0 deletions rust/psibase_macros/psibase-macros-lib/src/service_macro.rs
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()
}

0 comments on commit 2cfe582

Please sign in to comment.