Testing procedural macros
This commit is contained in:
@@ -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"
|
||||
2
aweblib_macros/.gitignore
vendored
Normal file
2
aweblib_macros/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
Cargo.lock
|
||||
/target
|
||||
11
aweblib_macros/Cargo.toml
Normal file
11
aweblib_macros/Cargo.toml
Normal file
@@ -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"
|
||||
28
aweblib_macros/src/lib.rs
Normal file
28
aweblib_macros/src/lib.rs
Normal file
@@ -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()
|
||||
}
|
||||
17
src/lib.rs
17
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,9 @@ pub(crate) fn get_static(file: &String) -> Response<Body> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_rendered() {}
|
||||
pub(crate) fn get_rendered(file: &String) -> Response<Body> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn read_static(file: &String) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let mut buf = Vec::new();
|
||||
|
||||
@@ -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!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user