Skip to content

Commit

Permalink
frontend-utils: Add TableExt trait
Browse files Browse the repository at this point in the history
This moves the logic previously found in BookmarkWriter's `with_underlying_table`
into a common trait to be reused by other writers.
  • Loading branch information
sleepycatcoding authored and torokati44 committed Apr 4, 2024
1 parent e4c798f commit d8082eb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 3 additions & 16 deletions desktop/src/preferences/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SavedGlobalPreferences>);
Expand Down Expand Up @@ -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)
})
}
Expand Down
1 change: 1 addition & 0 deletions frontend-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ workspace = true
[dependencies]
toml_edit = { version = "0.22.9", features = ["parse"] }
url = { workspace = true }
tracing = { workspace = true }
1 change: 1 addition & 0 deletions frontend-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod bookmarks;
pub mod parse;
pub mod write;
22 changes: 22 additions & 0 deletions frontend-utils/src/write.rs
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit d8082eb

Please sign in to comment.