Skip to content

Commit 9b2987b

Browse files
authored
Fix numeric deck and notetype names being treated as ids when importing csv (#3748)
* fallback to treating deck and notetype ids as names * add test for metadata
1 parent eaec53b commit 9b2987b

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

rslib/src/import_export/text/csv/metadata.rs

+29
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,35 @@ pub(in crate::import_export) mod test {
687687
);
688688
}
689689

690+
#[test]
691+
fn should_fallback_to_parsing_deck_ids_as_deck_names() {
692+
let mut col = Collection::new();
693+
let numeric_deck_id = col.get_or_create_normal_deck("123456789").unwrap().id.0;
694+
let numeric_deck_2_id = col
695+
.get_or_create_normal_deck(&numeric_deck_id.to_string())
696+
.unwrap()
697+
.id
698+
.0;
699+
700+
assert_eq!(
701+
metadata!(col, "#deck:123456789\n").unwrap_deck_id(),
702+
numeric_deck_id
703+
);
704+
// parsed as id first, fallback to name after
705+
assert_eq!(
706+
metadata!(col, format!("#deck:{numeric_deck_id}\n")).unwrap_deck_id(),
707+
numeric_deck_id
708+
);
709+
assert_eq!(
710+
metadata!(col, format!("#deck:{numeric_deck_2_id}\n")).unwrap_deck_id(),
711+
numeric_deck_2_id
712+
);
713+
assert_eq!(
714+
metadata!(col, format!("#deck:1234\n")).unwrap_deck_id(),
715+
1 // default deck
716+
);
717+
}
718+
690719
#[test]
691720
fn should_detect_valid_delimiters() {
692721
let mut col = Collection::new();

rslib/src/import_export/text/import.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,12 @@ impl DeckIdsByNameOrId {
167167
fn get(&self, name_or_id: &NameOrId) -> Option<DeckId> {
168168
match name_or_id {
169169
_ if *name_or_id == NameOrId::default() => self.default,
170-
NameOrId::Id(id) => self.ids.get(&DeckId(*id)).copied(),
170+
NameOrId::Id(id) => self
171+
.ids
172+
.get(&DeckId(*id))
173+
// try treating it as a numeric deck name
174+
.or_else(|| self.names.get(&UniCase::new(id.to_string())))
175+
.copied(),
171176
NameOrId::Name(name) => self.names.get(&UniCase::new(name.to_string())).copied(),
172177
}
173178
}
@@ -504,7 +509,13 @@ impl Note {
504509
impl Collection {
505510
pub(super) fn deck_id_by_name_or_id(&mut self, deck: &NameOrId) -> Result<Option<DeckId>> {
506511
match deck {
507-
NameOrId::Id(id) => Ok(self.get_deck(DeckId(*id))?.map(|_| DeckId(*id))),
512+
NameOrId::Id(id) => Ok({
513+
match self.get_deck(DeckId(*id))?.map(|d| d.id) {
514+
did @ Some(_) => did,
515+
// try treating it as a numeric deck name
516+
_ => self.get_deck_id(&id.to_string())?,
517+
}
518+
}),
508519
NameOrId::Name(name) => self.get_deck_id(name),
509520
}
510521
}
@@ -514,7 +525,13 @@ impl Collection {
514525
notetype: &NameOrId,
515526
) -> Result<Option<Arc<Notetype>>> {
516527
match notetype {
517-
NameOrId::Id(id) => self.get_notetype(NotetypeId(*id)),
528+
NameOrId::Id(id) => Ok({
529+
match self.get_notetype(NotetypeId(*id))? {
530+
nt @ Some(_) => nt,
531+
// try treating it as a numeric notetype name
532+
_ => self.get_notetype_by_name(&id.to_string())?,
533+
}
534+
}),
518535
NameOrId::Name(name) => self.get_notetype_by_name(name),
519536
}
520537
}

0 commit comments

Comments
 (0)