Merge pull request #1132 from RumovZ/refactor-decks

Refactor decks/mod.rs
This commit is contained in:
Damien Elmes 2021-04-17 18:21:56 +10:00 committed by GitHub
commit ac46d40db3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 655 additions and 617 deletions

165
rslib/src/decks/add.rs Normal file
View File

@ -0,0 +1,165 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::name::{immediate_parent_name, normalize_native_name};
use crate::{error::FilteredDeckError, prelude::*};
use std::borrow::Cow;
impl Collection {
/// Normalize deck name and rename if not unique. Bumps mtime and usn if
/// name was changed, but otherwise leaves it the same.
pub(super) fn prepare_deck_for_update(&mut self, deck: &mut Deck, usn: Usn) -> Result<()> {
if let Cow::Owned(name) = normalize_native_name(&deck.name) {
deck.name = name;
deck.set_modified(usn);
}
self.ensure_deck_name_unique(deck, usn)
}
/// Add or update an existing deck modified by the user. May add parents,
/// or rename children as required. Prefer add_deck() or update_deck() to
/// be explicit about your intentions; this function mainly exists so we
/// can integrate with older Python code that behaved this way.
pub(crate) fn add_or_update_deck(&mut self, deck: &mut Deck) -> Result<OpOutput<()>> {
if deck.id.0 == 0 {
self.add_deck(deck)
} else {
self.update_deck(deck)
}
}
/// Add a new deck. The id must be 0, as it will be automatically assigned.
pub fn add_deck(&mut self, deck: &mut Deck) -> Result<OpOutput<()>> {
if deck.id.0 != 0 {
return Err(AnkiError::invalid_input("deck to add must have id 0"));
}
self.transact(Op::AddDeck, |col| col.add_deck_inner(deck, col.usn()?))
}
pub(crate) fn add_deck_inner(&mut self, deck: &mut Deck, usn: Usn) -> Result<()> {
self.prepare_deck_for_update(deck, usn)?;
deck.set_modified(usn);
self.match_or_create_parents(deck, usn)?;
self.add_deck_undoable(deck)
}
pub fn update_deck(&mut self, deck: &mut Deck) -> Result<OpOutput<()>> {
self.transact(Op::UpdateDeck, |col| {
let existing_deck = col.storage.get_deck(deck.id)?.ok_or(AnkiError::NotFound)?;
col.update_deck_inner(deck, existing_deck, col.usn()?)
})
}
pub(crate) fn update_deck_inner(
&mut self,
deck: &mut Deck,
original: Deck,
usn: Usn,
) -> Result<()> {
self.prepare_deck_for_update(deck, usn)?;
deck.set_modified(usn);
let name_changed = original.name != deck.name;
if name_changed {
// match closest parent name
self.match_or_create_parents(deck, usn)?;
// rename children
self.rename_child_decks(&original, &deck.name, usn)?;
}
self.update_single_deck_undoable(deck, original)?;
if name_changed {
// after updating, we need to ensure all grandparents exist, which may not be the case
// in the parent->child case
self.create_missing_parents(&deck.name, usn)?;
}
Ok(())
}
/// Add/update a single deck when syncing/importing. Ensures name is unique
/// & normalized, but does not check parents/children or update mtime
/// (unless the name was changed). Caller must set up transaction.
pub(crate) fn add_or_update_single_deck_with_existing_id(
&mut self,
deck: &mut Deck,
usn: Usn,
) -> Result<()> {
self.prepare_deck_for_update(deck, usn)?;
self.add_or_update_deck_with_existing_id_undoable(deck)
}
pub(crate) fn recover_missing_deck(&mut self, did: DeckId, usn: Usn) -> Result<()> {
let mut deck = Deck::new_normal();
deck.id = did;
deck.name = format!("recovered{}", did);
deck.set_modified(usn);
self.add_or_update_single_deck_with_existing_id(&mut deck, usn)
}
}
impl Collection {
/// Add a single, normal deck with the provided name for a child deck.
/// Caller must have done necessarily validation on name.
fn add_parent_deck(&mut self, machine_name: &str, usn: Usn) -> Result<()> {
let mut deck = Deck::new_normal();
deck.name = machine_name.into();
deck.set_modified(usn);
self.add_deck_undoable(&mut deck)
}
/// If parent deck(s) exist, rewrite name to match their case.
/// If they don't exist, create them.
/// Returns an error if a DB operation fails, or if the first existing parent is a filtered deck.
fn match_or_create_parents(&mut self, deck: &mut Deck, usn: Usn) -> Result<()> {
let child_split: Vec<_> = deck.name.split('\x1f').collect();
if let Some(parent_deck) = self.first_existing_parent(&deck.name, 0)? {
if parent_deck.is_filtered() {
return Err(FilteredDeckError::MustBeLeafNode.into());
}
let parent_count = parent_deck.name.matches('\x1f').count() + 1;
let need_create = parent_count != child_split.len() - 1;
deck.name = format!(
"{}\x1f{}",
parent_deck.name,
&child_split[parent_count..].join("\x1f")
);
if need_create {
self.create_missing_parents(&deck.name, usn)?;
}
Ok(())
} else if child_split.len() == 1 {
// no parents required
Ok(())
} else {
// no existing parents
self.create_missing_parents(&deck.name, usn)
}
}
fn create_missing_parents(&mut self, mut machine_name: &str, usn: Usn) -> Result<()> {
while let Some(parent_name) = immediate_parent_name(machine_name) {
if self.storage.get_deck_id(parent_name)?.is_none() {
self.add_parent_deck(parent_name, usn)?;
}
machine_name = parent_name;
}
Ok(())
}
fn first_existing_parent(
&self,
machine_name: &str,
recursion_level: usize,
) -> Result<Option<Deck>> {
if recursion_level > 10 {
return Err(AnkiError::invalid_input("deck nesting level too deep"));
}
if let Some(parent_name) = immediate_parent_name(machine_name) {
if let Some(parent_did) = self.storage.get_deck_id(parent_name)? {
self.storage.get_deck(parent_did)
} else {
self.first_existing_parent(parent_name, recursion_level + 1)
}
} else {
Ok(None)
}
}
}

