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

View file

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

View file

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

View file

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