From f18d890beaf0027a884ce57e44191df4d90ca47e Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Thu, 4 Apr 2024 02:20:09 +0200 Subject: [PATCH] frontend-utils: Move Bookmark from desktop --- Cargo.lock | 1 + desktop/src/gui/bookmarks_dialog.rs | 3 ++- desktop/src/preferences.rs | 20 +++----------------- desktop/src/preferences/read.rs | 15 ++++++++------- desktop/src/preferences/write.rs | 17 +++++++---------- frontend-utils/Cargo.toml | 1 + frontend-utils/src/bookmarks.rs | 17 +++++++++++++++++ frontend-utils/src/lib.rs | 1 + frontend-utils/src/parse.rs | 2 +- 9 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 frontend-utils/src/bookmarks.rs diff --git a/Cargo.lock b/Cargo.lock index 35493036d..da022bc8b 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", + "url", ] [[package]] diff --git a/desktop/src/gui/bookmarks_dialog.rs b/desktop/src/gui/bookmarks_dialog.rs index 3b19d6f71..d3baf8d9e 100644 --- a/desktop/src/gui/bookmarks_dialog.rs +++ b/desktop/src/gui/bookmarks_dialog.rs @@ -1,8 +1,9 @@ use crate::gui::text; use crate::gui::widgets::PathOrUrlField; -use crate::preferences::{Bookmark, GlobalPreferences}; +use crate::preferences::GlobalPreferences; use egui::{Align2, Button, Grid, Label, Layout, Sense, Ui, Widget, Window}; use egui_extras::{Column, TableBuilder}; +use ruffle_frontend_utils::bookmarks::Bookmark; use unic_langid::LanguageIdentifier; use url::Url; diff --git a/desktop/src/preferences.rs b/desktop/src/preferences.rs index c3d17181e..e4e3d0e63 100644 --- a/desktop/src/preferences.rs +++ b/desktop/src/preferences.rs @@ -9,12 +9,12 @@ use crate::preferences::read::{read_bookmarks, read_preferences}; use crate::preferences::write::{BookmarksWriter, PreferencesWriter}; use anyhow::{Context, Error}; use ruffle_core::backend::ui::US_ENGLISH; +use ruffle_frontend_utils::bookmarks::Bookmarks; use ruffle_frontend_utils::parse::DocumentHolder; use ruffle_render_wgpu::clap::{GraphicsBackend, PowerPreference}; use std::sync::{Arc, Mutex}; use sys_locale::get_locale; use unic_langid::LanguageIdentifier; -use url::Url; /// The preferences that relate to the application itself. /// @@ -38,7 +38,7 @@ pub struct GlobalPreferences { /// The actual, mutable user preferences that are persisted to disk. preferences: Arc>>, - bookmarks: Arc>>>, + bookmarks: Arc>>, } impl GlobalPreferences { @@ -136,7 +136,7 @@ impl GlobalPreferences { .filename_pattern } - pub fn bookmarks(&self, fun: impl FnOnce(&Vec)) { + pub fn bookmarks(&self, fun: impl FnOnce(&Bookmarks)) { fun(&self.bookmarks.lock().expect("Bookmarks is not reentrant")) } @@ -222,17 +222,3 @@ pub struct LogPreferences { pub struct StoragePreferences { pub backend: storage::StorageBackend, } - -pub static INVALID_URL: &str = "invalid:///"; - -#[derive(Debug, PartialEq)] -pub struct Bookmark { - pub url: Url, - pub name: String, -} - -impl Bookmark { - pub fn is_invalid(&self) -> bool { - self.url.as_str() == INVALID_URL - } -} diff --git a/desktop/src/preferences/read.rs b/desktop/src/preferences/read.rs index 3df1e775a..88972248a 100644 --- a/desktop/src/preferences/read.rs +++ b/desktop/src/preferences/read.rs @@ -1,4 +1,5 @@ -use crate::preferences::{Bookmark, SavedGlobalPreferences}; +use crate::preferences::SavedGlobalPreferences; +use ruffle_frontend_utils::bookmarks::{Bookmark, Bookmarks}; use ruffle_frontend_utils::parse::{DocumentHolder, ParseContext, ParseResult, ReadExt}; use std::str::FromStr; use toml_edit::DocumentMut; @@ -68,7 +69,7 @@ pub fn read_preferences(input: &str) -> ParseResult { } } -pub fn read_bookmarks(input: &str) -> ParseResult> { +pub fn read_bookmarks(input: &str) -> ParseResult { let document = match input.parse::() { Ok(document) => document, Err(e) => { @@ -86,7 +87,7 @@ pub fn read_bookmarks(input: &str) -> ParseResult> { for bookmark in bookmarks.iter() { let url = match bookmark.parse_from_str(cx, "url") { Some(value) => value, - None => url::Url::parse(crate::preferences::INVALID_URL) + None => url::Url::parse(ruffle_frontend_utils::bookmarks::INVALID_URL) .expect("Url is constant and valid"), }; @@ -432,7 +433,7 @@ mod tests { let result = read_bookmarks("[[bookmark]]"); assert_eq!( &vec![Bookmark { - url: Url::parse(crate::preferences::INVALID_URL).unwrap(), + url: Url::parse(ruffle_frontend_utils::bookmarks::INVALID_URL).unwrap(), name: "".to_string(), }], result.values() @@ -442,7 +443,7 @@ mod tests { let result = read_bookmarks("[[bookmark]]\nurl = \"invalid\""); assert_eq!( &vec![Bookmark { - url: Url::parse(crate::preferences::INVALID_URL).unwrap(), + url: Url::parse(ruffle_frontend_utils::bookmarks::INVALID_URL).unwrap(), name: "".to_string(), }], result.values() @@ -512,11 +513,11 @@ mod tests { name: "example.swf".to_string(), }, Bookmark { - url: Url::parse(crate::preferences::INVALID_URL).unwrap(), + url: Url::parse(ruffle_frontend_utils::bookmarks::INVALID_URL).unwrap(), name: "".to_string(), }, Bookmark { - url: Url::parse(crate::preferences::INVALID_URL).unwrap(), + url: Url::parse(ruffle_frontend_utils::bookmarks::INVALID_URL).unwrap(), name: "".to_string(), }, Bookmark { diff --git a/desktop/src/preferences/write.rs b/desktop/src/preferences/write.rs index c7d7287cc..e3d11ad6e 100644 --- a/desktop/src/preferences/write.rs +++ b/desktop/src/preferences/write.rs @@ -1,6 +1,7 @@ use crate::log::FilenamePattern; use crate::preferences::storage::StorageBackend; -use crate::preferences::{Bookmark, SavedGlobalPreferences}; +use crate::preferences::SavedGlobalPreferences; +use ruffle_frontend_utils::bookmarks::{Bookmark, Bookmarks}; use ruffle_frontend_utils::parse::DocumentHolder; use ruffle_render_wgpu::clap::{GraphicsBackend, PowerPreference}; use toml_edit::{array, value, ArrayOfTables, DocumentMut, Table}; @@ -74,14 +75,14 @@ impl<'a> PreferencesWriter<'a> { } } -pub struct BookmarksWriter<'a>(&'a mut DocumentHolder>); +pub struct BookmarksWriter<'a>(&'a mut DocumentHolder); impl<'a> BookmarksWriter<'a> { - pub(super) fn new(bookmarks: &'a mut DocumentHolder>) -> Self { + pub(super) fn new(bookmarks: &'a mut DocumentHolder) -> Self { Self(bookmarks) } - fn with_underlying_table(&mut self, fun: impl FnOnce(&mut Vec, &mut ArrayOfTables)) { + 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"] @@ -102,11 +103,7 @@ impl<'a> BookmarksWriter<'a> { }) } - fn with_bookmark_table( - &mut self, - index: usize, - fun: impl FnOnce(&mut Vec, &mut Table), - ) { + fn with_bookmark_table(&mut self, index: usize, fun: impl FnOnce(&mut Bookmarks, &mut Table)) { self.with_underlying_table(|values, array_of_tables| { let table = array_of_tables .get_mut(index) @@ -305,7 +302,7 @@ mod tests { use std::str::FromStr; use url::Url; - define_serialization_test_helpers!(read_bookmarks, Vec, BookmarksWriter); + define_serialization_test_helpers!(read_bookmarks, Bookmarks, BookmarksWriter); #[test] fn add_bookmark() { diff --git a/frontend-utils/Cargo.toml b/frontend-utils/Cargo.toml index ef104c13d..1034e1f2d 100644 --- a/frontend-utils/Cargo.toml +++ b/frontend-utils/Cargo.toml @@ -12,3 +12,4 @@ workspace = true [dependencies] toml_edit = { version = "0.22.9", features = ["parse"] } +url = "2.5.0" diff --git a/frontend-utils/src/bookmarks.rs b/frontend-utils/src/bookmarks.rs new file mode 100644 index 000000000..78765a7cc --- /dev/null +++ b/frontend-utils/src/bookmarks.rs @@ -0,0 +1,17 @@ +use url::Url; + +pub static INVALID_URL: &str = "invalid:///"; + +#[derive(Debug, PartialEq)] +pub struct Bookmark { + pub url: Url, + pub name: String, +} + +impl Bookmark { + pub fn is_invalid(&self) -> bool { + self.url.as_str() == INVALID_URL + } +} + +pub type Bookmarks = Vec; diff --git a/frontend-utils/src/lib.rs b/frontend-utils/src/lib.rs index ea868482d..b7a8d3e8f 100644 --- a/frontend-utils/src/lib.rs +++ b/frontend-utils/src/lib.rs @@ -1 +1,2 @@ +pub mod bookmarks; pub mod parse; diff --git a/frontend-utils/src/parse.rs b/frontend-utils/src/parse.rs index 0d1cbc704..ba76651e1 100644 --- a/frontend-utils/src/parse.rs +++ b/frontend-utils/src/parse.rs @@ -101,7 +101,7 @@ impl ParseResult { pub struct ParseContext { pub warnings: Vec, /// Path of the current item being parsed - pub path: Vec<&'static str>, + path: Vec<&'static str>, } impl ParseContext {