View File

@ -1,7 +1,6 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::{collection::Collection, decks::DeckId, error::Result};
use crate::{backend_proto as pb, prelude::*};
use std::collections::HashMap;
#[derive(Debug)]
@ -11,6 +10,18 @@ pub(crate) struct DueCounts {
pub learning: u32,
}
impl Deck {
/// Return the studied counts if studied today.
/// May be negative if user has extended limits.
pub(crate) fn new_rev_counts(&self, today: u32) -> (i32, i32) {
if self.common.last_day_studied == today {
(self.common.new_studied, self.common.review_studied)
} else {
(0, 0)
}
}
}
impl Collection {
/// Get due counts for decks at the given timestamp.
pub(crate) fn due_counts(
@ -26,4 +37,17 @@ impl Collection {
limit_to,
)
}
pub(crate) fn counts_for_deck_today(
&mut self,
did: DeckId,
) -> Result<pb::CountsForDeckTodayOut> {
let today = self.current_due_day(0)?;
let mut deck = self.storage.get_deck(did)?.ok_or(AnkiError::NotFound)?;
deck.reset_stats_if_day_changed(today);
Ok(pb::CountsForDeckTodayOut {
new: deck.common.new_studied,
review: deck.common.review_studied,
})
}
}

View File

@ -3,7 +3,7 @@
use strum::IntoEnumIterator;
use super::{DeckCommon, DeckKind, FilteredDeck, FilteredSearchOrder, FilteredSearchTerm};
use super::{DeckCommon, FilteredDeck, FilteredSearchOrder, FilteredSearchTerm};
use crate::prelude::*;
impl Deck {

View File

@ -1,10 +1,15 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
mod add;
mod counts;
mod current;
mod filtered;
mod name;
mod remove;
mod reparent;
mod schema11;
mod stats;
mod tree;
pub(crate) mod undo;
@ -16,22 +21,15 @@ pub use crate::backend_proto::{
Deck as DeckProto,
};
use crate::{
backend_proto as pb,
collection::Collection,
deckconf::DeckConfId,
define_newtype,
error::FilteredDeckError,
error::{AnkiError, Result},
markdown::render_markdown,
prelude::*,
text::normalize_to_nfc,
define_newtype, error::FilteredDeckError, markdown::render_markdown, prelude::*,
text::sanitize_html_no_images,
timestamp::TimestampSecs,
types::Usn,
};
pub(crate) use counts::DueCounts;
pub(crate) use name::{
human_deck_name_to_native, immediate_parent_name, native_deck_name_to_human,
};
pub use schema11::DeckSchema11;
use std::{borrow::Cow, sync::Arc};
use std::sync::Arc;
define_newtype!(DeckId, i64);
@ -66,17 +64,6 @@ impl Deck {
}
}
fn reset_stats_if_day_changed(&mut self, today: u32) {
let c = &mut self.common;
if c.last_day_studied != today {
c.new_studied = 0;
c.learning_studied = 0;
c.review_studied = 0;
c.milliseconds_studied = 0;
c.last_day_studied = today;
}
}
/// Returns deck config ID if deck is a normal deck.
pub(crate) fn config_id(&self) -> Option<DeckConfId> {
if let DeckKind::Normal(ref norm) = self.kind {
@ -123,25 +110,11 @@ impl Deck {
}
}
pub fn human_name(&self) -> String {
self.name.replace("\x1f", "::")
}
pub(crate) fn set_modified(&mut self, usn: Usn) {
self.mtime_secs = TimestampSecs::now();
self.usn = usn;
}
/// Return the studied counts if studied today.
/// May be negative if user has extended limits.
pub(crate) fn new_rev_counts(&self, today: u32) -> (i32, i32) {
if self.common.last_day_studied == today {
(self.common.new_studied, self.common.review_studied)
} else {
(0, 0)
}
}
pub fn rendered_description(&self) -> String {
if let DeckKind::Normal(normal) = &self.kind {
if normal.markdown_description {
@ -156,60 +129,6 @@ impl Deck {
String::new()
}
}
// Mutate name to human representation for sharing.
pub fn with_human_name(mut self) -> Self {
self.name = self.human_name();
self
}
}
fn invalid_char_for_deck_component(c: char) -> bool {
c.is_ascii_control() || c == '"'
}
fn normalized_deck_name_component(comp: &str) -> Cow<str> {
let mut out = normalize_to_nfc(comp);
if out.contains(invalid_char_for_deck_component) {
out = out.replace(invalid_char_for_deck_component, "").into();
}
let trimmed = out.trim();
if trimmed.is_empty() {
"blank".to_string().into()
} else if trimmed.len() != out.len() {
trimmed.to_string().into()
} else {
out
}
}
fn normalize_native_name(name: &str) -> Cow<str> {
if name
.split('\x1f')
.any(|comp| matches!(normalized_deck_name_component(comp), Cow::Owned(_)))
{
let comps: Vec<_> = name
.split('\x1f')
.map(normalized_deck_name_component)
.collect::<Vec<_>>();
comps.join("\x1f").into()
} else {
// no changes required
name.into()
}
}
pub(crate) fn human_deck_name_to_native(name: &str) -> String {
let mut out = String::with_capacity(name.len());
for comp in name.split("::") {
out.push_str(&normalized_deck_name_component(comp));
out.push('\x1f');
}
out.trim_end_matches('\x1f').into()
}
pub(crate) fn native_deck_name_to_human(name: &str) -> String {
name.replace('\x1f', "::")
}
impl Collection {
@ -225,151 +144,11 @@ impl Collection {
Ok(None)
}
}
}
pub(crate) fn immediate_parent_name(machine_name: &str) -> Option<&str> {
machine_name.rsplitn(2, '\x1f').nth(1)
}
/// Determine name to rename a deck to, when `dragged` is dropped on `dropped`.
/// `dropped` being unset represents a drop at the top or bottom of the deck list.
/// The returned name should be used to rename `dragged`.
/// Arguments are expected in 'machine' form with an \x1f separator.
pub(crate) fn reparented_name(dragged: &str, dropped: Option<&str>) -> Option<String> {
let dragged_base = dragged.rsplit('\x1f').next().unwrap();
if let Some(dropped) = dropped {
if dropped.starts_with(dragged) {
// foo onto foo::bar, or foo onto itself -> no-op
None
} else {
// foo::bar onto baz -> baz::bar
Some(format!("{}\x1f{}", dropped, dragged_base))
}
} else {
// foo::bar onto top level -> bar
Some(dragged_base.into())
}
}
impl Collection {
pub(crate) fn default_deck_is_empty(&self) -> Result<bool> {
self.storage.deck_is_empty(DeckId(1))
}
/// Normalize deck name and rename if not unique. Bumps mtime and usn if
/// name was changed, but otherwise leaves it the same.
fn prepare_deck_for_update(&mut self, deck: &mut Deck, usn: Usn) -> Result<()> {
if let Cow::Owned(name) = normalize_native_name(&deck.name) {
deck.name = name;
deck.set_modified(usn);
}
self.ensure_deck_name_unique(deck, usn)
}
/// Add or update an existing deck modified by the user. May add parents,
/// or rename children as required. Prefer add_deck() or update_deck() to
/// be explicit about your intentions; this function mainly exists so we
/// can integrate with older Python code that behaved this way.
pub(crate) fn add_or_update_deck(&mut self, deck: &mut Deck) -> Result<OpOutput<()>> {
if deck.id.0 == 0 {
self.add_deck(deck)
} else {
self.update_deck(deck)
}
}
/// Add a new deck. The id must be 0, as it will be automatically assigned.
pub fn add_deck(&mut self, deck: &mut Deck) -> Result<OpOutput<()>> {
if deck.id.0 != 0 {
return Err(AnkiError::invalid_input("deck to add must have id 0"));
}
self.transact(Op::AddDeck, |col| col.add_deck_inner(deck, col.usn()?))
}
pub(crate) fn add_deck_inner(&mut self, deck: &mut Deck, usn: Usn) -> Result<()> {
self.prepare_deck_for_update(deck, usn)?;
deck.set_modified(usn);
self.match_or_create_parents(deck, usn)?;
self.add_deck_undoable(deck)
}
pub fn update_deck(&mut self, deck: &mut Deck) -> Result<OpOutput<()>> {
self.transact(Op::UpdateDeck, |col| {
let existing_deck = col.storage.get_deck(deck.id)?.ok_or(AnkiError::NotFound)?;
col.update_deck_inner(deck, existing_deck, col.usn()?)
})
}
pub fn rename_deck(&mut self, did: DeckId, new_human_name: &str) -> Result<OpOutput<()>> {
self.transact(Op::RenameDeck, |col| {
let existing_deck = col.storage.get_deck(did)?.ok_or(AnkiError::NotFound)?;
let mut deck = existing_deck.clone();
deck.name = human_deck_name_to_native(new_human_name);
col.update_deck_inner(&mut deck, existing_deck, col.usn()?)
})
}
pub(crate) fn update_deck_inner(
&mut self,
deck: &mut Deck,
original: Deck,
usn: Usn,
) -> Result<()> {
self.prepare_deck_for_update(deck, usn)?;
deck.set_modified(usn);
let name_changed = original.name != deck.name;
if name_changed {
// match closest parent name
self.match_or_create_parents(deck, usn)?;
// rename children
self.rename_child_decks(&original, &deck.name, usn)?;
}
self.update_single_deck_undoable(deck, original)?;
if name_changed {
// after updating, we need to ensure all grandparents exist, which may not be the case
// in the parent->child case
self.create_missing_parents(&deck.name, usn)?;
}
Ok(())
}
/// Add/update a single deck when syncing/importing. Ensures name is unique
/// & normalized, but does not check parents/children or update mtime
/// (unless the name was changed). Caller must set up transaction.
pub(crate) fn add_or_update_single_deck_with_existing_id(
&mut self,
deck: &mut Deck,
usn: Usn,
) -> Result<()> {
self.prepare_deck_for_update(deck, usn)?;
self.add_or_update_deck_with_existing_id_undoable(deck)
}
pub(crate) fn ensure_deck_name_unique(&self, deck: &mut Deck, usn: Usn) -> Result<()> {
loop {
match self.storage.get_deck_id(&deck.name)? {
Some(did) if did == deck.id => {
break;
}
None => break,
_ => (),
}
deck.name += "+";
deck.set_modified(usn);
}
Ok(())
}
pub(crate) fn recover_missing_deck(&mut self, did: DeckId, usn: Usn) -> Result<()> {
let mut deck = Deck::new_normal();
deck.id = did;
deck.name = format!("recovered{}", did);
deck.set_modified(usn);
self.add_or_update_single_deck_with_existing_id(&mut deck, usn)
}
pub fn get_or_create_normal_deck(&mut self, human_name: &str) -> Result<Deck> {
let native_name = human_deck_name_to_native(human_name);
if let Some(did) = self.storage.get_deck_id(&native_name)? {
@ -382,311 +161,16 @@ impl Collection {
}
}
fn rename_child_decks(&mut self, old: &Deck, new_name: &str, usn: Usn) -> Result<()> {
let children = self.storage.child_decks(old)?;
let old_component_count = old.name.matches('\x1f').count() + 1;
for mut child in children {
let original = child.clone();
let child_components: Vec<_> = child.name.split('\x1f').collect();
let child_only = &child_components[old_component_count..];
let new_name = format!("{}\x1f{}", new_name, child_only.join("\x1f"));
child.name = new_name;
child.set_modified(usn);
self.update_single_deck_undoable(&mut child, original)?;
}
Ok(())
}
/// Add a single, normal deck with the provided name for a child deck.
/// Caller must have done necessarily validation on name.
fn add_parent_deck(&mut self, machine_name: &str, usn: Usn) -> Result<()> {
let mut deck = Deck::new_normal();
deck.name = machine_name.into();
deck.set_modified(usn);
self.add_deck_undoable(&mut deck)
}
/// If parent deck(s) exist, rewrite name to match their case.
/// If they don't exist, create them.
/// Returns an error if a DB operation fails, or if the first existing parent is a filtered deck.
fn match_or_create_parents(&mut self, deck: &mut Deck, usn: Usn) -> Result<()> {
let child_split: Vec<_> = deck.name.split('\x1f').collect();
if let Some(parent_deck) = self.first_existing_parent(&deck.name, 0)? {
if parent_deck.is_filtered() {
return Err(FilteredDeckError::MustBeLeafNode.into());
}
let parent_count = parent_deck.name.matches('\x1f').count() + 1;
let need_create = parent_count != child_split.len() - 1;
deck.name = format!(
"{}\x1f{}",
parent_deck.name,
&child_split[parent_count..].join("\x1f")
);
if need_create {
self.create_missing_parents(&deck.name, usn)?;
}
Ok(())
} else if child_split.len() == 1 {
// no parents required
Ok(())
} else {
// no existing parents
self.create_missing_parents(&deck.name, usn)
}
}
fn create_missing_parents(&mut self, mut machine_name: &str, usn: Usn) -> Result<()> {
while let Some(parent_name) = immediate_parent_name(machine_name) {
if self.storage.get_deck_id(parent_name)?.is_none() {
self.add_parent_deck(parent_name, usn)?;
}
machine_name = parent_name;
}
Ok(())
}
fn first_existing_parent(
&self,
machine_name: &str,
recursion_level: usize,
) -> Result<Option<Deck>> {
if recursion_level > 10 {
return Err(AnkiError::invalid_input("deck nesting level too deep"));
}
if let Some(parent_name) = immediate_parent_name(machine_name) {
if let Some(parent_did) = self.storage.get_deck_id(parent_name)? {
self.storage.get_deck(parent_did)
} else {
self.first_existing_parent(parent_name, recursion_level + 1)
}
} else {
Ok(None)
}
}
/// Get a deck based on its human name. If you have a machine name,
/// use the method in storage instead.
pub(crate) fn get_deck_id(&self, human_name: &str) -> Result<Option<DeckId>> {
let machine_name = human_deck_name_to_native(&human_name);
self.storage.get_deck_id(&machine_name)
}
pub fn remove_decks_and_child_decks(&mut self, dids: &[DeckId]) -> Result<OpOutput<usize>> {
self.transact(Op::RemoveDeck, |col| {
let mut card_count = 0;
let usn = col.usn()?;
for did in dids {
if let Some(deck) = col.storage.get_deck(*did)? {
let child_decks = col.storage.child_decks(&deck)?;
// top level
card_count += col.remove_single_deck(&deck, usn)?;
// remove children
for deck in child_decks {
card_count += col.remove_single_deck(&deck, usn)?;
}
}
}
Ok(card_count)
})
}
pub(crate) fn remove_single_deck(&mut self, deck: &Deck, usn: Usn) -> Result<usize> {
let card_count = match deck.kind {
DeckKind::Normal(_) => self.delete_all_cards_in_normal_deck(deck.id)?,
DeckKind::Filtered(_) => {
self.return_all_cards_in_filtered_deck(deck.id)?;
0
}
};
self.clear_aux_config_for_deck(deck.id)?;
if deck.id.0 == 1 {
// if the default deck is included, just ensure it's reset to the default
// name, as we've already removed its cards
let mut modified_default = deck.clone();
modified_default.name = self.tr.deck_config_default_name().into();
self.prepare_deck_for_update(&mut modified_default, usn)?;
modified_default.set_modified(usn);
self.update_single_deck_undoable(&mut modified_default, deck.clone())?;
} else {
self.remove_deck_and_add_grave_undoable(deck.clone(), usn)?;
}
Ok(card_count)
}
fn delete_all_cards_in_normal_deck(&mut self, did: DeckId) -> Result<usize> {
let cids = self.storage.all_cards_in_single_deck(did)?;
self.remove_cards_and_orphaned_notes(&cids)?;
Ok(cids.len())
}
pub fn get_all_deck_names(&self, skip_empty_default: bool) -> Result<Vec<(DeckId, String)>> {
if skip_empty_default && self.default_deck_is_empty()? {
Ok(self
.storage
.get_all_deck_names()?
.into_iter()
.filter(|(id, _name)| id.0 != 1)
.collect())
} else {
self.storage.get_all_deck_names()
}
}
pub fn get_all_normal_deck_names(&mut self) -> Result<Vec<(DeckId, String)>> {
Ok(self
.storage
.get_all_deck_names()?
.into_iter()
.filter(|(id, _name)| match self.get_deck(*id) {
Ok(Some(deck)) => !deck.is_filtered(),
_ => true,
})
.collect())
}
/// Apply input delta to deck, and its parents.
/// Caller should ensure transaction.
pub(crate) fn update_deck_stats(
&mut self,
today: u32,
usn: Usn,
input: pb::UpdateStatsIn,
) -> Result<()> {
let did = input.deck_id.into();
let mutator = |c: &mut DeckCommon| {
c.new_studied += input.new_delta;
c.review_studied += input.review_delta;
c.milliseconds_studied += input.millisecond_delta;
};
if let Some(mut deck) = self.storage.get_deck(did)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
for mut deck in self.storage.parent_decks(&deck)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
}
}
Ok(())
}
/// Modify the deck's limits by adjusting the 'done today' count.
/// Positive values increase the limit, negative value decrease it.
/// Caller should ensure a transaction.
pub(crate) fn extend_limits(
&mut self,
today: u32,
usn: Usn,
did: DeckId,
new_delta: i32,
review_delta: i32,
) -> Result<()> {
let mutator = |c: &mut DeckCommon| {
c.new_studied -= new_delta;
c.review_studied -= review_delta;
};
if let Some(mut deck) = self.storage.get_deck(did)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
for mut deck in self.storage.parent_decks(&deck)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
}
for mut deck in self.storage.child_decks(&deck)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
}
}
Ok(())
}
pub(crate) fn counts_for_deck_today(
&mut self,
did: DeckId,
) -> Result<pb::CountsForDeckTodayOut> {
let today = self.current_due_day(0)?;
let mut deck = self.storage.get_deck(did)?.ok_or(AnkiError::NotFound)?;
deck.reset_stats_if_day_changed(today);
Ok(pb::CountsForDeckTodayOut {
new: deck.common.new_studied,
review: deck.common.review_studied,
})
}
fn update_deck_stats_single<F>(
&mut self,
today: u32,
usn: Usn,
deck: &mut Deck,
mutator: F,
) -> Result<()>
where
F: FnOnce(&mut DeckCommon),
{
let original = deck.clone();
deck.reset_stats_if_day_changed(today);
mutator(&mut deck.common);
deck.set_modified(usn);
self.update_single_deck_undoable(deck, original)
}
pub fn reparent_decks(
&mut self,
deck_ids: &[DeckId],
new_parent: Option<DeckId>,
) -> Result<OpOutput<usize>> {
self.transact(Op::ReparentDeck, |col| {
col.reparent_decks_inner(deck_ids, new_parent)
})
}
pub fn reparent_decks_inner(
&mut self,
deck_ids: &[DeckId],
new_parent: Option<DeckId>,
) -> Result<usize> {
let usn = self.usn()?;
let target_deck;
let mut target_name = None;
if let Some(target) = new_parent {
if let Some(target) = self.storage.get_deck(target)? {
if target.is_filtered() {
return Err(FilteredDeckError::MustBeLeafNode.into());
}
target_deck = target;
target_name = Some(target_deck.name.as_str());
}
}
let mut count = 0;
for deck in deck_ids {
if let Some(mut deck) = self.storage.get_deck(*deck)? {
if let Some(new_name) = reparented_name(&deck.name, target_name) {
count += 1;
let orig = deck.clone();
// this is basically update_deck_inner(), except:
// - we skip the normalization in prepare_for_update()
// - we skip the match_or_create_parents() step
// - we skip the final create_missing_parents(), as we don't allow parent->child
// renames
deck.set_modified(usn);
deck.name = new_name;
self.ensure_deck_name_unique(&mut deck, usn)?;
self.rename_child_decks(&orig, &deck.name, usn)?;
self.update_single_deck_undoable(&mut deck, orig)?;
}
}
}
Ok(count)
}
}
#[cfg(test)]
mod test {
use super::{human_deck_name_to_native, immediate_parent_name, normalize_native_name};
use crate::decks::reparented_name;
use crate::{
collection::{open_test_collection, Collection},
error::Result,
@ -702,33 +186,6 @@ mod test {
.collect()
}
#[test]
fn parent() {
assert_eq!(immediate_parent_name("foo"), None);
assert_eq!(immediate_parent_name("foo\x1fbar"), Some("foo"));
assert_eq!(
immediate_parent_name("foo\x1fbar\x1fbaz"),
Some("foo\x1fbar")
);
}
#[test]
fn from_human() {
assert_eq!(&human_deck_name_to_native("foo"), "foo");
assert_eq!(&human_deck_name_to_native("foo::bar"), "foo\x1fbar");
assert_eq!(&human_deck_name_to_native("fo\x1fo::ba\nr"), "foo\x1fbar");
assert_eq!(
&human_deck_name_to_native("foo::::baz"),
"foo\x1fblank\x1fbaz"
);
}
#[test]
fn normalize() {
assert_eq!(&normalize_native_name("foo\x1fbar"), "foo\x1fbar");
assert_eq!(&normalize_native_name("fo\u{a}o\x1fbar"), "foo\x1fbar");
}
#[test]
fn adding_updating() -> Result<()> {
let mut col = open_test_collection();
@ -852,40 +309,4 @@ mod test {
Ok(())
}
#[test]
fn drag_drop() {
// use custom separator to make the tests easier to read
fn n(s: &str) -> String {
s.replace(":", "\x1f")
}
#[allow(clippy::unnecessary_wraps)]
fn n_opt(s: &str) -> Option<String> {
Some(n(s))
}
assert_eq!(reparented_name("drag", Some("drop")), n_opt("drop:drag"));
assert_eq!(reparented_name("drag", None), n_opt("drag"));
assert_eq!(reparented_name(&n("drag:child"), None), n_opt("child"));
assert_eq!(
reparented_name(&n("drag:child"), Some(&n("drop:deck"))),
n_opt("drop:deck:child")
);
assert_eq!(
reparented_name(&n("drag:child"), Some("drag")),
n_opt("drag:child")
);
assert_eq!(
reparented_name(&n("drag:child:grandchild"), Some("drag")),
n_opt("drag:grandchild")
);
// drops to child not supported
assert_eq!(
reparented_name(&n("drag"), Some(&n("drag:child:grandchild"))),
None
);
// name doesn't change when deck dropped on itself
assert_eq!(reparented_name(&n("foo:bar"), Some(&n("foo:bar"))), None);
}
}

230
rslib/src/decks/name.rs Normal file
View File

@ -0,0 +1,230 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::{prelude::*, text::normalize_to_nfc};
use std::borrow::Cow;
impl Deck {
pub fn human_name(&self) -> String {
self.name.replace("\x1f", "::")
}
// Mutate name to human representation for sharing.
pub fn with_human_name(mut self) -> Self {
self.name = self.human_name();
self
}
}
impl Collection {
pub fn get_all_normal_deck_names(&mut self) -> Result<Vec<(DeckId, String)>> {
Ok(self
.storage
.get_all_deck_names()?
.into_iter()
.filter(|(id, _name)| match self.get_deck(*id) {
Ok(Some(deck)) => !deck.is_filtered(),
_ => true,
})
.collect())
}
pub fn rename_deck(&mut self, did: DeckId, new_human_name: &str) -> Result<OpOutput<()>> {
self.transact(Op::RenameDeck, |col| {
let existing_deck = col.storage.get_deck(did)?.ok_or(AnkiError::NotFound)?;
let mut deck = existing_deck.clone();
deck.name = human_deck_name_to_native(new_human_name);
col.update_deck_inner(&mut deck, existing_deck, col.usn()?)
})
}
pub(super) fn rename_child_decks(
&mut self,
old: &Deck,
new_name: &str,
usn: Usn,
) -> Result<()> {
let children = self.storage.child_decks(old)?;
let old_component_count = old.name.matches('\x1f').count() + 1;
for mut child in children {
let original = child.clone();
let child_components: Vec<_> = child.name.split('\x1f').collect();
let child_only = &child_components[old_component_count..];
let new_name = format!("{}\x1f{}", new_name, child_only.join("\x1f"));
child.name = new_name;
child.set_modified(usn);
self.update_single_deck_undoable(&mut child, original)?;
}
Ok(())
}
pub(crate) fn ensure_deck_name_unique(&self, deck: &mut Deck, usn: Usn) -> Result<()> {
loop {
match self.storage.get_deck_id(&deck.name)? {
Some(did) if did == deck.id => {
break;
}
None => break,
_ => (),
}
deck.name += "+";
deck.set_modified(usn);
}
Ok(())
}
pub fn get_all_deck_names(&self, skip_empty_default: bool) -> Result<Vec<(DeckId, String)>> {
if skip_empty_default && self.default_deck_is_empty()? {
Ok(self
.storage
.get_all_deck_names()?
.into_iter()
.filter(|(id, _name)| id.0 != 1)
.collect())
} else {
self.storage.get_all_deck_names()
}
}
}
fn invalid_char_for_deck_component(c: char) -> bool {
c.is_ascii_control() || c == '"'
}
fn normalized_deck_name_component(comp: &str) -> Cow<str> {
let mut out = normalize_to_nfc(comp);
if out.contains(invalid_char_for_deck_component) {
out = out.replace(invalid_char_for_deck_component, "").into();
}
let trimmed = out.trim();
if trimmed.is_empty() {
"blank".to_string().into()
} else if trimmed.len() != out.len() {
trimmed.to_string().into()
} else {
out
}
}
pub(super) fn normalize_native_name(name: &str) -> Cow<str> {
if name
.split('\x1f')
.any(|comp| matches!(normalized_deck_name_component(comp), Cow::Owned(_)))
{
let comps: Vec<_> = name
.split('\x1f')
.map(normalized_deck_name_component)
.collect::<Vec<_>>();
comps.join("\x1f").into()
} else {
// no changes required
name.into()
}
}
pub(crate) fn human_deck_name_to_native(name: &str) -> String {
let mut out = String::with_capacity(name.len());
for comp in name.split("::") {
out.push_str(&normalized_deck_name_component(comp));
out.push('\x1f');
}
out.trim_end_matches('\x1f').into()
}
pub(crate) fn native_deck_name_to_human(name: &str) -> String {
name.replace('\x1f', "::")
}
pub(crate) fn immediate_parent_name(machine_name: &str) -> Option<&str> {
machine_name.rsplitn(2, '\x1f').nth(1)
}
/// Determine name to rename a deck to, when `dragged` is dropped on `dropped`.
/// `dropped` being unset represents a drop at the top or bottom of the deck list.
/// The returned name should be used to rename `dragged`.
/// Arguments are expected in 'machine' form with an \x1f separator.
pub(crate) fn reparented_name(dragged: &str, dropped: Option<&str>) -> Option<String> {
let dragged_base = dragged.rsplit('\x1f').next().unwrap();
if let Some(dropped) = dropped {
if dropped.starts_with(dragged) {
// foo onto foo::bar, or foo onto itself -> no-op
None
} else {
// foo::bar onto baz -> baz::bar
Some(format!("{}\x1f{}", dropped, dragged_base))
}
} else {
// foo::bar onto top level -> bar
Some(dragged_base.into())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parent() {
assert_eq!(immediate_parent_name("foo"), None);
assert_eq!(immediate_parent_name("foo\x1fbar"), Some("foo"));
assert_eq!(
immediate_parent_name("foo\x1fbar\x1fbaz"),
Some("foo\x1fbar")
);
}
#[test]
fn from_human() {
assert_eq!(&human_deck_name_to_native("foo"), "foo");
assert_eq!(&human_deck_name_to_native("foo::bar"), "foo\x1fbar");
assert_eq!(&human_deck_name_to_native("fo\x1fo::ba\nr"), "foo\x1fbar");
assert_eq!(
&human_deck_name_to_native("foo::::baz"),
"foo\x1fblank\x1fbaz"
);
}
#[test]
fn normalize() {
assert_eq!(&normalize_native_name("foo\x1fbar"), "foo\x1fbar");
assert_eq!(&normalize_native_name("fo\u{a}o\x1fbar"), "foo\x1fbar");
}
#[test]
fn drag_drop() {
// use custom separator to make the tests easier to read
fn n(s: &str) -> String {
s.replace(":", "\x1f")
}
#[allow(clippy::unnecessary_wraps)]
fn n_opt(s: &str) -> Option<String> {
Some(n(s))
}
assert_eq!(reparented_name("drag", Some("drop")), n_opt("drop:drag"));
assert_eq!(reparented_name("drag", None), n_opt("drag"));
assert_eq!(reparented_name(&n("drag:child"), None), n_opt("child"));
assert_eq!(
reparented_name(&n("drag:child"), Some(&n("drop:deck"))),
n_opt("drop:deck:child")
);
assert_eq!(
reparented_name(&n("drag:child"), Some("drag")),
n_opt("drag:child")
);
assert_eq!(
reparented_name(&n("drag:child:grandchild"), Some("drag")),
n_opt("drag:grandchild")
);
// drops to child not supported
assert_eq!(
reparented_name(&n("drag"), Some(&n("drag:child:grandchild"))),
None
);
// name doesn't change when deck dropped on itself
assert_eq!(reparented_name(&n("foo:bar"), Some(&n("foo:bar"))), None);
}
}

57
rslib/src/decks/remove.rs Normal file
View File

@ -0,0 +1,57 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::prelude::*;
impl Collection {
pub fn remove_decks_and_child_decks(&mut self, dids: &[DeckId]) -> Result<OpOutput<usize>> {
self.transact(Op::RemoveDeck, |col| {
let mut card_count = 0;
let usn = col.usn()?;
for did in dids {
if let Some(deck) = col.storage.get_deck(*did)? {
let child_decks = col.storage.child_decks(&deck)?;
// top level
card_count += col.remove_single_deck(&deck, usn)?;
// remove children
for deck in child_decks {
card_count += col.remove_single_deck(&deck, usn)?;
}
}
}
Ok(card_count)
})
}
pub(crate) fn remove_single_deck(&mut self, deck: &Deck, usn: Usn) -> Result<usize> {
let card_count = match deck.kind {
DeckKind::Normal(_) => self.delete_all_cards_in_normal_deck(deck.id)?,
DeckKind::Filtered(_) => {
self.return_all_cards_in_filtered_deck(deck.id)?;
0
}
};
self.clear_aux_config_for_deck(deck.id)?;
if deck.id.0 == 1 {
// if the default deck is included, just ensure it's reset to the default
// name, as we've already removed its cards
let mut modified_default = deck.clone();
modified_default.name = self.tr.deck_config_default_name().into();
self.prepare_deck_for_update(&mut modified_default, usn)?;
modified_default.set_modified(usn);
self.update_single_deck_undoable(&mut modified_default, deck.clone())?;
} else {
self.remove_deck_and_add_grave_undoable(deck.clone(), usn)?;
}
Ok(card_count)
}
}
impl Collection {
fn delete_all_cards_in_normal_deck(&mut self, did: DeckId) -> Result<usize> {
let cids = self.storage.all_cards_in_single_deck(did)?;
self.remove_cards_and_orphaned_notes(&cids)?;
Ok(cids.len())
}
}

View File

@ -0,0 +1,59 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::name::reparented_name;
use crate::{error::FilteredDeckError, prelude::*};
impl Collection {
pub fn reparent_decks(
&mut self,
deck_ids: &[DeckId],
new_parent: Option<DeckId>,
) -> Result<OpOutput<usize>> {
self.transact(Op::ReparentDeck, |col| {
col.reparent_decks_inner(deck_ids, new_parent)
})
}
pub fn reparent_decks_inner(
&mut self,
deck_ids: &[DeckId],
new_parent: Option<DeckId>,
) -> Result<usize> {
let usn = self.usn()?;
let target_deck;
let mut target_name = None;
if let Some(target) = new_parent {
if let Some(target) = self.storage.get_deck(target)? {
if target.is_filtered() {
return Err(FilteredDeckError::MustBeLeafNode.into());
}
target_deck = target;
target_name = Some(target_deck.name.as_str());
}
}
let mut count = 0;
for deck in deck_ids {
if let Some(mut deck) = self.storage.get_deck(*deck)? {
if let Some(new_name) = reparented_name(&deck.name, target_name) {
count += 1;
let orig = deck.clone();
// this is basically update_deck_inner(), except:
// - we skip the normalization in prepare_for_update()
// - we skip the match_or_create_parents() step
// - we skip the final create_missing_parents(), as we don't allow parent->child
// renames
deck.set_modified(usn);
deck.name = new_name;
self.ensure_deck_name_unique(&mut deck, usn)?;
self.rename_child_decks(&orig, &deck.name, usn)?;
self.update_single_deck_undoable(&mut deck, orig)?;
}
}
}
Ok(count)
}
}

View File

@ -3,13 +3,12 @@
use super::DeckId;
use super::{
human_deck_name_to_native, native_deck_name_to_human, Deck, DeckCommon, DeckKind, FilteredDeck,
human_deck_name_to_native, native_deck_name_to_human, DeckCommon, FilteredDeck,
FilteredSearchTerm, NormalDeck,
};
use crate::{
prelude::*,
serde::{default_on_invalid, deserialize_bool_from_anything, deserialize_number_from_string},
timestamp::TimestampSecs,
types::Usn,
};
use serde_derive::{Deserialize, Serialize};

89
rslib/src/decks/stats.rs Normal file
View File

@ -0,0 +1,89 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::DeckCommon;
use crate::{backend_proto as pb, prelude::*};
impl Deck {
pub(super) fn reset_stats_if_day_changed(&mut self, today: u32) {
let c = &mut self.common;
if c.last_day_studied != today {
c.new_studied = 0;
c.learning_studied = 0;
c.review_studied = 0;
c.milliseconds_studied = 0;
c.last_day_studied = today;
}
}
}
impl Collection {
/// Apply input delta to deck, and its parents.
/// Caller should ensure transaction.
pub(crate) fn update_deck_stats(
&mut self,
today: u32,
usn: Usn,
input: pb::UpdateStatsIn,
) -> Result<()> {
let did = input.deck_id.into();
let mutator = |c: &mut DeckCommon| {
c.new_studied += input.new_delta;
c.review_studied += input.review_delta;
c.milliseconds_studied += input.millisecond_delta;
};
if let Some(mut deck) = self.storage.get_deck(did)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
for mut deck in self.storage.parent_decks(&deck)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
}
}
Ok(())
}
/// Modify the deck's limits by adjusting the 'done today' count.
/// Positive values increase the limit, negative value decrease it.
/// Caller should ensure a transaction.
pub(crate) fn extend_limits(
&mut self,
today: u32,
usn: Usn,
did: DeckId,
new_delta: i32,
review_delta: i32,
) -> Result<()> {
let mutator = |c: &mut DeckCommon| {
c.new_studied -= new_delta;
c.review_studied -= review_delta;
};
if let Some(mut deck) = self.storage.get_deck(did)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
for mut deck in self.storage.parent_decks(&deck)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
}
for mut deck in self.storage.child_decks(&deck)? {
self.update_deck_stats_single(today, usn, &mut deck, mutator)?;
}
}
Ok(())
}
}
impl Collection {
fn update_deck_stats_single<F>(
&mut self,
today: u32,
usn: Usn,
deck: &mut Deck,
mutator: F,
) -> Result<()>
where
F: FnOnce(&mut DeckCommon),
{
let original = deck.clone();
deck.reset_stats_if_day_changed(today);
mutator(&mut deck.common);
deck.set_modified(usn);
self.update_single_deck_undoable(deck, original)
}
}

