From e310e547f67f8e7749ea4cbf9b1050fefe7ff93e Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 13 Jun 2023 12:34:54 -0700 Subject: [PATCH] shit is fucked up, probably abandoning --- Cargo.lock | 9 +++++++ Cargo.toml | 3 +++ maybe_optional_user/Cargo.lock | 46 ++++++++++++++++++++++++++++++++++ maybe_optional_user/Cargo.toml | 11 ++++++++ maybe_optional_user/src/lib.rs | 34 +++++++++++++++++++++++++ src/login.rs | 6 ++--- src/signup.rs | 4 +-- src/templates.rs | 46 +++++++++++++++++++++++++++++++++- src/users.rs | 1 + src/watches/templates.rs | 5 +++- templates/base.html | 32 ++++++++++++----------- templates/header.html | 22 ++++++++++++++++ templates/signup.html | 2 ++ templates/signup_success.html | 2 +- 14 files changed, 200 insertions(+), 23 deletions(-) create mode 100644 maybe_optional_user/Cargo.lock create mode 100644 maybe_optional_user/Cargo.toml create mode 100644 maybe_optional_user/src/lib.rs create mode 100644 templates/header.html diff --git a/Cargo.lock b/Cargo.lock index 5d15bd0..45083fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1066,6 +1066,14 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40" +[[package]] +name = "maybe_optional_user" +version = "0.1.0" +dependencies = [ + "quote", + "syn 2.0.18", +] + [[package]] name = "memchr" version = "2.5.0" @@ -2373,6 +2381,7 @@ dependencies = [ "axum-macros", "axum-test", "justerror", + "maybe_optional_user", "password-hash", "rand_core", "serde", diff --git a/Cargo.toml b/Cargo.toml index 27e5949..7327a11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,9 @@ axum-login = { version = "0.5", features = ["sqlite", "sqlx"] } unicode-segmentation = "1" async-session = "3" +# proc macros: +maybe_optional_user = {path = "maybe_optional_user"} + [dev-dependencies] axum-test = "9.0.0" diff --git a/maybe_optional_user/Cargo.lock b/maybe_optional_user/Cargo.lock new file mode 100644 index 0000000..c5f4677 --- /dev/null +++ b/maybe_optional_user/Cargo.lock @@ -0,0 +1,46 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "maybe_optional_user" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" diff --git a/maybe_optional_user/Cargo.toml b/maybe_optional_user/Cargo.toml new file mode 100644 index 0000000..b25cdff --- /dev/null +++ b/maybe_optional_user/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "maybe_optional_user" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true + +[dependencies] +quote = "1.0.28" +syn = "2.0.18" diff --git a/maybe_optional_user/src/lib.rs b/maybe_optional_user/src/lib.rs new file mode 100644 index 0000000..fc767b6 --- /dev/null +++ b/maybe_optional_user/src/lib.rs @@ -0,0 +1,34 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, ItemStruct}; + +#[proc_macro_derive(ImplMaybeOptionalUser)] +pub fn derive_maybe_optional_user(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as ItemStruct); + + let name = &input.ident; + let mut has_opt_user = false; + input.fields.iter().inspect(|f| { + if let Some(ident) = f.ident.clone() { + match &f.ty { + syn::Type::Path(path) + if !path.path.segments.is_empty() + && path.path.segments.first().unwrap().ident == "Option" => + { + has_opt_user = true && ident == "user"; + } + _ => {} + } + } + }); + + let output = quote! { + impl crate::templates::MaybeOptionalUser for #name { + fn has_optional_user() -> bool { + #has_opt_user + } + } + }; + + TokenStream::from(output) +} diff --git a/src/login.rs b/src/login.rs index a2dea90..2060cc2 100644 --- a/src/login.rs +++ b/src/login.rs @@ -165,7 +165,7 @@ mod test { let s = server().await; let resp = s.get("/logout").await; let body = std::str::from_utf8(resp.bytes()).unwrap().to_string(); - assert_eq!(body, LogoutGet.to_string()); + assert_eq!(body, LogoutGet::default().to_string()); } #[tokio::test] @@ -174,7 +174,7 @@ mod test { let resp = s.post("/logout").await; resp.assert_status_ok(); let body = std::str::from_utf8(resp.bytes()).unwrap(); - let default = LogoutPost.to_string(); + let default = LogoutPost::default().to_string(); assert_eq!(body, &default); } @@ -205,7 +205,7 @@ mod test { let resp = s.post("/logout").await; let body = std::str::from_utf8(resp.bytes()).unwrap(); - let default = LogoutPost.to_string(); + let default = LogoutPost::default().to_string(); assert_eq!(body, &default); } } diff --git a/src/signup.rs b/src/signup.rs index 171a643..6365ff8 100644 --- a/src/signup.rs +++ b/src/signup.rs @@ -122,7 +122,7 @@ pub async fn handle_signup_success( .unwrap_or_default() }; - let mut resp = CreateUserSuccess(user.clone()).into_response(); + let mut resp = CreateUserSuccess { user: user.clone() }.into_response(); if user.username.is_empty() || id.is_empty() { // redirect to front page if we got here without a valid witch ID @@ -260,7 +260,7 @@ mod test { let resp = server.get(&path).expect_success().await; let body = std::str::from_utf8(resp.bytes()).unwrap(); - let expected = CreateUserSuccess(user).to_string(); + let expected = CreateUserSuccess { user }.to_string(); assert_eq!(&expected, body); } diff --git a/src/templates.rs b/src/templates.rs index 4d624f6..02ed349 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -3,6 +3,12 @@ use serde::{Deserialize, Serialize}; use crate::User; +pub trait MaybeOptionalUser { + fn has_optional_user() -> bool { + false + } +} + #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "signup.html")] pub struct CreateUser { @@ -11,11 +17,24 @@ pub struct CreateUser { pub email: Option, pub password: String, pub pw_verify: String, + user: Option, +} +impl MaybeOptionalUser for CreateUser { + fn has_optional_user() -> bool { + true + } } #[derive(Debug, Clone, Template, Default, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "signup_success.html")] -pub struct CreateUserSuccess(pub User); +pub struct CreateUserSuccess { + pub user: User, +} +impl MaybeOptionalUser for CreateUserSuccess { + fn has_optional_user() -> bool { + false + } +} #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "login_post.html")] @@ -23,6 +42,11 @@ pub struct LoginPost { pub username: String, pub password: String, } +impl MaybeOptionalUser for LoginPost { + fn has_optional_user() -> bool { + false + } +} #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "login_get.html")] @@ -30,17 +54,37 @@ pub struct LoginGet { pub username: String, pub password: String, } +impl MaybeOptionalUser for LoginGet { + fn has_optional_user() -> bool { + false + } +} #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "logout_get.html")] pub struct LogoutGet; +impl MaybeOptionalUser for LogoutGet { + fn has_optional_user() -> bool { + false + } +} #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "logout_post.html")] pub struct LogoutPost; +impl MaybeOptionalUser for LogoutPost { + fn has_optional_user() -> bool { + false + } +} #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "index.html")] pub struct MainPage { pub user: Option, } +impl MaybeOptionalUser for MainPage { + fn has_optional_user() -> bool { + true + } +} diff --git a/src/users.rs b/src/users.rs index 433130c..209ef13 100644 --- a/src/users.rs +++ b/src/users.rs @@ -1,4 +1,5 @@ use std::{ + any::Any, fmt::{Debug, Display}, time::{SystemTime, UNIX_EPOCH}, }; diff --git a/src/watches/templates.rs b/src/watches/templates.rs index 5aa9629..f27e72f 100644 --- a/src/watches/templates.rs +++ b/src/watches/templates.rs @@ -12,4 +12,7 @@ pub struct GetWatches { #[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)] #[template(path = "get_search_watches.html")] -pub struct GetSearchWatches {} +pub struct GetSearchWatches { + pub watches: Vec, + pub user: Option, +} diff --git a/templates/base.html b/templates/base.html index ba52e06..6875281 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,18 +1,20 @@ - - {% block title %}{{ title }} - Witch Watch{% endblock %} - {% block head %}{% endblock %} - - - -
- {% block content %}{% endblock %} -
- - + + + + {% block title %}{{ title }} - Witch Watch{% endblock %} + {% block head %}{% include "header.html" %}{% endblock %} + + + +
+ {% block content %}{% endblock %} +
+ + diff --git a/templates/header.html b/templates/header.html new file mode 100644 index 0000000..e2e9788 --- /dev/null +++ b/templates/header.html @@ -0,0 +1,22 @@ +{% if Self::has_optional_user() %} + +{% match user %} +{% when Some with (usr) %} +
+ Hello, {{ usr.username }}! +
+
+ + +
+{% else %} +
+ Heya, why don't you log in or sign up? +
+{% endmatch %} + +{% else %} + +{% endif %} + +
diff --git a/templates/signup.html b/templates/signup.html index 61b5b44..6de857d 100644 --- a/templates/signup.html +++ b/templates/signup.html @@ -2,6 +2,8 @@ {% block title %}Sign Up for Witch Watch, Bish{% endblock %} +{% block header %}{% endblock %} + {% block content %}

diff --git a/templates/signup_success.html b/templates/signup_success.html index f8e9efc..3bd41da 100644 --- a/templates/signup_success.html +++ b/templates/signup_success.html @@ -7,7 +7,7 @@

You did it!

-{{ self.0 }} +{{ self.user }}