rename some templates, lean in to big-endian IDs

This commit is contained in:
Joe Ardent 2023-06-22 14:10:18 -07:00
parent 4211ead59e
commit 84d70336d3
5 changed files with 19 additions and 19 deletions

View file

@ -16,10 +16,10 @@ pub struct DbId(pub Ulid);
impl DbId {
pub fn bytes(&self) -> [u8; 16] {
self.as_be_bytes()
self.to_be_bytes()
}
pub fn as_be_bytes(&self) -> [u8; 16] {
pub fn to_be_bytes(self) -> [u8; 16] {
self.0 .0.to_be_bytes()
}
@ -102,7 +102,7 @@ impl<'r> Decode<'r, Sqlite> for DbId {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
let bytes = <&[u8] as Decode<Sqlite>>::decode(value)?;
let bytes: [u8; 16] = bytes.try_into().unwrap_or_default();
Ok(u128::from_ne_bytes(bytes).into())
Ok(u128::from_be_bytes(bytes).into())
}
}
@ -192,21 +192,21 @@ impl<'de> Visitor<'de> for DbIdVisitor {
where
A: serde::de::SeqAccess<'de>,
{
let mut bytes = [0u8; 16];
let mut raw_bytes_from_db = [0u8; 16];
let size = seq.size_hint().unwrap_or(0);
let mut count = 0;
while let Some(val) = seq.next_element()? {
if count > 15 {
break;
}
bytes[count] = val;
raw_bytes_from_db[count] = val;
count += 1;
}
if count != 16 || size > 16 {
let sz = if count < 16 { count } else { size };
Err(serde::de::Error::invalid_length(sz, &self))
} else {
Ok(u128::from_ne_bytes(bytes).into())
Ok(u128::from_be_bytes(raw_bytes_from_db).into())
}
}
}

View file

@ -74,7 +74,7 @@ pub async fn app(db_pool: sqlx::SqlitePool, session_secret: &[u8]) -> axum::Rout
//-************************************************************************
#[cfg(test)]
mod test {
use super::{CreateUserSuccess, MainPage, OptionalOptionalUser, User};
use super::{MainPage, OptionalOptionalUser, SignupSuccessPage, User};
#[test]
fn main_page_has_optional_user() {
@ -83,7 +83,7 @@ mod test {
#[test]
fn signup_success_has_no_optional_user() {
assert!(!CreateUserSuccess::default().has_optional_user());
assert!(!SignupSuccessPage::default().has_optional_user());
}
#[test]

View file

@ -11,7 +11,7 @@ use axum::{
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use crate::{AuthContext, LoginPage, LogoutPage, LogoutSuccess, User};
use crate::{AuthContext, LoginPage, LogoutPage, LogoutSuccessPage, User};
//-************************************************************************
// Constants
@ -101,13 +101,13 @@ pub async fn post_logout(mut auth: AuthContext) -> impl IntoResponse {
if auth.current_user.is_some() {
auth.logout().await;
}
LogoutSuccess
LogoutSuccessPage
}
#[cfg(test)]
mod test {
use crate::{
templates::{LoginPage, LogoutPage, LogoutSuccess, MainPage},
templates::{LoginPage, LogoutPage, LogoutSuccessPage, MainPage},
test_utils::{get_test_user, massage, server, FORM_CONTENT_TYPE},
};
@ -182,7 +182,7 @@ mod test {
let resp = s.post("/logout").await;
resp.assert_status_ok();
let body = std::str::from_utf8(resp.bytes()).unwrap();
let default = LogoutSuccess.to_string();
let default = LogoutSuccessPage.to_string();
assert_eq!(body, &default);
}
@ -213,7 +213,7 @@ mod test {
let resp = s.post("/logout").await;
let body = std::str::from_utf8(resp.bytes()).unwrap();
let default = LogoutSuccess.to_string();
let default = LogoutSuccessPage.to_string();
assert_eq!(body, &default);
}
}

View file

@ -11,7 +11,7 @@ use serde::Deserialize;
use sqlx::{query_as, SqlitePool};
use unicode_segmentation::UnicodeSegmentation;
use crate::{util::empty_string_as_none, CreateUserSuccess, DbId, SignupPage, User};
use crate::{util::empty_string_as_none, DbId, SignupPage, SignupSuccessPage, User};
pub(crate) const CREATE_QUERY: &str =
"insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)";
@ -134,7 +134,7 @@ pub async fn get_signup_success(
.unwrap_or_default()
};
let mut resp = CreateUserSuccess(user.clone()).into_response();
let mut resp = SignupSuccessPage(user.clone()).into_response();
if user.username.is_empty() || id.is_nil() {
// redirect to front page if we got here without a valid witch ID
@ -213,7 +213,7 @@ mod test {
use crate::{
db::get_db_pool,
templates::{CreateUserSuccess, SignupPage},
templates::{SignupPage, SignupSuccessPage},
test_utils::{get_test_user, insert_user, massage, server_with_pool, FORM_CONTENT_TYPE},
User,
};
@ -264,7 +264,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 = SignupSuccessPage(user).to_string();
assert_eq!(&expected, body);
}

View file

@ -17,7 +17,7 @@ pub struct SignupPage {
Debug, Clone, Template, Default, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser,
)]
#[template(path = "signup_success.html")]
pub struct CreateUserSuccess(pub User);
pub struct SignupSuccessPage(pub User);
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "login_page.html")]
@ -32,7 +32,7 @@ pub struct LogoutPage;
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "logout_success.html")]
pub struct LogoutSuccess;
pub struct LogoutSuccessPage;
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq, OptionalOptionalUser)]
#[template(path = "index.html")]