From c0e939ceb62428ca0bbd73cb48f859f2002b46ae Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 8 Mar 2021 11:59:51 +1000 Subject: [PATCH] split config.rs up --- rslib/src/config/bool.rs | 67 ++++++++++++++++ rslib/src/{config.rs => config/mod.rs} | 107 ++----------------------- rslib/src/config/schema11.rs | 28 +++++++ rslib/src/config/string.rs | 28 +++++++ rslib/src/storage/sqlite.rs | 2 +- 5 files changed, 129 insertions(+), 103 deletions(-) create mode 100644 rslib/src/config/bool.rs rename rslib/src/{config.rs => config/mod.rs} (73%) create mode 100644 rslib/src/config/schema11.rs create mode 100644 rslib/src/config/string.rs diff --git a/rslib/src/config/bool.rs b/rslib/src/config/bool.rs new file mode 100644 index 000000000..150eeaacb --- /dev/null +++ b/rslib/src/config/bool.rs @@ -0,0 +1,67 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +use crate::prelude::*; +use serde_aux::field_attributes::deserialize_bool_from_anything; +use serde_derive::Deserialize; +use strum::IntoStaticStr; + +#[derive(Debug, Clone, Copy, IntoStaticStr)] +#[strum(serialize_all = "camelCase")] +pub enum BoolKey { + CardCountsSeparateInactive, + CollapseCardState, + CollapseDecks, + CollapseFlags, + CollapseNotetypes, + CollapseSavedSearches, + CollapseTags, + CollapseToday, + FutureDueShowBacklog, + PreviewBothSides, + Sched2021, + + #[strum(to_string = "sortBackwards")] + BrowserSortBackwards, + #[strum(to_string = "normalize_note_text")] + NormalizeNoteText, + #[strum(to_string = "dayLearnFirst")] + ShowDayLearningCardsFirst, + #[strum(to_string = "estTimes")] + ShowIntervalsAboveAnswerButtons, + #[strum(to_string = "dueCounts")] + ShowRemainingDueCountsInStudy, + #[strum(to_string = "addToCur")] + AddingDefaultsToCurrentDeck, +} + +/// This is a workaround for old clients that used ints to represent boolean +/// values. For new config items, prefer using a bool directly. +#[derive(Deserialize, Default)] +struct BoolLike(#[serde(deserialize_with = "deserialize_bool_from_anything")] bool); + +impl Collection { + pub(crate) fn get_bool(&self, key: BoolKey) -> bool { + match key { + BoolKey::BrowserSortBackwards => { + // older clients were storing this as an int + self.get_config_default::(BoolKey::BrowserSortBackwards) + .0 + } + + // some keys default to true + BoolKey::AddingDefaultsToCurrentDeck + | BoolKey::FutureDueShowBacklog + | BoolKey::ShowRemainingDueCountsInStudy + | BoolKey::CardCountsSeparateInactive + | BoolKey::NormalizeNoteText => self.get_config_optional(key).unwrap_or(true), + + // other options default to false + other => self.get_config_default(other), + } + } + + pub(crate) fn set_bool(&self, key: BoolKey, value: bool) -> Result<()> { + self.set_config(key, &value) + } +} diff --git a/rslib/src/config.rs b/rslib/src/config/mod.rs similarity index 73% rename from rslib/src/config.rs rename to rslib/src/config/mod.rs index fbc12d5f6..0dd795794 100644 --- a/rslib/src/config.rs +++ b/rslib/src/config/mod.rs @@ -1,42 +1,21 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +mod bool; +pub(crate) mod schema11; +mod string; + +pub use self::{bool::BoolKey, string::StringKey}; use crate::{ collection::Collection, decks::DeckID, err::Result, notetype::NoteTypeID, timestamp::TimestampSecs, }; use serde::{de::DeserializeOwned, Serialize}; -use serde_aux::field_attributes::deserialize_bool_from_anything; use serde_derive::Deserialize; -use serde_json::json; use serde_repr::{Deserialize_repr, Serialize_repr}; use slog::warn; use strum::IntoStaticStr; -/// These items are expected to exist in schema 11. When adding -/// new config variables, you do not need to add them here - -/// just create an accessor function below with an appropriate -/// default on missing/invalid values instead. -pub(crate) fn schema11_config_as_string() -> String { - let obj = json!({ - "activeDecks": [1], - "curDeck": 1, - "newSpread": 0, - "collapseTime": 1200, - "timeLim": 0, - "estTimes": true, - "dueCounts": true, - "curModel": null, - "nextPos": 1, - "sortType": "noteFld", - "sortBackwards": false, - "addToCur": true, - "dayLearnFirst": false, - "schedVer": 1, - }); - serde_json::to_string(&obj).unwrap() -} - #[derive(IntoStaticStr)] #[strum(serialize_all = "camelCase")] pub(crate) enum ConfigKey { @@ -65,51 +44,12 @@ pub(crate) enum ConfigKey { SchedulerVersion, } -#[derive(Debug, Clone, Copy, IntoStaticStr)] -#[strum(serialize_all = "camelCase")] -pub enum BoolKey { - CardCountsSeparateInactive, - CollapseCardState, - CollapseDecks, - CollapseFlags, - CollapseNotetypes, - CollapseSavedSearches, - CollapseTags, - CollapseToday, - FutureDueShowBacklog, - PreviewBothSides, - Sched2021, - - #[strum(to_string = "sortBackwards")] - BrowserSortBackwards, - #[strum(to_string = "normalize_note_text")] - NormalizeNoteText, - #[strum(to_string = "dayLearnFirst")] - ShowDayLearningCardsFirst, - #[strum(to_string = "estTimes")] - ShowIntervalsAboveAnswerButtons, - #[strum(to_string = "dueCounts")] - ShowRemainingDueCountsInStudy, -} - -#[derive(Debug, Clone, Copy, IntoStaticStr)] -#[strum(serialize_all = "camelCase")] -pub enum StringKey { - SetDueBrowser, - SetDueReviewer, -} - #[derive(PartialEq, Serialize_repr, Deserialize_repr, Clone, Copy, Debug)] #[repr(u8)] pub(crate) enum SchedulerVersion { V1 = 1, V2 = 2, } -/// This is a workaround for old clients that used ints to represent boolean -/// values. For new config items, prefer using a bool directly. -#[derive(Deserialize, Default)] -struct BoolLike(#[serde(deserialize_with = "deserialize_bool_from_anything")] bool); - impl Collection { /// Get config item, returning None if missing/invalid. pub(crate) fn get_config_optional<'a, T, K>(&self, key: K) -> Option @@ -152,43 +92,6 @@ impl Collection { self.storage.remove_config(key.into()) } - pub(crate) fn get_bool(&self, key: BoolKey) -> bool { - match key { - BoolKey::BrowserSortBackwards => { - // older clients were storing this as an int - self.get_config_default::(BoolKey::BrowserSortBackwards) - .0 - } - - // some keys default to true - BoolKey::FutureDueShowBacklog - | BoolKey::ShowRemainingDueCountsInStudy - | BoolKey::CardCountsSeparateInactive - | BoolKey::NormalizeNoteText => self.get_config_optional(key).unwrap_or(true), - - // other options default to false - other => self.get_config_default(other), - } - } - - pub(crate) fn set_bool(&self, key: BoolKey, value: bool) -> Result<()> { - self.set_config(key, &value) - } - - pub(crate) fn get_string(&self, key: StringKey) -> String { - let default = match key { - StringKey::SetDueBrowser => "0", - StringKey::SetDueReviewer => "1", - // other => "", - }; - self.get_config_optional(key) - .unwrap_or_else(|| default.to_string()) - } - - pub(crate) fn set_string(&self, key: StringKey, val: &str) -> Result<()> { - self.set_config(key, &val) - } - pub(crate) fn get_browser_sort_kind(&self) -> SortKind { self.get_config_default(ConfigKey::BrowserSortKind) } diff --git a/rslib/src/config/schema11.rs b/rslib/src/config/schema11.rs new file mode 100644 index 000000000..e210a5d12 --- /dev/null +++ b/rslib/src/config/schema11.rs @@ -0,0 +1,28 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +use serde_json::json; + +/// These items are expected to exist in schema 11. When adding +/// new config variables, you do not need to add them here - +/// just create an accessor function below with an appropriate +/// default on missing/invalid values instead. +pub(crate) fn schema11_config_as_string() -> String { + let obj = json!({ + "activeDecks": [1], + "curDeck": 1, + "newSpread": 0, + "collapseTime": 1200, + "timeLim": 0, + "estTimes": true, + "dueCounts": true, + "curModel": null, + "nextPos": 1, + "sortType": "noteFld", + "sortBackwards": false, + "addToCur": true, + "dayLearnFirst": false, + "schedVer": 1, + }); + serde_json::to_string(&obj).unwrap() +} diff --git a/rslib/src/config/string.rs b/rslib/src/config/string.rs new file mode 100644 index 000000000..d39f371d8 --- /dev/null +++ b/rslib/src/config/string.rs @@ -0,0 +1,28 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +use crate::prelude::*; +use strum::IntoStaticStr; + +#[derive(Debug, Clone, Copy, IntoStaticStr)] +#[strum(serialize_all = "camelCase")] +pub enum StringKey { + SetDueBrowser, + SetDueReviewer, +} + +impl Collection { + pub(crate) fn get_string(&self, key: StringKey) -> String { + let default = match key { + StringKey::SetDueBrowser => "0", + StringKey::SetDueReviewer => "1", + // other => "", + }; + self.get_config_optional(key) + .unwrap_or_else(|| default.to_string()) + } + + pub(crate) fn set_string(&self, key: StringKey, val: &str) -> Result<()> { + self.set_config(key, &val) + } +} diff --git a/rslib/src/storage/sqlite.rs b/rslib/src/storage/sqlite.rs index 99c41ef64..bbc0e0ff0 100644 --- a/rslib/src/storage/sqlite.rs +++ b/rslib/src/storage/sqlite.rs @@ -1,7 +1,7 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -use crate::config::schema11_config_as_string; +use crate::config::schema11::schema11_config_as_string; use crate::err::Result; use crate::err::{AnkiError, DBErrorKind}; use crate::timestamp::{TimestampMillis, TimestampSecs};