This commit is contained in:
2023-05-10 01:02:37 +02:00
parent 9dad419dd2
commit 63a6b9d276
4 changed files with 24 additions and 24 deletions

View File

@@ -14,14 +14,14 @@ impl Inventory {
div id="pkglist-item-manager" { div id="pkglist-item-manager" {
div ."p-8" ."grid" ."grid-cols-4" ."gap-3" { div ."p-8" ."grid" ."grid-cols-4" ."gap-3" {
div ."col-span-2" { div ."col-span-2" {
({<InventoryCategoryList as Into<Markup>>::into(InventoryCategoryList::build(&categories).await?)}) ({<InventoryCategoryList as Into<Markup>>::into(InventoryCategoryList::build(&categories))})
} }
div ."col-span-2" { div ."col-span-2" {
h1 ."text-2xl" ."mb-5" ."text-center" { "Items" } h1 ."text-2xl" ."mb-5" ."text-center" { "Items" }
@if state.active_category_id.is_some() { @if state.active_category_id.is_some() {
({<InventoryItemList as Into<Markup>>::into(InventoryItemList::build(categories.iter().find(|category| category.active).unwrap().items()).await?)}) ({<InventoryItemList as Into<Markup>>::into(InventoryItemList::build(categories.iter().find(|category| category.active).unwrap().items()))})
} }
({<InventoryNewItemForm as Into<Markup>>::into(InventoryNewItemForm::build(&state, &categories).await?)}) ({<InventoryNewItemForm as Into<Markup>>::into(InventoryNewItemForm::build(&state, &categories))})
} }
} }
@@ -43,10 +43,10 @@ pub struct InventoryCategoryList {
} }
impl InventoryCategoryList { impl InventoryCategoryList {
pub async fn build(categories: &Vec<Category>) -> Result<Self, Error> { pub fn build(categories: &Vec<Category>) -> Self {
let biggest_category_weight: u32 = categories let biggest_category_weight: u32 = categories
.iter() .iter()
.map(|category| category.total_weight()) .map(Category::total_weight)
.max() .max()
.unwrap_or(1); .unwrap_or(1);
@@ -116,8 +116,8 @@ impl InventoryCategoryList {
format!( format!(
"width: {width}%;position:absolute;left:0;bottom:0;right:0;", "width: {width}%;position:absolute;left:0;bottom:0;right:0;",
width=( width=(
category.total_weight() as f32 f64::from(category.total_weight())
/ biggest_category_weight as f32 / f64::from(biggest_category_weight)
* 100.0 * 100.0
) )
) )
@@ -131,7 +131,7 @@ impl InventoryCategoryList {
} }
td ."border" ."p-0" ."m-0" { td ."border" ."p-0" ."m-0" {
p ."p-2" ."m-2" { p ."p-2" ."m-2" {
(categories.iter().map(|category| category.total_weight()).sum::<u32>().to_string()) (categories.iter().map(Category::total_weight).sum::<u32>().to_string())
} }
} }
} }
@@ -140,7 +140,7 @@ impl InventoryCategoryList {
} }
); );
Ok(Self { doc }) Self { doc }
} }
} }
@@ -155,7 +155,7 @@ pub struct InventoryItemList {
} }
impl InventoryItemList { impl InventoryItemList {
pub async fn build(items: &Vec<Item>) -> Result<Self, Error> { pub fn build(items: &Vec<Item>) -> Self {
let biggest_item_weight: u32 = items.iter().map(|item| item.weight).max().unwrap_or(1); let biggest_item_weight: u32 = items.iter().map(|item| item.weight).max().unwrap_or(1);
let doc = html!( let doc = html!(
div #items { div #items {
@@ -196,7 +196,7 @@ impl InventoryItemList {
position:absolute; position:absolute;
left:0; left:0;
bottom:0; bottom:0;
right:0;", width=(item.weight as f32 / biggest_item_weight as f32 * 100.0))) {} right:0;", width=(f64::from(item.weight) / f64::from(biggest_item_weight) * 100.0))) {}
} }
td td
."border" ."border"
@@ -222,7 +222,7 @@ impl InventoryItemList {
} }
); );
Ok(Self { doc }) Self { doc }
} }
} }
@@ -237,7 +237,7 @@ pub struct InventoryNewItemForm {
} }
impl InventoryNewItemForm { impl InventoryNewItemForm {
pub async fn build(state: &ClientState, categories: &Vec<Category>) -> Result<Self, Error> { pub fn build(state: &ClientState, categories: &Vec<Category>) -> Self {
let doc = html!( let doc = html!(
form form
@@ -335,7 +335,7 @@ impl InventoryNewItemForm {
} }
); );
Ok(Self { doc }) Self { doc }
} }
} }

