From be77ab9361f654db36ac9a61e261adb17d3b6c4b Mon Sep 17 00:00:00 2001 From: AINDUSTRIES Date: Mon, 2 Dec 2024 18:21:48 +0100 Subject: [PATCH] Testing procedural macros --- Cargo.toml | 1 + aweblib_macros/.gitignore | 2 ++ aweblib_macros/Cargo.toml | 11 +++++++++++ aweblib_macros/src/lib.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 17 +++++++++++++++++ src/renderer.rs | 4 +++- src/routing.rs | 26 +++++++++++++++++++++----- 7 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 aweblib_macros/.gitignore create mode 100644 aweblib_macros/Cargo.toml create mode 100644 aweblib_macros/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 05f02f3..d935827 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ bytes = "1.8.0" http-body-util = "0.1.2" sqlx = {version = "0.8.1", features = ["sqlite", "sqlx-macros", "runtime-tokio"]} daemonize = {version = "0.5.0", optional = true} +aweblib_macros = {path = "aweblib_macros" } [dev-dependencies] futures = "0.3.31" \ No newline at end of file diff --git a/aweblib_macros/.gitignore b/aweblib_macros/.gitignore new file mode 100644 index 0000000..36e2917 --- /dev/null +++ b/aweblib_macros/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +/target \ No newline at end of file diff --git a/aweblib_macros/Cargo.toml b/aweblib_macros/Cargo.toml new file mode 100644 index 0000000..2f61231 --- /dev/null +++ b/aweblib_macros/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "aweblib_macros" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +syn = "2.0.90" +quote = "1.0.37" \ No newline at end of file diff --git a/aweblib_macros/src/lib.rs b/aweblib_macros/src/lib.rs new file mode 100644 index 0000000..60dfeae --- /dev/null +++ b/aweblib_macros/src/lib.rs @@ -0,0 +1,28 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, Data, DataStruct}; + +#[proc_macro_derive(Getter)] +pub fn getter(item: TokenStream) -> TokenStream { + let input = parse_macro_input!(item as syn::DeriveInput); + let ident = input.ident; + let mut quote = quote! { + println!("Test"); + }; + if let Data::Struct(DataStruct { fields, .. }) = input.data { + for field in fields { + let name = field.ident.unwrap(); + quote.extend(quote! { + println!("{}",stringify!(#name)); + }) + } + } + quote!( + impl #ident { + fn get() { + #quote + } + } + ) + .into() +} diff --git a/src/lib.rs b/src/lib.rs index 30bce55..1cc5338 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,8 @@ mod routing; mod server; mod settings; +pub use aweblib_macros::Getter; + pub struct SimpleHttpServer { server: Server, } @@ -34,3 +36,18 @@ impl SimpleHttpServer { self.server.run() } } + +#[cfg(test)] +mod test { + use aweblib_macros::Getter; + #[derive(Getter)] + pub struct Test { + one: u8, + two: String, + } + + #[test] + fn test() { + Test::get(); + } +} diff --git a/src/renderer.rs b/src/renderer.rs index c8112fb..9e986b6 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -13,7 +13,9 @@ pub(crate) fn get_static(file: &String) -> Response { } } -pub(crate) fn get_rendered() {} +pub(crate) fn get_rendered(file: &String) -> Response { + todo!() +} fn read_static(file: &String) -> Result, Box> { let mut buf = Vec::new(); diff --git a/src/routing.rs b/src/routing.rs index dd57b79..6bcbb3f 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -1,6 +1,6 @@ use crate::body::Body; -use crate::renderer::get_static; -use hyper::{Request, Response}; +use crate::renderer::{get_rendered, get_static}; +use hyper::{Method, Request, Response}; use serde::Deserialize; use std::collections::HashMap; use std::fs::File; @@ -11,6 +11,7 @@ use std::sync::{Arc, Mutex}; pub(crate) enum Resource { Page(String), File(String), + Data(String), Static, #[default] None, @@ -107,10 +108,25 @@ where } i += 1; } + let args = split[i..split.len()].to_vec(); let route = &tree.route; - match &route.resource { - Resource::File(file) => get_static(file), - _ => Response::new(Body::Empty), + match req.method() { + &Method::GET => { + match &route.resource { + Resource::File(file) => get_static(file), + Resource::Page(page) => get_rendered(page), + Resource::Data(data) => todo!(), + Resource::Static => { + let rel_path = args.join(""); + // Replace under with settings + let file = format!("static/{}", rel_path); + get_static(&file) + } + _ => Response::new(Body::Empty), + } + } + &Method::POST => todo!(), + _ => todo!(), } }