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 // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::{backend_proto as pb, config::BoolKey}; use crate::{
use pb::config::bool::Key; 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 { impl From<BoolKeyProto> for BoolKey {
fn from(k: Key) -> Self { fn from(k: BoolKeyProto) -> Self {
match k { match k {
Key::BrowserSortBackwards => BoolKey::BrowserSortBackwards, BoolKeyProto::BrowserSortBackwards => BoolKey::BrowserSortBackwards,
Key::PreviewBothSides => BoolKey::PreviewBothSides, BoolKeyProto::PreviewBothSides => BoolKey::PreviewBothSides,
Key::CollapseTags => BoolKey::CollapseTags, BoolKeyProto::CollapseTags => BoolKey::CollapseTags,
Key::CollapseNotetypes => BoolKey::CollapseNotetypes, BoolKeyProto::CollapseNotetypes => BoolKey::CollapseNotetypes,
Key::CollapseDecks => BoolKey::CollapseDecks, BoolKeyProto::CollapseDecks => BoolKey::CollapseDecks,
Key::CollapseSavedSearches => BoolKey::CollapseSavedSearches, BoolKeyProto::CollapseSavedSearches => BoolKey::CollapseSavedSearches,
Key::CollapseToday => BoolKey::CollapseToday, BoolKeyProto::CollapseToday => BoolKey::CollapseToday,
Key::CollapseCardState => BoolKey::CollapseCardState, BoolKeyProto::CollapseCardState => BoolKey::CollapseCardState,
Key::CollapseFlags => BoolKey::CollapseFlags, BoolKeyProto::CollapseFlags => BoolKey::CollapseFlags,
Key::Sched2021 => BoolKey::Sched2021, 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,13 +1527,15 @@ impl BackendService for Backend {
fn get_config_string(&self, input: pb::config::String) -> BackendResult<pb::String> { fn get_config_string(&self, input: pb::config::String) -> BackendResult<pb::String> {
self.with_col(|col| { self.with_col(|col| {
Ok(pb::String { 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> { fn set_config_string(&self, input: pb::SetConfigStringIn) -> BackendResult<pb::Empty> {
self.with_col(|col| col.transact(None, |col| col.set_string(input))) self.with_col(|col| {
col.transact(None, |col| col.set_string(input.key().into(), &input.value))
})
.map(Into::into) .map(Into::into)
} }

View File

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