View File

@@ -19,7 +19,7 @@ pub enum TopLevelPage {
} }
impl Root { impl Root {
pub fn build(body: Markup, active_page: TopLevelPage) -> Self { pub fn build(body: Markup, active_page: &TopLevelPage) -> Self {
let doc = html!( let doc = html!(
(DOCTYPE) (DOCTYPE)
html { html {

View File

@@ -103,7 +103,7 @@ async fn main() -> Result<(), sqlx::Error> {
async fn root() -> (StatusCode, Html<String>) { async fn root() -> (StatusCode, Html<String>) {
( (
StatusCode::OK, StatusCode::OK,
Html::from(Root::build(Home::build().into(), TopLevelPage::None).into_string()), Html::from(Root::build(Home::build().into(), &TopLevelPage::None).into_string()),
) )
} }
@@ -133,7 +133,7 @@ async fn inventory(
let mut categories = query("SELECT id,name,description FROM inventoryitemcategories") let mut categories = query("SELECT id,name,description FROM inventoryitemcategories")
.fetch(&state.database_pool) .fetch(&state.database_pool)
.map_ok(|row| row.try_into()) .map_ok(std::convert::TryInto::try_into)
.try_collect::<Vec<Result<Category, models::Error>>>() .try_collect::<Vec<Result<Category, models::Error>>>()
.await .await
// we have two error handling lines here. these are distinct errors // we have two error handling lines here. these are distinct errors
@@ -166,7 +166,7 @@ async fn inventory(
.await .await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Html::from(e.to_string())))? .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Html::from(e.to_string())))?
.into(), .into(),
TopLevelPage::Inventory, &TopLevelPage::Inventory,
) )
.into_string(), .into_string(),
), ),
@@ -178,7 +178,7 @@ async fn trips(
) -> Result<(StatusCode, Html<String>), (StatusCode, Html<String>)> { ) -> Result<(StatusCode, Html<String>), (StatusCode, Html<String>)> {
let trips = query("SELECT * FROM trips") let trips = query("SELECT * FROM trips")
.fetch(&state.database_pool) .fetch(&state.database_pool)
.map_ok(|row| row.try_into()) .map_ok(std::convert::TryInto::try_into)
.try_collect::<Vec<Result<Trip, models::Error>>>() .try_collect::<Vec<Result<Trip, models::Error>>>()
.await .await
// we have two error handling lines here. these are distinct errors // we have two error handling lines here. these are distinct errors
@@ -192,7 +192,7 @@ async fn trips(
Ok(( Ok((
StatusCode::OK, StatusCode::OK,
Html::from(Root::build(TripList::build(trips).into(), TopLevelPage::Trips).into_string()), Html::from(Root::build(TripList::build(trips).into(), &TopLevelPage::Trips).into_string()),
)) ))
} }

View File

@@ -17,10 +17,10 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Self::SqlError { description } => { Self::SqlError { description } => {
write!(f, "SQL error: {}", description) write!(f, "SQL error: {description}")
} }
Self::UuidError { description } => { Self::UuidError { description } => {
write!(f, "UUID error: {}", description) write!(f, "UUID error: {description}")
} }
} }
} }
@@ -29,7 +29,7 @@ impl fmt::Display for Error {
impl fmt::Debug for Error { impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// defer to Display // defer to Display
write!(f, "SQL error: {}", self) write!(f, "SQL error: {self}")
} }
} }
@@ -129,7 +129,7 @@ impl<'a> Category {
id = self.id id = self.id
)) ))
.fetch(&pool) .fetch(&pool)
.map_ok(|row| row.try_into()) .map_ok(std::convert::TryInto::try_into)
.try_collect::<Vec<Result<Item, Error>>>() .try_collect::<Vec<Result<Item, Error>>>()
.await? .await?
.into_iter() .into_iter()