Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ members = [
"clift",
"fastn",
"fastn-builtins",
"fastn-context",
"fastn-context-macros",
"fastn-core",
"fastn-daemon",
"fastn-ds",
Expand Down Expand Up @@ -89,6 +91,7 @@ enum-iterator = "0.6"
enum-iterator-derive = "0.6"
env_logger = "0.11"
fastn-builtins.path = "fastn-builtins"
fastn-context.path = "fastn-context"
fastn-core.path = "fastn-core"
fastn-ds.path = "fastn-ds"
fastn-daemon.path = "fastn-daemon"
Expand Down Expand Up @@ -139,6 +142,7 @@ snafu = "0.8"
thiserror = "2"
tokio = { version = "1", features = ["full"] }
tokio-postgres = { version = "0.7", features = ["with-serde_json-1", "with-uuid-1"] }
tokio-util = "0.7"
tracing = "0.1"
url = "2"
walkdir = "2"
Expand Down
18 changes: 18 additions & 0 deletions fastn-context-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "fastn-context-macros"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
description.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true
rust-version.workspace = true

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full", "extra-traits"] }
35 changes: 35 additions & 0 deletions fastn-context-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{ItemFn, parse_macro_input};

/// Main function attribute macro for fastn applications with context support
#[proc_macro_attribute]
pub fn main(_args: TokenStream, input: TokenStream) -> TokenStream {
let input_fn = parse_macro_input!(input as ItemFn);

let user_fn_name = syn::Ident::new("__fastn_user_main", proc_macro2::Span::call_site());
let fn_block = &input_fn.block;
let fn_attrs = &input_fn.attrs;
let fn_vis = &input_fn.vis;

quote! {
#(#fn_attrs)*
#fn_vis fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
// Initialize tokio runtime
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(async {
// Global context automatically created

// Call user's main function
let result = #user_fn_name().await;

result
})
}

async fn #user_fn_name() -> std::result::Result<(), Box<dyn std::error::Error>> #fn_block
}
.into()
}
15 changes: 15 additions & 0 deletions fastn-context/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "fastn-context"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
description.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true
rust-version.workspace = true

[dependencies]
tokio.workspace = true
tokio-util.workspace = true
fastn-context-macros = { path = "../fastn-context-macros" }
Loading