Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend-utils: Add TableExt trait #15870

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}