mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
split config.rs up
This commit is contained in:
parent
545cb76018
commit
c0e939ceb6
5 changed files with 129 additions and 103 deletions
67
rslib/src/config/bool.rs
Normal file
67
rslib/src/config/bool.rs
Normal file
|
@ -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::<BoolLike, _>(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)
|
||||
}
|
||||
}
|
|
@ -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<T>
|
||||
|
@ -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::<BoolLike, _>(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)
|
||||
}
|
28
rslib/src/config/schema11.rs
Normal file
28
rslib/src/config/schema11.rs
Normal file
|
@ -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()
|
||||
}
|
28
rslib/src/config/string.rs
Normal file
28
rslib/src/config/string.rs
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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};
|
||||
|
|
Loading…
Reference in a new issue