Skip to content

Commit

Permalink
Add surfer_macros crate and surfer_launch attribute + add main example
Browse files Browse the repository at this point in the history
  • Loading branch information
Padi2312 committed Mar 10, 2024
1 parent 02bdea2 commit 1e09367
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 31 deletions.
57 changes: 32 additions & 25 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ authors = ["Patrick Arndt <patrick.arndt.99@web.de>"]
async-std = { version = "1.12.0", features = ["attributes"] }
chrono = "0.4.34"
futures = "0.3.30"
proc-macro2 = "1.0"
syn = { version = "^2.0", features = ["full", "derive"] }
quote = "1.0"
regex = "1.10.3"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }

surfer_macros = { path = "./surfer_macros" }

[lib]
name = "surfer"
Expand Down
3 changes: 2 additions & 1 deletion bin/main_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use surfer::response::json_response::JsonResponse;
use surfer::response::{IntoResponse, Response};
use surfer::route;
use surfer::server::Server;
use surfer_macros::surfer_launch;

async fn index(_: Request) -> Response {
let json_obj = json!({
Expand All @@ -22,7 +23,7 @@ async fn index(_: Request) -> Response {
}

#[cfg(not(release))]
#[async_std::main]
#[surfer_launch]
async fn main() {
println!("Testing the 'surfer' library...");
let mut server = Server::new(None, None);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pub mod request;
pub mod response;
pub mod server;
mod utils;

pub use surfer_macros;
1 change: 1 addition & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use async_std::net::TcpStream;
use async_std::prelude::*;
use std::collections::HashMap;

#[derive(Debug,Clone)]
pub enum Method {
GET,
POST,
Expand Down
46 changes: 46 additions & 0 deletions surfer_macros/Cargo.lock

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

12 changes: 12 additions & 0 deletions surfer_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "surfer_macros"
version = "0.1.0"
edition = "2018"

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0"
syn = { version = "1.0", features = ["full"] }
quote = "1.0"
26 changes: 26 additions & 0 deletions surfer_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse_macro_input, ItemFn, LitStr};

#[proc_macro_attribute]
pub fn surfer_launch(_attr: TokenStream, item: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree
let input = parse_macro_input!(item as ItemFn);

// Get the components of the input function
let name = &input.sig.ident;
let inputs = &input.sig.inputs;
let output = &input.sig.output;
let body = &input.block;

// Generate the new function
let expanded = quote! {
#[async_std::main]
async fn #name(#inputs) #output {
#body
}
};
expanded.into()
}

0 comments on commit 1e09367

Please sign in to comment.