-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
451 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "example-todo-list" | ||
version = "0.1.0" | ||
publish = false | ||
description = "TODO List - Flareon example." | ||
edition = "2021" | ||
|
||
[dependencies] | ||
askama = "0.12.1" | ||
flareon = { path = "../../flareon" } | ||
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] } | ||
env_logger = "0.11.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::sync::Arc; | ||
|
||
use askama::Template; | ||
use flareon::prelude::{ | ||
Body, Error, FlareonApp, FlareonProject, Request, Response, Route, StatusCode, | ||
}; | ||
|
||
#[derive(Form)] | ||
struct TodoForm { | ||
title: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct TodoItem { | ||
id: u32, | ||
title: String, | ||
} | ||
|
||
#[derive(Debug, Template)] | ||
#[template(path = "index.html")] | ||
struct IndexTemplate { | ||
todo_items: Vec<TodoItem>, | ||
} | ||
|
||
fn index(_request: Request) -> Result<Response, Error> { | ||
let index_template = IndexTemplate { | ||
todo_items: vec![ | ||
TodoItem { | ||
id: 1, | ||
title: "Buy milk".to_string(), | ||
}, | ||
TodoItem { | ||
id: 2, | ||
title: "Buy eggs".to_string(), | ||
}, | ||
], | ||
}; | ||
let rendered = index_template.render().unwrap(); | ||
|
||
Ok(Response::new_html( | ||
StatusCode::OK, | ||
Body::fixed(rendered.as_bytes().to_vec()), | ||
)) | ||
} | ||
|
||
fn add_todo(request: Request) -> Result<Response, Error> { | ||
let todo_form = TodoForm::from_request(&request).unwrap(); | ||
|
||
// TODO add to global list | ||
|
||
Ok(Response::new_html( | ||
StatusCode::OK, | ||
Body::fixed("redirect is not implemented yet".as_bytes().to_vec()), | ||
)) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
|
||
let todo_app = FlareonApp::builder() | ||
.urls([Route::with_handler("/", Arc::new(Box::new(index)))]) | ||
.urls([Route::with_handler("/add", Arc::new(Box::new(add_todo)))]) | ||
.build() | ||
.unwrap(); | ||
|
||
let todo_project = FlareonProject::builder() | ||
.register_app_with_views(todo_app, "") | ||
.build() | ||
.unwrap(); | ||
|
||
flareon::run(todo_project, "127.0.0.1:8000").await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>TODO List</title> | ||
</head> | ||
<body> | ||
<h1>TODO List</h1> | ||
<form id="todo-form"> | ||
<input type="text" id="todo-input" placeholder="Enter a new TODO" required> | ||
<button type="submit">Add TODO</button> | ||
</form> | ||
<ul id="todo-list"> | ||
{% for todo in todo_items %} | ||
<li> | ||
<span>{{ todo.title }}</span> | ||
<button onclick="removeTodoItem(this)">Remove</button> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::any::{Any, TypeId}; | ||
|
||
use darling::{FromDeriveInput, FromField, FromMeta}; | ||
use proc_macro2::Ident; | ||
|
||
pub fn form_for_struct(ast: syn::DeriveInput) -> proc_macro2::TokenStream { | ||
let opts = match MyTraitOpts::from_derive_input(&ast) { | ||
Ok(val) => val, | ||
Err(err) => { | ||
return err.write_errors(); | ||
} | ||
}; | ||
|
||
for field in opts.data.take_struct().unwrap().fields { | ||
println!("{:#?}", field); | ||
} | ||
|
||
// ast.data | ||
// println!("{:#?}", opts); | ||
proc_macro2::TokenStream::new() | ||
} | ||
|
||
#[derive(Debug, FromDeriveInput)] | ||
#[darling(attributes(my_crate), forward_attrs(allow, doc, cfg))] | ||
pub struct MyTraitOpts { | ||
ident: syn::Ident, | ||
attrs: Vec<syn::Attribute>, | ||
data: darling::ast::Data<darling::util::Ignored, Field>, | ||
} | ||
|
||
#[derive(Debug, Clone, FromField)] | ||
pub struct Field { | ||
ident: Option<Ident>, | ||
ty: syn::Type, | ||
} | ||
|
||
fn get_form_field_for_field(field: &Field) -> () { | ||
// let xd: String = "xd"; | ||
// | ||
// // check if string | ||
// if field.ty == syn::parse_quote!(String) { | ||
// println!("String"); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
mod form; | ||
|
||
use darling::FromDeriveInput; | ||
use proc_macro::TokenStream; | ||
use syn::parse_macro_input; | ||
|
||
use crate::form::form_for_struct; | ||
|
||
#[proc_macro] | ||
pub fn flareon(_input: TokenStream) -> TokenStream { | ||
unimplemented!() | ||
#[proc_macro_derive(Form)] | ||
pub fn derive_form(input: TokenStream) -> TokenStream { | ||
let ast = parse_macro_input!(input as syn::DeriveInput); | ||
form_for_struct(ast).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[test] | ||
fn test_derive_form() { | ||
let t = trybuild::TestCases::new(); | ||
t.pass("tests/ui/derive_form.rs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use flareon_macros::Form; | ||
|
||
#[derive(Debug, Form)] | ||
struct MyForm { | ||
name: String, | ||
name2: std::string::String, | ||
age: u32, | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub use crate::router::Route; | ||
pub use crate::{ | ||
Body, Error, FlareonApp, FlareonProject, Request, RequestHandler, Response, Route, StatusCode, | ||
Body, Error, FlareonApp, FlareonProject, Request, RequestHandler, Response, StatusCode, | ||
}; |
Oops, something went wrong.