diff --git a/rslib/src/import_export/package/apkg/import.rs b/rslib/src/import_export/package/apkg/import.rs index d3595bb3f..56df56e51 100644 --- a/rslib/src/import_export/package/apkg/import.rs +++ b/rslib/src/import_export/package/apkg/import.rs @@ -39,6 +39,7 @@ struct Context<'a> { remapped_notetypes: HashMap, remapped_notes: HashMap, remapped_decks: HashMap, + remapped_deck_configs: HashMap, data: ExchangeData, usn: Usn, /// Map of source media files, that do not already exist in the target. @@ -99,6 +100,7 @@ impl Collection { ctx.prepare_media()?; ctx.import_notetypes()?; ctx.import_notes()?; + ctx.import_deck_configs()?; ctx.import_decks()?; ctx.copy_media()?; Ok(()) @@ -149,6 +151,7 @@ impl<'a> Context<'a> { remapped_notetypes: HashMap::new(), remapped_notes: HashMap::new(), remapped_decks: HashMap::new(), + remapped_deck_configs: HashMap::new(), used_media_entries: HashMap::new(), normalize_notes, }) @@ -324,12 +327,24 @@ impl<'a> Context<'a> { }) } + fn import_deck_configs(&mut self) -> Result<()> { + // TODO: keep ids if possible? + for mut config in mem::take(&mut self.data.deck_configs) { + let old_id = mem::take(&mut config.id); + self.target_col + .add_deck_config_inner(&mut config, Some(self.usn))?; + self.remapped_deck_configs.insert(old_id, config.id); + } + Ok(()) + } + fn import_decks(&mut self) -> Result<()> { // TODO: ensure alphabetical order, so parents are seen first let mut renamed_parents = Vec::new(); for mut deck in mem::take(&mut self.data.decks) { deck.maybe_reparent(&renamed_parents); + self.remap_deck_config_id(&mut deck)?; if let Some(original) = self.get_deck_by_name(&deck)? { if original.is_filtered() { deck.uniquify_name(&mut renamed_parents); @@ -345,6 +360,16 @@ impl<'a> Context<'a> { Ok(()) } + fn remap_deck_config_id(&mut self, deck: &mut Deck) -> Result<()> { + if let Some(config_id) = self + .remapped_deck_configs + .get(&DeckConfigId(deck.normal()?.config_id)) + { + deck.normal_mut()?.config_id = config_id.0; + } + Ok(()) + } + fn add_deck(&mut self, deck: &mut Deck) -> Result<()> { let old_id = mem::take(&mut deck.id); self.target_col.add_deck_inner(deck, self.usn)?;