From fdbc37db06d9ff1e96f39aa53d84d2e13bf75aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Fri, 2 Feb 2024 21:55:29 +0100 Subject: [PATCH] refactor: allow set_widget to take Field as well --- src/admin/domain.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/admin/domain.rs b/src/admin/domain.rs index 826662a..f9134ca 100644 --- a/src/admin/domain.rs +++ b/src/admin/domain.rs @@ -143,7 +143,7 @@ pub mod repository { } #[derive(Debug, Serialize)] - struct Field { + pub struct Field { widget: String, label: Option, #[serde(rename = "type")] @@ -301,8 +301,9 @@ pub mod repository { self } - // set field for a given key. - pub fn set_widget(mut self, name: &str, widget: Widget) -> Self { + pub fn set_widget>(mut self, name: &str, item: T) -> Self { + let field = item.into(); // Convert the input into a Field + // Find the index of the existing entry with the same name, if it exists let pos = self .fields @@ -311,12 +312,10 @@ pub mod repository { match pos { Some(index) => { - // If found, replace the `Field` part of the tuple - self.fields[index].1 = Field::from(widget); + self.fields[index].1 = field; } None => { - // If not found, add a new entry - self.fields.push((name.to_owned(), Field::from(widget))); + self.fields.push((name.to_owned(), field)); } } self