create deck if it doesn't exist

This commit is contained in:
llama 2025-07-04 17:50:20 +08:00
parent 96a2c626b9
commit db0b86e90f
No known key found for this signature in database
GPG key ID: 0B7543854B9413C3
2 changed files with 18 additions and 2 deletions

View file

@ -147,7 +147,7 @@ impl Duplicate {
}
impl DeckIdsByNameOrId {
fn new(col: &mut Collection, default: &NameOrId) -> Result<Self> {
fn new(col: &mut Collection, default: &NameOrId, usn: Usn) -> Result<Self> {
let names: HashMap<UniCase<String>, 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()?;

View file

@ -83,6 +83,15 @@ impl From<String> 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 {