Make InputGroup value a reference

It is somewhat cumbersome to manually cast the value to &dyn Display
when mapping on an Option, but I guess it is more efficient.
This commit is contained in:
Simon Bruder 2024-07-21 15:03:29 +02:00
parent c8bc885919
commit f21f8dfa5e
Signed by: simon
GPG key ID: 347FF8699CDA0776
4 changed files with 26 additions and 23 deletions

View file

@ -3,6 +3,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::collections::HashMap;
use std::fmt::Display;
use actix_identity::Identity;
use actix_web::{error, get, post, web, HttpRequest, Responder};
@ -313,7 +314,7 @@ async fn add_form(
name: "name",
title: "Name",
optional: true,
value: form.name.clone(),
value: form.name.as_ref().map(|s| s as &dyn Display),
..Default::default()
})
(forms::InputGroup {
@ -321,7 +322,7 @@ async fn add_form(
name: "class",
title: "Class",
required: true,
value: form.class.map(|id| id.to_string()),
value: form.class.as_ref().map(|id| id as &dyn Display),
datalist: Some(&datalist_item_classes),
..Default::default()
})
@ -330,7 +331,7 @@ async fn add_form(
name: "parent",
title: "Parent",
optional: true,
value: form.parent.map(|id| id.to_string()),
value: form.parent.as_ref().map(|id| id as &dyn Display),
datalist: Some(&datalist_items),
..Default::default()
})
@ -340,7 +341,7 @@ async fn add_form(
title: "Original Packaging",
optional: true,
disabled: true,
value: form.original_packaging.map(|id| id.to_string()),
value: form.original_packaging.as_ref().map(|id| id as &dyn Display),
datalist: Some(&datalist_items),
..Default::default()
})
@ -348,7 +349,7 @@ async fn add_form(
r#type: forms::InputType::Textarea,
name: "description",
title: "Description ",
value: form.description.clone(),
value: form.description.as_ref().map(|s| s as &dyn Display),
..Default::default()
})
@ -463,7 +464,7 @@ async fn edit_form(
title: "UUID",
required: true,
disabled: true,
value: Some(item.id.to_string()),
value: Some(&item.id),
..Default::default()
})
(forms::InputGroup {
@ -472,7 +473,7 @@ async fn edit_form(
title: "Name",
optional: true,
disabled: item.name.is_none(),
value: item.name,
value: item.name.as_ref().map(|s| s as &dyn Display),
..Default::default()
})
(forms::InputGroup {
@ -480,7 +481,7 @@ async fn edit_form(
name: "class",
title: "Class",
required: true,
value: Some(item.class.to_string()),
value: Some(&item.class),
datalist: Some(&datalist_item_classes),
..Default::default()
})
@ -489,7 +490,7 @@ async fn edit_form(
name: "parent",
title: "Parent",
optional: true,
value: item.parent.map(|id| id.to_string()),
value: item.parent.as_ref().map(|id| id as &dyn Display),
disabled: item.parent.is_none(),
datalist: Some(&datalist_items),
..Default::default()
@ -499,7 +500,7 @@ async fn edit_form(
name: "original_packaging",
title: "Original Packaging",
optional: true,
value: item.original_packaging.map(|id| id.to_string()),
value: item.original_packaging.as_ref().map(|id| id as &dyn Display),
disabled: item.original_packaging.is_none(),
datalist: Some(&datalist_items),
..Default::default()
@ -508,7 +509,7 @@ async fn edit_form(
r#type: forms::InputType::Textarea,
name: "description",
title: "Description ",
value: Some(item.description),
value: Some(&item.description),
..Default::default()
})

View file

@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::fmt::Display;
use actix_identity::Identity;
use actix_web::{error, get, post, web, Responder};
use maud::html;
@ -258,7 +260,7 @@ async fn add_form(
name: "name",
title: "Name",
required: true,
value: form.name.clone(),
value: form.name.as_ref().map(|s| s as &dyn Display),
..Default::default()
})
(forms::InputGroup {
@ -267,7 +269,7 @@ async fn add_form(
title: "Parent",
optional: true,
disabled: form.parent.is_none(),
value: form.parent.map(|id| id.to_string()),
value: form.parent.as_ref().map(|id| id as &dyn Display),
datalist: Some(&datalist_item_classes),
..Default::default()
})
@ -275,7 +277,7 @@ async fn add_form(
r#type: forms::InputType::Textarea,
name: "description",
title: "Description ",
value: form.description.clone(),
value: form.description.as_ref().map(|s| s as &dyn Display),
..Default::default()
})
@ -341,7 +343,7 @@ async fn edit_form(
title: "UUID",
disabled: true,
required: true,
value: Some(item_class.id.to_string()),
value: Some(&item_class.id),
..Default::default()
})
(forms::InputGroup {
@ -349,7 +351,7 @@ async fn edit_form(
name: "name",
title: "Name",
required: true,
value: Some(item_class.name),
value: Some(&item_class.name),
..Default::default()
})
(forms::InputGroup {
@ -358,7 +360,7 @@ async fn edit_form(
title: "Parent",
optional: true,
disabled: item_class.parent.is_none(),
value: item_class.parent.map(|id| id.to_string()),
value: item_class.parent.as_ref().map(|id| id as &dyn Display),
datalist: Some(&datalist_item_classes),
..Default::default()
})
@ -366,7 +368,7 @@ async fn edit_form(
r#type: forms::InputType::Textarea,
name: "description",
title: "Description ",
value: Some(item_class.description),
value: Some(&item_class.description),
..Default::default()
})

View file

@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::fmt;
use std::fmt::{self, Display};
use maud::{html, Markup, Render};
@ -14,7 +14,7 @@ pub enum InputType {
Textarea,
}
impl fmt::Display for InputType {
impl Display for InputType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Text => write!(f, "text"),
@ -30,7 +30,7 @@ pub struct InputGroup<'a> {
pub required: bool,
pub optional: bool,
pub disabled: bool,
pub value: Option<String>,
pub value: Option<&'a dyn Display>,
pub datalist: Option<&'a Datalist>,
}
@ -60,7 +60,7 @@ impl InputGroup<'_> {
required[self.required || force_required]
disabled[self.disabled]
rows="5" // FIXME hardcoded
{ (self.value.as_ref().unwrap_or(&"".to_string())) },
{ (self.value.unwrap_or(&"")) },
_ => input
.form-control
#(self.name)

View file

@ -68,7 +68,7 @@ fn footer() -> Markup {
pub struct TemplateConfig<'a> {
pub path: &'a str,
pub title: Option<&'a str>,
pub page_title: Option<Box<dyn Render>>,
pub page_title: Option<Box<dyn Render + 'a>>,
pub page_actions: Vec<PageActionGroup>,
pub extra_css: Vec<Css<'a>>,
pub extra_js: Vec<Js<'a>>,