DeckConfId -> DeckConfigId

This commit is contained in:
Damien Elmes 2021-04-28 21:09:26 +10:00
parent 1df86b28e8
commit a95cbb8515
14 changed files with 54 additions and 48 deletions

View file

@ -69,9 +69,9 @@ impl From<pb::NotetypeId> for NotetypeId {
} }
} }
impl From<pb::DeckConfigId> for DeckConfId { impl From<pb::DeckConfigId> for DeckConfigId {
fn from(dcid: pb::DeckConfigId) -> Self { fn from(dcid: pb::DeckConfigId) -> Self {
DeckConfId(dcid.dcid) DeckConfigId(dcid.dcid)
} }
} }

View file

@ -25,11 +25,11 @@ use crate::{
types::Usn, types::Usn,
}; };
define_newtype!(DeckConfId, i64); define_newtype!(DeckConfigId, i64);
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct DeckConfig { pub struct DeckConfig {
pub id: DeckConfId, pub id: DeckConfigId,
pub name: String, pub name: String,
pub mtime_secs: TimestampSecs, pub mtime_secs: TimestampSecs,
pub usn: Usn, pub usn: Usn,
@ -39,7 +39,7 @@ pub struct DeckConfig {
impl Default for DeckConfig { impl Default for DeckConfig {
fn default() -> Self { fn default() -> Self {
DeckConfig { DeckConfig {
id: DeckConfId(0), id: DeckConfigId(0),
name: "".to_string(), name: "".to_string(),
mtime_secs: Default::default(), mtime_secs: Default::default(),
usn: Default::default(), usn: Default::default(),
@ -85,12 +85,16 @@ impl DeckConfig {
impl Collection { impl Collection {
/// If fallback is true, guaranteed to return a deck config. /// If fallback is true, guaranteed to return a deck config.
pub fn get_deck_config(&self, dcid: DeckConfId, fallback: bool) -> Result<Option<DeckConfig>> { pub fn get_deck_config(
&self,
dcid: DeckConfigId,
fallback: bool,
) -> Result<Option<DeckConfig>> {
if let Some(conf) = self.storage.get_deck_config(dcid)? { if let Some(conf) = self.storage.get_deck_config(dcid)? {
return Ok(Some(conf)); return Ok(Some(conf));
} }
if fallback { if fallback {
if let Some(conf) = self.storage.get_deck_config(DeckConfId(1))? { if let Some(conf) = self.storage.get_deck_config(DeckConfigId(1))? {
return Ok(Some(conf)); return Ok(Some(conf));
} }
// if even the default deck config is missing, just return the defaults // if even the default deck config is missing, just return the defaults
@ -156,7 +160,7 @@ impl Collection {
} }
/// Remove a deck configuration. This will force a full sync. /// Remove a deck configuration. This will force a full sync.
pub(crate) fn remove_deck_config_inner(&mut self, dcid: DeckConfId) -> Result<()> { pub(crate) fn remove_deck_config_inner(&mut self, dcid: DeckConfigId) -> Result<()> {
if dcid.0 == 1 { if dcid.0 == 1 {
return Err(AnkiError::invalid_input("can't delete default conf")); return Err(AnkiError::invalid_input("can't delete default conf"));
} }

View file

@ -9,14 +9,16 @@ use serde_json::Value;
use serde_repr::{Deserialize_repr, Serialize_repr}; use serde_repr::{Deserialize_repr, Serialize_repr};
use serde_tuple::Serialize_tuple; use serde_tuple::Serialize_tuple;
use super::{DeckConfId, DeckConfig, DeckConfigInner, NewCardOrder, INITIAL_EASE_FACTOR_THOUSANDS}; use super::{
DeckConfig, DeckConfigId, DeckConfigInner, NewCardOrder, INITIAL_EASE_FACTOR_THOUSANDS,
};
use crate::{serde::default_on_invalid, timestamp::TimestampSecs, types::Usn}; use crate::{serde::default_on_invalid, timestamp::TimestampSecs, types::Usn};
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct DeckConfSchema11 { pub struct DeckConfSchema11 {
#[serde(deserialize_with = "deserialize_number_from_string")] #[serde(deserialize_with = "deserialize_number_from_string")]
pub(crate) id: DeckConfId, pub(crate) id: DeckConfigId,
#[serde(rename = "mod", deserialize_with = "deserialize_number_from_string")] #[serde(rename = "mod", deserialize_with = "deserialize_number_from_string")]
pub(crate) mtime: TimestampSecs, pub(crate) mtime: TimestampSecs,
pub(crate) name: String, pub(crate) name: String,
@ -191,7 +193,7 @@ impl Default for LapseConfSchema11 {
impl Default for DeckConfSchema11 { impl Default for DeckConfSchema11 {
fn default() -> Self { fn default() -> Self {
DeckConfSchema11 { DeckConfSchema11 {
id: DeckConfId(0), id: DeckConfigId(0),
mtime: TimestampSecs(0), mtime: TimestampSecs(0),
name: "Default".to_string(), name: "Default".to_string(),
usn: Usn(0), usn: Usn(0),

View file

@ -18,7 +18,7 @@ pub struct UpdateDeckConfigsIn {
pub target_deck_id: DeckId, pub target_deck_id: DeckId,
/// Deck will be set to last provided deck config. /// Deck will be set to last provided deck config.
pub configs: Vec<DeckConfig>, pub configs: Vec<DeckConfig>,
pub removed_config_ids: Vec<DeckConfId>, pub removed_config_ids: Vec<DeckConfigId>,
pub apply_to_children: bool, pub apply_to_children: bool,
} }
@ -64,11 +64,11 @@ impl Collection {
.collect()) .collect())
} }
fn get_deck_config_use_counts(&self) -> Result<HashMap<DeckConfId, usize>> { fn get_deck_config_use_counts(&self) -> Result<HashMap<DeckConfigId, usize>> {
let mut counts = HashMap::new(); let mut counts = HashMap::new();
for deck in self.storage.get_all_decks()? { for deck in self.storage.get_all_decks()? {
if let Ok(normal) = deck.normal() { if let Ok(normal) = deck.normal() {
*counts.entry(DeckConfId(normal.config_id)).or_default() += 1; *counts.entry(DeckConfigId(normal.config_id)).or_default() += 1;
} }
} }
@ -90,7 +90,7 @@ impl Collection {
} }
/// Deck configs used by parent decks. /// Deck configs used by parent decks.
fn parent_config_ids(&self, deck: &Deck) -> Result<HashSet<DeckConfId>> { fn parent_config_ids(&self, deck: &Deck) -> Result<HashSet<DeckConfigId>> {
Ok(self Ok(self
.storage .storage
.parent_decks(deck)? .parent_decks(deck)?
@ -98,7 +98,7 @@ impl Collection {
.filter_map(|deck| { .filter_map(|deck| {
deck.normal() deck.normal()
.ok() .ok()
.map(|normal| DeckConfId(normal.config_id)) .map(|normal| DeckConfigId(normal.config_id))
}) })
.collect()) .collect())
} }
@ -146,7 +146,7 @@ impl Collection {
let deck_id = deck.id; let deck_id = deck.id;
// previous order // previous order
let previous_config_id = DeckConfId(normal.config_id); let previous_config_id = DeckConfigId(normal.config_id);
let previous_order = configs_before_update let previous_order = configs_before_update
.get(&previous_config_id) .get(&previous_config_id)
.map(|c| c.inner.new_card_order()) .map(|c| c.inner.new_card_order())

View file

@ -68,9 +68,9 @@ impl Deck {
} }
/// Returns deck config ID if deck is a normal deck. /// Returns deck config ID if deck is a normal deck.
pub(crate) fn config_id(&self) -> Option<DeckConfId> { pub(crate) fn config_id(&self) -> Option<DeckConfigId> {
if let DeckKind::Normal(ref norm) = self.kind { if let DeckKind::Normal(ref norm) = self.kind {
Some(DeckConfId(norm.config_id)) Some(DeckConfigId(norm.config_id))
} else { } else {
None None
} }

View file

@ -94,7 +94,7 @@ fn apply_limits(
node: &mut DeckTreeNode, node: &mut DeckTreeNode,
today: u32, today: u32,
decks: &HashMap<DeckId, Deck>, decks: &HashMap<DeckId, Deck>,
dconf: &HashMap<DeckConfId, DeckConfig>, dconf: &HashMap<DeckConfigId, DeckConfig>,
parent_limits: (u32, u32), parent_limits: (u32, u32),
) { ) {
let (mut remaining_new, mut remaining_rev) = let (mut remaining_new, mut remaining_rev) =
@ -128,7 +128,7 @@ fn apply_limits_v2_old(
node: &mut DeckTreeNode, node: &mut DeckTreeNode,
today: u32, today: u32,
decks: &HashMap<DeckId, Deck>, decks: &HashMap<DeckId, Deck>,
dconf: &HashMap<DeckConfId, DeckConfig>, dconf: &HashMap<DeckConfigId, DeckConfig>,
parent_limits: (u32, u32), parent_limits: (u32, u32),
) -> u32 { ) -> u32 {
let original_rev_count = node.review_count; let original_rev_count = node.review_count;
@ -161,15 +161,15 @@ fn remaining_counts_for_deck(
did: DeckId, did: DeckId,
today: u32, today: u32,
decks: &HashMap<DeckId, Deck>, decks: &HashMap<DeckId, Deck>,
dconf: &HashMap<DeckConfId, DeckConfig>, dconf: &HashMap<DeckConfigId, DeckConfig>,
) -> (u32, u32) { ) -> (u32, u32) {
if let Some(deck) = decks.get(&did) { if let Some(deck) = decks.get(&did) {
match &deck.kind { match &deck.kind {
DeckKind::Normal(norm) => { DeckKind::Normal(norm) => {
let (new_today, rev_today) = deck.new_rev_counts(today); let (new_today, rev_today) = deck.new_rev_counts(today);
if let Some(conf) = dconf if let Some(conf) = dconf
.get(&DeckConfId(norm.config_id)) .get(&DeckConfigId(norm.config_id))
.or_else(|| dconf.get(&DeckConfId(1))) .or_else(|| dconf.get(&DeckConfigId(1)))
{ {
let new = (conf.inner.new_per_day as i32) let new = (conf.inner.new_per_day as i32)
.saturating_sub(new_today) .saturating_sub(new_today)
@ -355,7 +355,7 @@ impl Collection {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::{collection::open_test_collection, deckconfig::DeckConfId, error::Result}; use crate::{collection::open_test_collection, deckconfig::DeckConfigId, error::Result};
#[test] #[test]
fn wellformed() -> Result<()> { fn wellformed() -> Result<()> {
@ -427,7 +427,7 @@ mod test {
assert_eq!(tree.children[0].children[0].new_count, 4); assert_eq!(tree.children[0].children[0].new_count, 4);
// set the limit to 4, which should mean 3 are left // set the limit to 4, which should mean 3 are left
let mut conf = col.get_deck_config(DeckConfId(1), false)?.unwrap(); let mut conf = col.get_deck_config(DeckConfigId(1), false)?.unwrap();
conf.inner.new_per_day = 4; conf.inner.new_per_day = 4;
col.add_or_update_deck_config(&mut conf, false)?; col.add_or_update_deck_config(&mut conf, false)?;

View file

@ -11,7 +11,7 @@ use crate::{
card::{Card, CardId}, card::{Card, CardId},
cloze::add_cloze_numbers_in_string, cloze::add_cloze_numbers_in_string,
collection::Collection, collection::Collection,
deckconfig::{DeckConfId, DeckConfig}, deckconfig::{DeckConfig, DeckConfigId},
decks::DeckId, decks::DeckId,
error::{AnkiError, Result}, error::{AnkiError, Result},
notes::{Note, NoteId}, notes::{Note, NoteId},
@ -304,7 +304,7 @@ impl Collection {
fn due_for_deck( fn due_for_deck(
&mut self, &mut self,
did: DeckId, did: DeckId,
dcid: DeckConfId, dcid: DeckConfigId,
cache: &mut CardGenCache, cache: &mut CardGenCache,
) -> Result<u32> { ) -> Result<u32> {
if !cache.deck_configs.contains_key(&did) { if !cache.deck_configs.contains_key(&did) {
@ -324,7 +324,7 @@ impl Collection {
} }
/// If deck ID does not exist or points to a filtered deck, fall back on default. /// If deck ID does not exist or points to a filtered deck, fall back on default.
fn deck_for_adding(&mut self, did: Option<DeckId>) -> Result<(DeckId, DeckConfId)> { fn deck_for_adding(&mut self, did: Option<DeckId>) -> Result<(DeckId, DeckConfigId)> {
if let Some(did) = did { if let Some(did) = did {
if let Some(deck) = self.deck_conf_if_normal(did)? { if let Some(deck) = self.deck_conf_if_normal(did)? {
return Ok(deck); return Ok(deck);
@ -334,14 +334,14 @@ impl Collection {
self.default_deck_conf() self.default_deck_conf()
} }
fn default_deck_conf(&mut self) -> Result<(DeckId, DeckConfId)> { fn default_deck_conf(&mut self) -> Result<(DeckId, DeckConfigId)> {
// currently hard-coded to 1, we could create this as needed in the future // currently hard-coded to 1, we could create this as needed in the future
self.deck_conf_if_normal(DeckId(1))? self.deck_conf_if_normal(DeckId(1))?
.ok_or_else(|| AnkiError::invalid_input("invalid default deck")) .ok_or_else(|| AnkiError::invalid_input("invalid default deck"))
} }
/// If deck exists and and is a normal deck, return its ID and config /// If deck exists and and is a normal deck, return its ID and config
fn deck_conf_if_normal(&mut self, did: DeckId) -> Result<Option<(DeckId, DeckConfId)>> { fn deck_conf_if_normal(&mut self, did: DeckId) -> Result<Option<(DeckId, DeckConfigId)>> {
Ok(self.get_deck(did)?.and_then(|d| { Ok(self.get_deck(did)?.and_then(|d| {
if let Some(conf_id) = d.config_id() { if let Some(conf_id) = d.config_id() {
Some((did, conf_id)) Some((did, conf_id))

View file

@ -8,7 +8,7 @@ pub use crate::{
card::{Card, CardId}, card::{Card, CardId},
collection::Collection, collection::Collection,
config::BoolKey, config::BoolKey,
deckconfig::{DeckConfId, DeckConfig}, deckconfig::{DeckConfig, DeckConfigId},
decks::{Deck, DeckId, DeckKind, NativeDeckName}, decks::{Deck, DeckId, DeckKind, NativeDeckName},
error::{AnkiError, Result}, error::{AnkiError, Result},
i18n::I18n, i18n::I18n,

View file

@ -354,7 +354,7 @@ impl Collection {
fn home_deck_config( fn home_deck_config(
&self, &self,
config_id: Option<DeckConfId>, config_id: Option<DeckConfigId>,
home_deck_id: DeckId, home_deck_id: DeckId,
) -> Result<DeckConfig> { ) -> Result<DeckConfig> {
let config_id = if let Some(config_id) = config_id { let config_id = if let Some(config_id) = config_id {

View file

@ -24,7 +24,7 @@ mod test {
col.add_note(&mut note, DeckId(1))?; col.add_note(&mut note, DeckId(1))?;
// turn burying and leech suspension on // turn burying and leech suspension on
let mut conf = col.storage.get_deck_config(DeckConfId(1))?.unwrap(); let mut conf = col.storage.get_deck_config(DeckConfigId(1))?.unwrap();
conf.inner.bury_new = true; conf.inner.bury_new = true;
conf.inner.leech_action = LeechAction::Suspend as i32; conf.inner.leech_action = LeechAction::Suspend as i32;
col.storage.update_deck_conf(&conf)?; col.storage.update_deck_conf(&conf)?;

View file

@ -4,7 +4,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use super::{Deck, DeckKind}; use super::{Deck, DeckKind};
use crate::deckconfig::{DeckConfId, DeckConfig}; use crate::deckconfig::{DeckConfig, DeckConfigId};
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct RemainingLimits { pub(crate) struct RemainingLimits {
@ -36,7 +36,7 @@ impl RemainingLimits {
pub(super) fn remaining_limits_capped_to_parents( pub(super) fn remaining_limits_capped_to_parents(
decks: &[Deck], decks: &[Deck],
config: &HashMap<DeckConfId, DeckConfig>, config: &HashMap<DeckConfigId, DeckConfig>,
today: u32, today: u32,
) -> Vec<RemainingLimits> { ) -> Vec<RemainingLimits> {
let mut limits = get_remaining_limits(decks, config, today); let mut limits = get_remaining_limits(decks, config, today);
@ -48,7 +48,7 @@ pub(super) fn remaining_limits_capped_to_parents(
/// the provided deck order. /// the provided deck order.
fn get_remaining_limits( fn get_remaining_limits(
decks: &[Deck], decks: &[Deck],
config: &HashMap<DeckConfId, DeckConfig>, config: &HashMap<DeckConfigId, DeckConfig>,
today: u32, today: u32,
) -> Vec<RemainingLimits> { ) -> Vec<RemainingLimits> {
decks decks
@ -56,7 +56,7 @@ fn get_remaining_limits(
.map(move |deck| { .map(move |deck| {
// get deck config if not filtered // get deck config if not filtered
let config = if let DeckKind::Normal(normal) = &deck.kind { let config = if let DeckKind::Normal(normal) = &deck.kind {
config.get(&DeckConfId(normal.config_id)) config.get(&DeckConfigId(normal.config_id))
} else { } else {
None None
}; };

View file

@ -63,7 +63,7 @@ impl Card {
fn get_filter_info_for_card( fn get_filter_info_for_card(
card: &Card, card: &Card,
decks: &HashMap<DeckId, Deck>, decks: &HashMap<DeckId, Deck>,
configs: &HashMap<DeckConfId, DeckConfig>, configs: &HashMap<DeckConfigId, DeckConfig>,
) -> Option<V1FilteredDeckInfo> { ) -> Option<V1FilteredDeckInfo> {
if card.original_deck_id.0 == 0 { if card.original_deck_id.0 == 0 {
None None
@ -84,7 +84,7 @@ fn get_filter_info_for_card(
let home_conf_id = decks let home_conf_id = decks
.get(&card.original_deck_id) .get(&card.original_deck_id)
.and_then(|deck| deck.config_id()) .and_then(|deck| deck.config_id())
.unwrap_or(DeckConfId(1)); .unwrap_or(DeckConfigId(1));
Some( Some(
configs configs
.get(&home_conf_id) .get(&home_conf_id)

View file

@ -14,7 +14,7 @@ use rusqlite::{
use super::ids_to_string; use super::ids_to_string;
use crate::{ use crate::{
card::{Card, CardId, CardQueue, CardType}, card::{Card, CardId, CardQueue, CardType},
deckconfig::DeckConfId, deckconfig::DeckConfigId,
decks::{Deck, DeckId, DeckKind}, decks::{Deck, DeckId, DeckKind},
error::Result, error::Result,
notes::NoteId, notes::NoteId,
@ -487,7 +487,7 @@ impl super::SqliteStorage {
/// 130% when the deck options were edited for the first time. /// 130% when the deck options were edited for the first time.
pub(crate) fn fix_low_card_eases_for_configs( pub(crate) fn fix_low_card_eases_for_configs(
&self, &self,
configs: &[DeckConfId], configs: &[DeckConfigId],
server: bool, server: bool,
) -> Result<()> { ) -> Result<()> {
let mut affected_decks = vec![]; let mut affected_decks = vec![];

View file

@ -9,7 +9,7 @@ use serde_json::Value;
use super::SqliteStorage; use super::SqliteStorage;
use crate::{ use crate::{
deckconfig::{DeckConfId, DeckConfSchema11, DeckConfig, DeckConfigInner}, deckconfig::{DeckConfSchema11, DeckConfig, DeckConfigId, DeckConfigInner},
prelude::*, prelude::*,
}; };
@ -32,7 +32,7 @@ impl SqliteStorage {
.collect() .collect()
} }
pub(crate) fn get_deck_config_map(&self) -> Result<HashMap<DeckConfId, DeckConfig>> { pub(crate) fn get_deck_config_map(&self) -> Result<HashMap<DeckConfigId, DeckConfig>> {
self.db self.db
.prepare_cached(include_str!("get.sql"))? .prepare_cached(include_str!("get.sql"))?
.query_and_then(NO_PARAMS, row_to_deckconf)? .query_and_then(NO_PARAMS, row_to_deckconf)?
@ -40,7 +40,7 @@ impl SqliteStorage {
.collect() .collect()
} }
pub(crate) fn get_deck_config(&self, dcid: DeckConfId) -> Result<Option<DeckConfig>> { pub(crate) fn get_deck_config(&self, dcid: DeckConfigId) -> Result<Option<DeckConfig>> {
self.db self.db
.prepare_cached(concat!(include_str!("get.sql"), " where id = ?"))? .prepare_cached(concat!(include_str!("get.sql"), " where id = ?"))?
.query_and_then(params![dcid], row_to_deckconf)? .query_and_then(params![dcid], row_to_deckconf)?
@ -105,7 +105,7 @@ impl SqliteStorage {
Ok(()) Ok(())
} }
pub(crate) fn remove_deck_conf(&self, dcid: DeckConfId) -> Result<()> { pub(crate) fn remove_deck_conf(&self, dcid: DeckConfigId) -> Result<()> {
self.db self.db
.prepare_cached("delete from deck_config where id=?")? .prepare_cached("delete from deck_config where id=?")?
.execute(params![dcid])?; .execute(params![dcid])?;
@ -148,7 +148,7 @@ impl SqliteStorage {
} }
pub(super) fn upgrade_deck_conf_to_schema14(&self) -> Result<()> { pub(super) fn upgrade_deck_conf_to_schema14(&self) -> Result<()> {
let conf: HashMap<DeckConfId, DeckConfSchema11> = let conf: HashMap<DeckConfigId, DeckConfSchema11> =
self.db self.db
.query_row_and_then("select dconf from col", NO_PARAMS, |row| -> Result<_> { .query_row_and_then("select dconf from col", NO_PARAMS, |row| -> Result<_> {
let text = row.get_raw(0).as_str()?; let text = row.get_raw(0).as_str()?;
@ -216,7 +216,7 @@ impl SqliteStorage {
pub(super) fn downgrade_deck_conf_from_schema16(&self) -> Result<()> { pub(super) fn downgrade_deck_conf_from_schema16(&self) -> Result<()> {
let allconf = self.all_deck_config()?; let allconf = self.all_deck_config()?;
let confmap: HashMap<DeckConfId, DeckConfSchema11> = allconf let confmap: HashMap<DeckConfigId, DeckConfSchema11> = allconf
.into_iter() .into_iter()
.map(|c| -> DeckConfSchema11 { c.into() }) .map(|c| -> DeckConfSchema11 { c.into() })
.map(|c| (c.id, c)) .map(|c| (c.id, c))