From a95cbb8515b4034d179a1c8e514fc90e33ccd87c Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 28 Apr 2021 21:09:26 +1000 Subject: [PATCH] DeckConfId -> DeckConfigId --- rslib/src/backend/generic.rs | 4 ++-- rslib/src/deckconfig/mod.rs | 16 ++++++++++------ rslib/src/deckconfig/schema11.rs | 8 +++++--- rslib/src/deckconfig/update.rs | 12 ++++++------ rslib/src/decks/mod.rs | 4 ++-- rslib/src/decks/tree.rs | 14 +++++++------- rslib/src/notetype/cardgen.rs | 10 +++++----- rslib/src/prelude.rs | 2 +- rslib/src/scheduler/answering/mod.rs | 2 +- rslib/src/scheduler/answering/undo.rs | 2 +- rslib/src/scheduler/queue/limits.rs | 8 ++++---- rslib/src/scheduler/upgrade.rs | 4 ++-- rslib/src/storage/card/mod.rs | 4 ++-- rslib/src/storage/deckconfig/mod.rs | 12 ++++++------ 14 files changed, 54 insertions(+), 48 deletions(-) diff --git a/rslib/src/backend/generic.rs b/rslib/src/backend/generic.rs index 5310837b6..c78d80e3f 100644 --- a/rslib/src/backend/generic.rs +++ b/rslib/src/backend/generic.rs @@ -69,9 +69,9 @@ impl From for NotetypeId { } } -impl From for DeckConfId { +impl From for DeckConfigId { fn from(dcid: pb::DeckConfigId) -> Self { - DeckConfId(dcid.dcid) + DeckConfigId(dcid.dcid) } } diff --git a/rslib/src/deckconfig/mod.rs b/rslib/src/deckconfig/mod.rs index 185ea401d..15064d101 100644 --- a/rslib/src/deckconfig/mod.rs +++ b/rslib/src/deckconfig/mod.rs @@ -25,11 +25,11 @@ use crate::{ types::Usn, }; -define_newtype!(DeckConfId, i64); +define_newtype!(DeckConfigId, i64); #[derive(Debug, PartialEq, Clone)] pub struct DeckConfig { - pub id: DeckConfId, + pub id: DeckConfigId, pub name: String, pub mtime_secs: TimestampSecs, pub usn: Usn, @@ -39,7 +39,7 @@ pub struct DeckConfig { impl Default for DeckConfig { fn default() -> Self { DeckConfig { - id: DeckConfId(0), + id: DeckConfigId(0), name: "".to_string(), mtime_secs: Default::default(), usn: Default::default(), @@ -85,12 +85,16 @@ impl DeckConfig { impl Collection { /// If fallback is true, guaranteed to return a deck config. - pub fn get_deck_config(&self, dcid: DeckConfId, fallback: bool) -> Result> { + pub fn get_deck_config( + &self, + dcid: DeckConfigId, + fallback: bool, + ) -> Result> { if let Some(conf) = self.storage.get_deck_config(dcid)? { return Ok(Some(conf)); } 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)); } // 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. - 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 { return Err(AnkiError::invalid_input("can't delete default conf")); } diff --git a/rslib/src/deckconfig/schema11.rs b/rslib/src/deckconfig/schema11.rs index 05eebf215..67b6b0d12 100644 --- a/rslib/src/deckconfig/schema11.rs +++ b/rslib/src/deckconfig/schema11.rs @@ -9,14 +9,16 @@ use serde_json::Value; use serde_repr::{Deserialize_repr, Serialize_repr}; 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}; #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] #[serde(rename_all = "camelCase")] pub struct DeckConfSchema11 { #[serde(deserialize_with = "deserialize_number_from_string")] - pub(crate) id: DeckConfId, + pub(crate) id: DeckConfigId, #[serde(rename = "mod", deserialize_with = "deserialize_number_from_string")] pub(crate) mtime: TimestampSecs, pub(crate) name: String, @@ -191,7 +193,7 @@ impl Default for LapseConfSchema11 { impl Default for DeckConfSchema11 { fn default() -> Self { DeckConfSchema11 { - id: DeckConfId(0), + id: DeckConfigId(0), mtime: TimestampSecs(0), name: "Default".to_string(), usn: Usn(0), diff --git a/rslib/src/deckconfig/update.rs b/rslib/src/deckconfig/update.rs index dac3faf67..4ce1762fe 100644 --- a/rslib/src/deckconfig/update.rs +++ b/rslib/src/deckconfig/update.rs @@ -18,7 +18,7 @@ pub struct UpdateDeckConfigsIn { pub target_deck_id: DeckId, /// Deck will be set to last provided deck config. pub configs: Vec, - pub removed_config_ids: Vec, + pub removed_config_ids: Vec, pub apply_to_children: bool, } @@ -64,11 +64,11 @@ impl Collection { .collect()) } - fn get_deck_config_use_counts(&self) -> Result> { + fn get_deck_config_use_counts(&self) -> Result> { let mut counts = HashMap::new(); for deck in self.storage.get_all_decks()? { 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. - fn parent_config_ids(&self, deck: &Deck) -> Result> { + fn parent_config_ids(&self, deck: &Deck) -> Result> { Ok(self .storage .parent_decks(deck)? @@ -98,7 +98,7 @@ impl Collection { .filter_map(|deck| { deck.normal() .ok() - .map(|normal| DeckConfId(normal.config_id)) + .map(|normal| DeckConfigId(normal.config_id)) }) .collect()) } @@ -146,7 +146,7 @@ impl Collection { let deck_id = deck.id; // previous order - let previous_config_id = DeckConfId(normal.config_id); + let previous_config_id = DeckConfigId(normal.config_id); let previous_order = configs_before_update .get(&previous_config_id) .map(|c| c.inner.new_card_order()) diff --git a/rslib/src/decks/mod.rs b/rslib/src/decks/mod.rs index f847f1a5a..ac51873a3 100644 --- a/rslib/src/decks/mod.rs +++ b/rslib/src/decks/mod.rs @@ -68,9 +68,9 @@ impl Deck { } /// Returns deck config ID if deck is a normal deck. - pub(crate) fn config_id(&self) -> Option { + pub(crate) fn config_id(&self) -> Option { if let DeckKind::Normal(ref norm) = self.kind { - Some(DeckConfId(norm.config_id)) + Some(DeckConfigId(norm.config_id)) } else { None } diff --git a/rslib/src/decks/tree.rs b/rslib/src/decks/tree.rs index c138ca18b..12f09f4b5 100644 --- a/rslib/src/decks/tree.rs +++ b/rslib/src/decks/tree.rs @@ -94,7 +94,7 @@ fn apply_limits( node: &mut DeckTreeNode, today: u32, decks: &HashMap, - dconf: &HashMap, + dconf: &HashMap, parent_limits: (u32, u32), ) { let (mut remaining_new, mut remaining_rev) = @@ -128,7 +128,7 @@ fn apply_limits_v2_old( node: &mut DeckTreeNode, today: u32, decks: &HashMap, - dconf: &HashMap, + dconf: &HashMap, parent_limits: (u32, u32), ) -> u32 { let original_rev_count = node.review_count; @@ -161,15 +161,15 @@ fn remaining_counts_for_deck( did: DeckId, today: u32, decks: &HashMap, - dconf: &HashMap, + dconf: &HashMap, ) -> (u32, u32) { if let Some(deck) = decks.get(&did) { match &deck.kind { DeckKind::Normal(norm) => { let (new_today, rev_today) = deck.new_rev_counts(today); if let Some(conf) = dconf - .get(&DeckConfId(norm.config_id)) - .or_else(|| dconf.get(&DeckConfId(1))) + .get(&DeckConfigId(norm.config_id)) + .or_else(|| dconf.get(&DeckConfigId(1))) { let new = (conf.inner.new_per_day as i32) .saturating_sub(new_today) @@ -355,7 +355,7 @@ impl Collection { #[cfg(test)] mod test { use super::*; - use crate::{collection::open_test_collection, deckconfig::DeckConfId, error::Result}; + use crate::{collection::open_test_collection, deckconfig::DeckConfigId, error::Result}; #[test] fn wellformed() -> Result<()> { @@ -427,7 +427,7 @@ mod test { assert_eq!(tree.children[0].children[0].new_count, 4); // 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; col.add_or_update_deck_config(&mut conf, false)?; diff --git a/rslib/src/notetype/cardgen.rs b/rslib/src/notetype/cardgen.rs index 396b83c34..c269ea77a 100644 --- a/rslib/src/notetype/cardgen.rs +++ b/rslib/src/notetype/cardgen.rs @@ -11,7 +11,7 @@ use crate::{ card::{Card, CardId}, cloze::add_cloze_numbers_in_string, collection::Collection, - deckconfig::{DeckConfId, DeckConfig}, + deckconfig::{DeckConfig, DeckConfigId}, decks::DeckId, error::{AnkiError, Result}, notes::{Note, NoteId}, @@ -304,7 +304,7 @@ impl Collection { fn due_for_deck( &mut self, did: DeckId, - dcid: DeckConfId, + dcid: DeckConfigId, cache: &mut CardGenCache, ) -> Result { 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. - fn deck_for_adding(&mut self, did: Option) -> Result<(DeckId, DeckConfId)> { + fn deck_for_adding(&mut self, did: Option) -> Result<(DeckId, DeckConfigId)> { if let Some(did) = did { if let Some(deck) = self.deck_conf_if_normal(did)? { return Ok(deck); @@ -334,14 +334,14 @@ impl Collection { 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 self.deck_conf_if_normal(DeckId(1))? .ok_or_else(|| AnkiError::invalid_input("invalid default deck")) } /// If deck exists and and is a normal deck, return its ID and config - fn deck_conf_if_normal(&mut self, did: DeckId) -> Result> { + fn deck_conf_if_normal(&mut self, did: DeckId) -> Result> { Ok(self.get_deck(did)?.and_then(|d| { if let Some(conf_id) = d.config_id() { Some((did, conf_id)) diff --git a/rslib/src/prelude.rs b/rslib/src/prelude.rs index cea8fc89b..a1daa90bb 100644 --- a/rslib/src/prelude.rs +++ b/rslib/src/prelude.rs @@ -8,7 +8,7 @@ pub use crate::{ card::{Card, CardId}, collection::Collection, config::BoolKey, - deckconfig::{DeckConfId, DeckConfig}, + deckconfig::{DeckConfig, DeckConfigId}, decks::{Deck, DeckId, DeckKind, NativeDeckName}, error::{AnkiError, Result}, i18n::I18n, diff --git a/rslib/src/scheduler/answering/mod.rs b/rslib/src/scheduler/answering/mod.rs index 12ff5659b..56a1ac075 100644 --- a/rslib/src/scheduler/answering/mod.rs +++ b/rslib/src/scheduler/answering/mod.rs @@ -354,7 +354,7 @@ impl Collection { fn home_deck_config( &self, - config_id: Option, + config_id: Option, home_deck_id: DeckId, ) -> Result { let config_id = if let Some(config_id) = config_id { diff --git a/rslib/src/scheduler/answering/undo.rs b/rslib/src/scheduler/answering/undo.rs index 8829419db..e2424765a 100644 --- a/rslib/src/scheduler/answering/undo.rs +++ b/rslib/src/scheduler/answering/undo.rs @@ -24,7 +24,7 @@ mod test { col.add_note(&mut note, DeckId(1))?; // 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.leech_action = LeechAction::Suspend as i32; col.storage.update_deck_conf(&conf)?; diff --git a/rslib/src/scheduler/queue/limits.rs b/rslib/src/scheduler/queue/limits.rs index 9c23603cb..dcd93a28e 100644 --- a/rslib/src/scheduler/queue/limits.rs +++ b/rslib/src/scheduler/queue/limits.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use super::{Deck, DeckKind}; -use crate::deckconfig::{DeckConfId, DeckConfig}; +use crate::deckconfig::{DeckConfig, DeckConfigId}; #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) struct RemainingLimits { @@ -36,7 +36,7 @@ impl RemainingLimits { pub(super) fn remaining_limits_capped_to_parents( decks: &[Deck], - config: &HashMap, + config: &HashMap, today: u32, ) -> Vec { 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. fn get_remaining_limits( decks: &[Deck], - config: &HashMap, + config: &HashMap, today: u32, ) -> Vec { decks @@ -56,7 +56,7 @@ fn get_remaining_limits( .map(move |deck| { // get deck config if not filtered let config = if let DeckKind::Normal(normal) = &deck.kind { - config.get(&DeckConfId(normal.config_id)) + config.get(&DeckConfigId(normal.config_id)) } else { None }; diff --git a/rslib/src/scheduler/upgrade.rs b/rslib/src/scheduler/upgrade.rs index bbc8450be..79a2f472b 100644 --- a/rslib/src/scheduler/upgrade.rs +++ b/rslib/src/scheduler/upgrade.rs @@ -63,7 +63,7 @@ impl Card { fn get_filter_info_for_card( card: &Card, decks: &HashMap, - configs: &HashMap, + configs: &HashMap, ) -> Option { if card.original_deck_id.0 == 0 { None @@ -84,7 +84,7 @@ fn get_filter_info_for_card( let home_conf_id = decks .get(&card.original_deck_id) .and_then(|deck| deck.config_id()) - .unwrap_or(DeckConfId(1)); + .unwrap_or(DeckConfigId(1)); Some( configs .get(&home_conf_id) diff --git a/rslib/src/storage/card/mod.rs b/rslib/src/storage/card/mod.rs index ea31ded2f..44d1e5a38 100644 --- a/rslib/src/storage/card/mod.rs +++ b/rslib/src/storage/card/mod.rs @@ -14,7 +14,7 @@ use rusqlite::{ use super::ids_to_string; use crate::{ card::{Card, CardId, CardQueue, CardType}, - deckconfig::DeckConfId, + deckconfig::DeckConfigId, decks::{Deck, DeckId, DeckKind}, error::Result, notes::NoteId, @@ -487,7 +487,7 @@ impl super::SqliteStorage { /// 130% when the deck options were edited for the first time. pub(crate) fn fix_low_card_eases_for_configs( &self, - configs: &[DeckConfId], + configs: &[DeckConfigId], server: bool, ) -> Result<()> { let mut affected_decks = vec![]; diff --git a/rslib/src/storage/deckconfig/mod.rs b/rslib/src/storage/deckconfig/mod.rs index 9026f5136..ba7d12776 100644 --- a/rslib/src/storage/deckconfig/mod.rs +++ b/rslib/src/storage/deckconfig/mod.rs @@ -9,7 +9,7 @@ use serde_json::Value; use super::SqliteStorage; use crate::{ - deckconfig::{DeckConfId, DeckConfSchema11, DeckConfig, DeckConfigInner}, + deckconfig::{DeckConfSchema11, DeckConfig, DeckConfigId, DeckConfigInner}, prelude::*, }; @@ -32,7 +32,7 @@ impl SqliteStorage { .collect() } - pub(crate) fn get_deck_config_map(&self) -> Result> { + pub(crate) fn get_deck_config_map(&self) -> Result> { self.db .prepare_cached(include_str!("get.sql"))? .query_and_then(NO_PARAMS, row_to_deckconf)? @@ -40,7 +40,7 @@ impl SqliteStorage { .collect() } - pub(crate) fn get_deck_config(&self, dcid: DeckConfId) -> Result> { + pub(crate) fn get_deck_config(&self, dcid: DeckConfigId) -> Result> { self.db .prepare_cached(concat!(include_str!("get.sql"), " where id = ?"))? .query_and_then(params![dcid], row_to_deckconf)? @@ -105,7 +105,7 @@ impl SqliteStorage { Ok(()) } - pub(crate) fn remove_deck_conf(&self, dcid: DeckConfId) -> Result<()> { + pub(crate) fn remove_deck_conf(&self, dcid: DeckConfigId) -> Result<()> { self.db .prepare_cached("delete from deck_config where id=?")? .execute(params![dcid])?; @@ -148,7 +148,7 @@ impl SqliteStorage { } pub(super) fn upgrade_deck_conf_to_schema14(&self) -> Result<()> { - let conf: HashMap = + let conf: HashMap = self.db .query_row_and_then("select dconf from col", NO_PARAMS, |row| -> Result<_> { let text = row.get_raw(0).as_str()?; @@ -216,7 +216,7 @@ impl SqliteStorage { pub(super) fn downgrade_deck_conf_from_schema16(&self) -> Result<()> { let allconf = self.all_deck_config()?; - let confmap: HashMap = allconf + let confmap: HashMap = allconf .into_iter() .map(|c| -> DeckConfSchema11 { c.into() }) .map(|c| (c.id, c))