From 311b49a2fdd97b8b870dbaccccb55058ee0207c8 Mon Sep 17 00:00:00 2001 From: Melody Horn / boringcactus Date: Sun, 13 Jun 2021 16:33:46 -0600 Subject: lay groundwork for models --- Cargo.toml | 3 +++ examples/tutorial01/main.rs | 4 ++-- examples/tutorial01/polls/mod.rs | 2 ++ examples/tutorial02/main.rs | 25 +++++++++++++++++++++++++ examples/tutorial02/polls/mod.rs | 11 +++++++++++ examples/tutorial02/polls/models.rs | 21 +++++++++++++++++++++ examples/tutorial02/polls/urls.rs | 9 +++++++++ examples/tutorial02/polls/views.rs | 5 +++++ src/apps.rs | 3 +++ src/contrib/admin/mod.rs | 6 ++++++ src/contrib/admin/site/mod.rs | 2 ++ src/db/mod.rs | 1 + src/db/models/mod.rs | 3 +++ src/lib.rs | 2 ++ src/settings.rs | 5 ++++- tosin-macros/Cargo.toml | 14 ++++++++++++++ tosin-macros/src/lib.rs | 26 ++++++++++++++++++++++++++ 17 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 examples/tutorial02/main.rs create mode 100644 examples/tutorial02/polls/mod.rs create mode 100644 examples/tutorial02/polls/models.rs create mode 100644 examples/tutorial02/polls/urls.rs create mode 100644 examples/tutorial02/polls/views.rs create mode 100644 src/apps.rs create mode 100644 src/db/mod.rs create mode 100644 src/db/models/mod.rs create mode 100644 tosin-macros/Cargo.toml create mode 100644 tosin-macros/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 7c0c675..aa99c74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,10 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +diesel = { version = "1.4.4", features = ["sqlite"] } hyper = { version = "0.14", features = ["full"] } structopt = "0.3.21" +time = "0.2.27" tokio = { version = "1", features = ["full"] } +tosin-macros = { version = "0.1.0", path = "./tosin-macros" } warp = "0.3" diff --git a/examples/tutorial01/main.rs b/examples/tutorial01/main.rs index f99ae7f..0fb087d 100644 --- a/examples/tutorial01/main.rs +++ b/examples/tutorial01/main.rs @@ -6,8 +6,8 @@ mod polls; fn urls() -> UrlMap { url_map! { - "polls" / ..polls::urls::urls(), - "admin" / ..admin::site::urls::urls(), + "polls" / ..polls::urls(), + "admin" / ..admin::site::urls(), } } diff --git a/examples/tutorial01/polls/mod.rs b/examples/tutorial01/polls/mod.rs index fd1dad8..d22af0f 100644 --- a/examples/tutorial01/polls/mod.rs +++ b/examples/tutorial01/polls/mod.rs @@ -1,2 +1,4 @@ pub mod urls; pub mod views; + +pub use urls::urls; diff --git a/examples/tutorial02/main.rs b/examples/tutorial02/main.rs new file mode 100644 index 0000000..c662001 --- /dev/null +++ b/examples/tutorial02/main.rs @@ -0,0 +1,25 @@ +use tosin::Settings; +use tosin::contrib::admin; +use tosin::urls::{UrlMap, url_map}; + +mod polls; + +fn urls() -> UrlMap { + url_map! { + "polls" / ..polls::urls(), + "admin" / ..admin::site::urls(), + } +} + +fn settings() -> Settings { + Settings { + installed_apps: &[ + &polls::APP, + &admin::APP, + ], + database_url: "example_data.sqlite3", + ..Settings::default() + } +} + +tosin::main!(urls(), settings()); diff --git a/examples/tutorial02/polls/mod.rs b/examples/tutorial02/polls/mod.rs new file mode 100644 index 0000000..f4fde54 --- /dev/null +++ b/examples/tutorial02/polls/mod.rs @@ -0,0 +1,11 @@ +use tosin::apps::AppConfig; + +pub mod models; +pub mod urls; +pub mod views; + +pub use urls::urls; + +pub const APP: AppConfig = AppConfig { + +}; diff --git a/examples/tutorial02/polls/models.rs b/examples/tutorial02/polls/models.rs new file mode 100644 index 0000000..5ed52c5 --- /dev/null +++ b/examples/tutorial02/polls/models.rs @@ -0,0 +1,21 @@ +use tosin::db::models::{Model, Id}; + +#[derive(Model)] +pub struct Question { + id: Option, + #[model(max_length=200)] + question_text: String, + /// date published + pub_date: time::PrimitiveDateTime, +} + +#[derive(Model)] +pub struct Choice { + id: Option, + #[model(Question, on_delete=Cascade)] + question: Id, + #[model(max_length=200)] + choice_text: String, + #[model(default = 0)] + votes: usize, +} diff --git a/examples/tutorial02/polls/urls.rs b/examples/tutorial02/polls/urls.rs new file mode 100644 index 0000000..04d93cc --- /dev/null +++ b/examples/tutorial02/polls/urls.rs @@ -0,0 +1,9 @@ +use tosin::urls::{UrlMap, url_map}; + +use super::views; + +pub fn urls() -> UrlMap { + url_map! { + => views::index, // TODO name: "index" + } +} diff --git a/examples/tutorial02/polls/views.rs b/examples/tutorial02/polls/views.rs new file mode 100644 index 0000000..4859cec --- /dev/null +++ b/examples/tutorial02/polls/views.rs @@ -0,0 +1,5 @@ +use tosin::http::{Reply, Response}; + +pub fn index() -> Response { + "Hello, world. You're at the polls index.".into_response() +} diff --git a/src/apps.rs b/src/apps.rs new file mode 100644 index 0000000..26c6124 --- /dev/null +++ b/src/apps.rs @@ -0,0 +1,3 @@ +pub struct AppConfig { + +} diff --git a/src/contrib/admin/mod.rs b/src/contrib/admin/mod.rs index 4d481aa..0f7e880 100644 --- a/src/contrib/admin/mod.rs +++ b/src/contrib/admin/mod.rs @@ -1 +1,7 @@ +use crate::apps::AppConfig; + pub mod site; + +pub const APP: AppConfig = AppConfig { + +}; diff --git a/src/contrib/admin/site/mod.rs b/src/contrib/admin/site/mod.rs index c8faf42..8448370 100644 --- a/src/contrib/admin/site/mod.rs +++ b/src/contrib/admin/site/mod.rs @@ -1 +1,3 @@ pub mod urls; + +pub use urls::urls; diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..c446ac8 --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1 @@ +pub mod models; diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs new file mode 100644 index 0000000..5e59949 --- /dev/null +++ b/src/db/models/mod.rs @@ -0,0 +1,3 @@ +pub use tosin_macros::Model; + +pub type Id = usize; diff --git a/src/lib.rs b/src/lib.rs index 38ae0e2..677d75d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ +pub mod apps; mod cli; pub mod contrib; +pub mod db; pub mod http; pub mod settings; pub mod urls; diff --git a/src/settings.rs b/src/settings.rs index 9d29cfe..9d34c0d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,10 +1,13 @@ pub struct Settings { + pub installed_apps: &'static [&'static crate::apps::AppConfig], + pub database_url: &'static str, } impl Default for Settings { fn default() -> Self { Self { - + installed_apps: &[], + database_url: "", } } } diff --git a/tosin-macros/Cargo.toml b/tosin-macros/Cargo.toml new file mode 100644 index 0000000..4a58a20 --- /dev/null +++ b/tosin-macros/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "tosin-macros" +version = "0.1.0" +authors = ["Melody Horn / boringcactus "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +proc-macro = true + +[dependencies] +syn = "1.0" +quote = "1.0" diff --git a/tosin-macros/src/lib.rs b/tosin-macros/src/lib.rs new file mode 100644 index 0000000..c0651fa --- /dev/null +++ b/tosin-macros/src/lib.rs @@ -0,0 +1,26 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; + +#[proc_macro_derive(Model, attributes(model))] +pub fn model_derive(input: TokenStream) -> TokenStream { + // Construct a representation of Rust code as a syntax tree + // that we can manipulate + let ast = syn::parse(input).unwrap(); + + // Build the trait implementation + impl_model(&ast) +} + +fn impl_model(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + impl #name { + fn hello_macro() { + println!("Hello, Macro! My name is {}!", stringify!(#name)); + } + } + }; + gen.into() +} -- cgit v1.2.3