native stringkey

This commit is contained in:
Damien Elmes 2021-03-07 23:56:04 +10:00
parent 1dea8babee
commit 545cb76018
3 changed files with 52 additions and 43 deletions

View File

@ -1,22 +1,35 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::{backend_proto as pb, config::BoolKey};
use pb::config::bool::Key;
use crate::{
backend_proto as pb,
config::{BoolKey, StringKey},
};
use pb::config::bool::Key as BoolKeyProto;
use pb::config::string::Key as StringKeyProto;
impl From<Key> for BoolKey {
fn from(k: Key) -> Self {
impl From<BoolKeyProto> for BoolKey {
fn from(k: BoolKeyProto) -> Self {
match k {
Key::BrowserSortBackwards => BoolKey::BrowserSortBackwards,
Key::PreviewBothSides => BoolKey::PreviewBothSides,
Key::CollapseTags => BoolKey::CollapseTags,
Key::CollapseNotetypes => BoolKey::CollapseNotetypes,
Key::CollapseDecks => BoolKey::CollapseDecks,
Key::CollapseSavedSearches => BoolKey::CollapseSavedSearches,
Key::CollapseToday => BoolKey::CollapseToday,
Key::CollapseCardState => BoolKey::CollapseCardState,
Key::CollapseFlags => BoolKey::CollapseFlags,
Key::Sched2021 => BoolKey::Sched2021,
BoolKeyProto::BrowserSortBackwards => BoolKey::BrowserSortBackwards,
BoolKeyProto::PreviewBothSides => BoolKey::PreviewBothSides,
BoolKeyProto::CollapseTags => BoolKey::CollapseTags,
BoolKeyProto::CollapseNotetypes => BoolKey::CollapseNotetypes,
BoolKeyProto::CollapseDecks => BoolKey::CollapseDecks,
BoolKeyProto::CollapseSavedSearches => BoolKey::CollapseSavedSearches,
BoolKeyProto::CollapseToday => BoolKey::CollapseToday,
BoolKeyProto::CollapseCardState => BoolKey::CollapseCardState,
BoolKeyProto::CollapseFlags => BoolKey::CollapseFlags,
BoolKeyProto::Sched2021 => BoolKey::Sched2021,
}
}
}
impl From<StringKeyProto> for StringKey {
fn from(k: StringKeyProto) -> Self {
match k {
StringKeyProto::SetDueBrowser => StringKey::SetDueBrowser,
StringKeyProto::SetDueReviewer => StringKey::SetDueReviewer,
}
}
}

View File

@ -1527,14 +1527,16 @@ impl BackendService for Backend {
fn get_config_string(&self, input: pb::config::String) -> BackendResult<pb::String> {
self.with_col(|col| {
Ok(pb::String {
val: col.get_string(input),
val: col.get_string(input.key().into()),
})
})
}
fn set_config_string(&self, input: pb::SetConfigStringIn) -> BackendResult<pb::Empty> {
self.with_col(|col| col.transact(None, |col| col.set_string(input)))
.map(Into::into)
self.with_col(|col| {
col.transact(None, |col| col.set_string(input.key().into(), &input.value))
})
.map(Into::into)
}
fn get_preferences(&self, _input: Empty) -> BackendResult<pb::Preferences> {

View File

@ -2,10 +2,9 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::{
backend_proto as pb, collection::Collection, decks::DeckID, err::Result, notetype::NoteTypeID,
collection::Collection, decks::DeckID, err::Result, notetype::NoteTypeID,
timestamp::TimestampSecs,
};
pub use pb::config::string::Key as StringKey;
use serde::{de::DeserializeOwned, Serialize};
use serde_aux::field_attributes::deserialize_bool_from_anything;
use serde_derive::Deserialize;
@ -45,8 +44,6 @@ pub(crate) enum ConfigKey {
FirstDayOfWeek,
LocalOffset,
Rollover,
SetDueBrowser,
SetDueReviewer,
#[strum(to_string = "timeLim")]
AnswerTimeLimitSecs,
@ -95,13 +92,11 @@ pub enum BoolKey {
ShowRemainingDueCountsInStudy,
}
impl From<StringKey> for ConfigKey {
fn from(key: StringKey) -> Self {
match key {
StringKey::SetDueBrowser => ConfigKey::SetDueBrowser,
StringKey::SetDueReviewer => ConfigKey::SetDueReviewer,
}
}
#[derive(Debug, Clone, Copy, IntoStaticStr)]
#[strum(serialize_all = "camelCase")]
pub enum StringKey {
SetDueBrowser,
SetDueReviewer,
}
#[derive(PartialEq, Serialize_repr, Deserialize_repr, Clone, Copy, Debug)]
@ -180,6 +175,20 @@ impl Collection {
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)
}
@ -300,21 +309,6 @@ impl Collection {
pub(crate) fn set_last_unburied_day(&self, day: u32) -> Result<()> {
self.set_config(ConfigKey::LastUnburiedDay, &day)
}
pub(crate) fn get_string(&self, config: pb::config::String) -> String {
let key = config.key();
let default = match key {
StringKey::SetDueBrowser => "0",
StringKey::SetDueReviewer => "1",
// other => "",
};
self.get_config_optional(ConfigKey::from(key))
.unwrap_or_else(|| default.to_string())
}
pub(crate) fn set_string(&self, input: pb::SetConfigStringIn) -> Result<()> {
self.set_config(ConfigKey::from(input.key()), &input.value)
}
}
#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]