be more explicit

This commit is contained in:
Joe Ardent 2024-02-25 22:49:02 -08:00
parent 1daec80430
commit b924819211
2 changed files with 49 additions and 44 deletions

View file

@ -1,21 +1,23 @@
use std::{ use std::net::SocketAddr;
fmt::{Debug, Display},
net::SocketAddr,
};
use axum::{routing::get, Router}; use axum::{routing::get, Router};
use serde::{Deserialize, Serialize};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use tower_sessions::{MemoryStore, SessionManagerLayer}; use tower_sessions::{MemoryStore, SessionManagerLayer};
// "extern crate" is this olde-timey way to bring macros into the current live
// namespace; I'm not sure why it's needed here; we're using this to allow to do
// like such as #[derive(Error)] before a struct or enum.
#[macro_use] #[macro_use]
extern crate justerror; extern crate justerror;
mod handlers; mod handlers;
use handlers::*; use handlers::{get_signup, post_signup, signup_success};
mod templates; mod templates;
mod user;
use user::User;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let session_store = MemoryStore::default(); let session_store = MemoryStore::default();
@ -36,41 +38,3 @@ async fn main() {
.await .await
.unwrap(); .unwrap();
} }
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct User {
pub username: String,
pub displayname: Option<String>,
pub email: String,
pub password: String,
pub pw_verify: String,
}
impl Debug for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let pw_check = if self.password == self.pw_verify {
"password matched"
} else {
"PASSWORD MISMATCH"
};
f.debug_struct("User")
.field("username", &self.username)
.field("displayname", &self.displayname)
.field("email", &self.email)
.field("pw-check", &pw_check)
.finish()
}
}
impl Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let uname = &self.username;
let dname = if let Some(ref n) = self.displayname {
n
} else {
""
};
let email = &self.email;
write!(f, "Username: {uname}\nDisplayname: {dname}\nEmail: {email}")
}
}

41
src/user.rs Normal file
View file

@ -0,0 +1,41 @@
use std::fmt::{Debug, Display};
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct User {
pub username: String,
pub displayname: Option<String>,
pub email: String,
pub password: String,
pub pw_verify: String,
}
impl Debug for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let pw_check = if self.password == self.pw_verify {
"password matched"
} else {
"PASSWORD MISMATCH"
};
f.debug_struct("User")
.field("username", &self.username)
.field("displayname", &self.displayname)
.field("email", &self.email)
.field("pw-check", &pw_check)
.finish()
}
}
impl Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let uname = &self.username;
let dname = if let Some(ref n) = self.displayname {
n
} else {
""
};
let email = &self.email;
write!(f, "Username: {uname}\nDisplayname: {dname}\nEmail: {email}")
}
}