refactor: allow set_widget to take Field as well

This commit is contained in:
Gabor Körber 2024-02-02 21:55:29 +01:00
parent 6e0a71b7de
commit fdbc37db06

View File

@ -143,7 +143,7 @@ pub mod repository {
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct Field { pub struct Field {
widget: String, widget: String,
label: Option<String>, label: Option<String>,
#[serde(rename = "type")] #[serde(rename = "type")]
@ -301,8 +301,9 @@ pub mod repository {
self self
} }
// set field for a given key. pub fn set_widget<T: Into<Field>>(mut self, name: &str, item: T) -> Self {
pub fn set_widget(mut self, name: &str, widget: Widget) -> 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 // Find the index of the existing entry with the same name, if it exists
let pos = self let pos = self
.fields .fields
@ -311,12 +312,10 @@ pub mod repository {
match pos { match pos {
Some(index) => { Some(index) => {
// If found, replace the `Field` part of the tuple self.fields[index].1 = field;
self.fields[index].1 = Field::from(widget);
} }
None => { None => {
// If not found, add a new entry self.fields.push((name.to_owned(), field));
self.fields.push((name.to_owned(), Field::from(widget)));
} }
} }
self self