View File

@ -1,18 +1,10 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::{Deck, DeckKind, DueCounts};
use super::DueCounts;
pub use crate::backend_proto::set_deck_collapsed_in::Scope as DeckCollapseScope;
use crate::{
backend_proto::DeckTreeNode,
collection::Collection,
config::{BoolKey, SchedulerVersion},
deckconf::{DeckConf, DeckConfId},
decks::DeckId,
error::Result,
ops::OpOutput,
timestamp::TimestampSecs,
undo::Op,
backend_proto::DeckTreeNode, config::SchedulerVersion, ops::OpOutput, prelude::*, undo::Op,
};
use serde_tuple::Serialize_tuple;
use std::{

View File

@ -30,6 +30,20 @@ impl Collection {
}
}
pub(crate) fn remove_deck_and_add_grave_undoable(
&mut self,
deck: Deck,
usn: Usn,
) -> Result<()> {
self.state.deck_cache.clear();
self.add_deck_grave_undoable(deck.id, usn)?;
self.storage.remove_deck(deck.id)?;
self.save_undo(UndoableDeckChange::Removed(Box::new(deck)));
Ok(())
}
}
impl Collection {
pub(super) fn add_deck_undoable(&mut self, deck: &mut Deck) -> Result<(), AnkiError> {
self.storage.add_deck(deck)?;
self.save_undo(UndoableDeckChange::Added(Box::new(deck.clone())));
@ -59,18 +73,6 @@ impl Collection {
self.storage.update_deck(deck)
}
pub(crate) fn remove_deck_and_add_grave_undoable(
&mut self,
deck: Deck,
usn: Usn,
) -> Result<()> {
self.state.deck_cache.clear();
self.add_deck_grave_undoable(deck.id, usn)?;
self.storage.remove_deck(deck.id)?;
self.save_undo(UndoableDeckChange::Removed(Box::new(deck)));
Ok(())
}
fn restore_deleted_deck(&mut self, deck: Deck) -> Result<()> {
self.storage.add_or_update_deck_with_existing_id(&deck)?;
self.save_undo(UndoableDeckChange::Added(Box::new(deck)));