From db0b86e90f81a3062791a14905e30a7bff4804f7 Mon Sep 17 00:00:00 2001 From: llama Date: Fri, 4 Jul 2025 17:50:20 +0800 Subject: [PATCH] create deck if it doesn't exist --- rslib/src/import_export/text/import.rs | 11 +++++++++-- rslib/src/import_export/text/mod.rs | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rslib/src/import_export/text/import.rs b/rslib/src/import_export/text/import.rs index 4425bb386..202189eb6 100644 --- a/rslib/src/import_export/text/import.rs +++ b/rslib/src/import_export/text/import.rs @@ -147,7 +147,7 @@ impl Duplicate { } impl DeckIdsByNameOrId { - fn new(col: &mut Collection, default: &NameOrId) -> Result { + fn new(col: &mut Collection, default: &NameOrId, usn: Usn) -> Result { let names: HashMap, DeckId> = col .get_all_normal_deck_names(false)? .into_iter() @@ -160,6 +160,13 @@ impl DeckIdsByNameOrId { default: None, }; new.default = new.get(default); + if new.default.is_none() && *default != NameOrId::default() { + let mut deck = Deck::new_normal(); + deck.name = NativeDeckName::from_human_name(default.to_string()); + col.add_deck_inner(&mut deck, usn)?; + new.insert(deck.id, deck.human_name()); + new.default = Some(deck.id); + } Ok(new) } @@ -193,7 +200,7 @@ impl<'a> Context<'a> { NameOrId::default(), col.notetype_by_name_or_id(&data.default_notetype)?, ); - let deck_ids = DeckIdsByNameOrId::new(col, &data.default_deck)?; + let deck_ids = DeckIdsByNameOrId::new(col, &data.default_deck, usn)?; let existing_checksums = ExistingChecksums::new(col, data.match_scope)?; let existing_guids = col.storage.all_notes_by_guid()?; diff --git a/rslib/src/import_export/text/mod.rs b/rslib/src/import_export/text/mod.rs index e9e2da766..fdf94971a 100644 --- a/rslib/src/import_export/text/mod.rs +++ b/rslib/src/import_export/text/mod.rs @@ -83,6 +83,15 @@ impl From for NameOrId { } } +impl std::fmt::Display for NameOrId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + NameOrId::Id(did) => write!(f, "{did}"), + NameOrId::Name(name) => write!(f, "{name}"), + } + } +} + impl ForeignNote { pub(crate) fn into_log_note(self) -> LogNote { LogNote {