desktop: Refactor preferences system to make it easier to reuse
The goal of this refactor is to make it easier to load other TOML files similarly how preferences are loaded currently.
This commit is contained in:
parent
fd5646d160
commit
f657208860
|
@ -1,14 +1,15 @@
|
|||
use crate::preferences::SavedGlobalPreferences;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
use toml_edit::{DocumentMut, Item, TableLike};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct ParseResult {
|
||||
pub result: SavedGlobalPreferences,
|
||||
pub struct ParseResult<T: PartialEq + fmt::Debug> {
|
||||
pub result: T,
|
||||
pub warnings: Vec<String>,
|
||||
}
|
||||
|
||||
impl ParseResult {
|
||||
impl<T: fmt::Debug + PartialEq> ParseResult<T> {
|
||||
fn add_warning(&mut self, message: String) {
|
||||
self.warnings.push(message);
|
||||
}
|
||||
|
@ -169,7 +170,7 @@ impl<'a> ReadExt<'a> for dyn TableLike + 'a {
|
|||
/// Default values are used wherever an unknown or invalid value is found;
|
||||
/// this is to support the case of, for example, a later version having different supported
|
||||
/// backends than an older version.
|
||||
pub fn read_preferences(input: &str) -> (ParseResult, DocumentMut) {
|
||||
pub fn read_preferences(input: &str) -> (ParseResult<SavedGlobalPreferences>, DocumentMut) {
|
||||
let mut result = ParseResult {
|
||||
result: Default::default(),
|
||||
warnings: vec![],
|
||||
|
|
|
@ -54,32 +54,45 @@ impl<'a> PreferencesWriter<'a> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::preferences::read::read_preferences;
|
||||
use fluent_templates::loader::langid;
|
||||
|
||||
fn parse(input: &str) -> PreferencesAndDocument {
|
||||
let (result, document) = read_preferences(input);
|
||||
PreferencesAndDocument {
|
||||
macro_rules! define_serialization_test_helpers {
|
||||
($read_method:ident, $doc_struct:ident, $writer:ident) => {
|
||||
fn parse(input: &str) -> $doc_struct {
|
||||
let (result, document) = $read_method(input);
|
||||
$doc_struct {
|
||||
toml_document: document,
|
||||
values: result.result,
|
||||
}
|
||||
}
|
||||
|
||||
fn check_roundtrip(preferences: &mut PreferencesAndDocument) {
|
||||
let read_result = read_preferences(&preferences.toml_document.to_string());
|
||||
fn check_roundtrip(preferences: &mut $doc_struct) {
|
||||
let read_result = $read_method(&preferences.toml_document.to_string());
|
||||
assert_eq!(
|
||||
preferences.values, read_result.0.result,
|
||||
"roundtrip failed: expected != actual"
|
||||
);
|
||||
}
|
||||
|
||||
fn test(original: &str, fun: impl FnOnce(&mut PreferencesWriter), expected: &str) {
|
||||
fn test(original: &str, fun: impl FnOnce(&mut $writer), expected: &str) {
|
||||
let mut preferences = parse(original);
|
||||
let mut writer = PreferencesWriter::new(&mut preferences);
|
||||
let mut writer = $writer::new(&mut preferences);
|
||||
fun(&mut writer);
|
||||
check_roundtrip(&mut preferences);
|
||||
assert_eq!(expected, preferences.toml_document.to_string());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
mod preferences {
|
||||
use super::*;
|
||||
use crate::preferences::read::read_preferences;
|
||||
|
||||
define_serialization_test_helpers!(
|
||||
read_preferences,
|
||||
PreferencesAndDocument,
|
||||
PreferencesWriter
|
||||
);
|
||||
|
||||
#[test]
|
||||
fn set_graphics_backend() {
|
||||
|
@ -175,3 +188,4 @@ mod tests {
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue