diff --git a/Cargo.lock b/Cargo.lock index da022bc8bf1d..c6e4248304d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4286,6 +4286,7 @@ name = "ruffle_frontend_utils" version = "0.1.0" dependencies = [ "toml_edit 0.22.9", + "tracing", "url", ] diff --git a/desktop/src/preferences/write.rs b/desktop/src/preferences/write.rs index e3d11ad6ed91..11638c20b151 100644 --- a/desktop/src/preferences/write.rs +++ b/desktop/src/preferences/write.rs @@ -3,8 +3,9 @@ use crate::preferences::storage::StorageBackend; use crate::preferences::SavedGlobalPreferences; use ruffle_frontend_utils::bookmarks::{Bookmark, Bookmarks}; use ruffle_frontend_utils::parse::DocumentHolder; +use ruffle_frontend_utils::write::TableExt; use ruffle_render_wgpu::clap::{GraphicsBackend, PowerPreference}; -use toml_edit::{array, value, ArrayOfTables, DocumentMut, Table}; +use toml_edit::{value, ArrayOfTables, Table}; use unic_langid::LanguageIdentifier; pub struct PreferencesWriter<'a>(&'a mut DocumentHolder); @@ -83,22 +84,8 @@ impl<'a> BookmarksWriter<'a> { } fn with_underlying_table(&mut self, fun: impl FnOnce(&mut Bookmarks, &mut ArrayOfTables)) { - fn find_table(toml_document: &mut DocumentMut) -> &mut ArrayOfTables { - if toml_document.contains_array_of_tables("bookmark") { - return toml_document["bookmark"] - .as_array_of_tables_mut() - .expect("type was just verified"); - } - - tracing::warn!("missing or invalid bookmark array, recreating.."); - toml_document.insert("bookmark", array()); - toml_document["bookmark"] - .as_array_of_tables_mut() - .expect("type was just created") - } - self.0.edit(|values, toml_document| { - let table = find_table(toml_document); + let table = toml_document.get_or_create_array_of_tables("bookmark"); fun(values, table) }) } diff --git a/frontend-utils/Cargo.toml b/frontend-utils/Cargo.toml index 15759c8ccd78..abe2fdf7a006 100644 --- a/frontend-utils/Cargo.toml +++ b/frontend-utils/Cargo.toml @@ -13,3 +13,4 @@ workspace = true [dependencies] toml_edit = { version = "0.22.9", features = ["parse"] } url = { workspace = true } +tracing = { workspace = true } diff --git a/frontend-utils/src/lib.rs b/frontend-utils/src/lib.rs index b7a8d3e8f859..3ad509114445 100644 --- a/frontend-utils/src/lib.rs +++ b/frontend-utils/src/lib.rs @@ -1,2 +1,3 @@ pub mod bookmarks; pub mod parse; +pub mod write; diff --git a/frontend-utils/src/write.rs b/frontend-utils/src/write.rs new file mode 100644 index 000000000000..f0aea2e5bdb3 --- /dev/null +++ b/frontend-utils/src/write.rs @@ -0,0 +1,22 @@ +use toml_edit::{array, ArrayOfTables, Table}; + +pub trait TableExt { + /// Gets an existing array of tables, or creates a new one if it does not exist or type is different. + fn get_or_create_array_of_tables(&mut self, key: &str) -> &mut ArrayOfTables; +} + +impl TableExt for Table { + fn get_or_create_array_of_tables(&mut self, key: &str) -> &mut ArrayOfTables { + if self.contains_array_of_tables(key) { + return self[key] + .as_array_of_tables_mut() + .expect("type was just verified"); + } + + tracing::warn!("missing or invalid '{key}' array, recreating.."); + self.insert(key, array()); + self[key] + .as_array_of_tables_mut() + .expect("type was just created") + } +}