return resolved deck to facilitate random order mode

This commit is contained in:
Damien Elmes 2020-05-12 11:30:52 +10:00
parent 9317cee9ba
commit 7597130145

View file

@ -6,15 +6,15 @@ use crate::{
card::{Card, CardID}, card::{Card, CardID},
cloze::add_cloze_numbers_in_string, cloze::add_cloze_numbers_in_string,
collection::Collection, collection::Collection,
decks::DeckID, decks::{Deck, DeckID},
err::Result, err::{AnkiError, Result},
notes::{Note, NoteID}, notes::{Note, NoteID},
notetype::NoteTypeKind, notetype::NoteTypeKind,
template::ParsedTemplate, template::ParsedTemplate,
types::Usn, types::Usn,
}; };
use itertools::Itertools; use itertools::Itertools;
use std::collections::HashSet; use std::{collections::HashSet, sync::Arc};
/// Info about an existing card required when generating new cards /// Info about an existing card required when generating new cards
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -259,14 +259,14 @@ impl Collection {
) -> Result<()> { ) -> Result<()> {
let mut next_pos = None; let mut next_pos = None;
for c in cards { for c in cards {
let did = self.deck_id_for_adding(c.did.or(target_deck_id))?; let target_deck = self.deck_for_adding(c.did.or(target_deck_id))?;
let due = c.due.unwrap_or_else(|| { let due = c.due.unwrap_or_else(|| {
if next_pos.is_none() { if next_pos.is_none() {
next_pos = Some(self.get_and_update_next_card_position().unwrap_or(0)); next_pos = Some(self.get_and_update_next_card_position().unwrap_or(0));
} }
next_pos.unwrap() next_pos.unwrap()
}); });
let mut card = Card::new(nid, c.ord as u16, did, due as i32); let mut card = Card::new(nid, c.ord as u16, target_deck.id, due as i32);
self.add_card(&mut card)?; self.add_card(&mut card)?;
} }
@ -274,23 +274,27 @@ 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_id_for_adding(&mut self, did: Option<DeckID>) -> Result<DeckID> { fn deck_for_adding(&mut self, did: Option<DeckID>) -> Result<Arc<Deck>> {
if let Some(did) = did.and_then(|did| self.deck_id_if_normal(did)) { if let Some(did) = did {
Ok(did) if let Some(deck) = self.deck_if_normal(did)? {
} else { return Ok(deck);
self.default_deck_id() }
} }
self.default_deck()
} }
fn default_deck_id(&mut self) -> Result<DeckID> { fn default_deck(&mut self) -> Result<Arc<Deck>> {
// currently hard-coded, we could create this as needed in the future // currently hard-coded to 1, we could create this as needed in the future
Ok(DeckID(1)) Ok(self
.get_deck(DeckID(1))?
.ok_or_else(|| AnkiError::invalid_input("missing default deck"))?)
} }
/// If deck exists and and is a normal deck, return it. /// If deck exists and and is a normal deck, return it.
fn deck_id_if_normal(&mut self, did: DeckID) -> Option<DeckID> { fn deck_if_normal(&mut self, did: DeckID) -> Result<Option<Arc<Deck>>> {
self.get_deck(did) Ok(self
.ok() .get_deck(did)?
.and_then(|opt| opt.and_then(|d| if !d.is_filtered() { Some(d.id) } else { None })) .and_then(|d| if !d.is_filtered() { Some(d) } else { None }))
} }
} }