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)]
struct Field {
pub struct Field {
widget: String,
label: Option<String>,
#[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<T: Into<Field>>(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