Don't fail silently when an empty csv is imported

This commit is contained in:
Damien Elmes 2023-06-15 17:42:31 +10:00
parent f4fca1f5fd
commit de8f62f831
3 changed files with 9 additions and 1 deletions

View File

@ -115,6 +115,7 @@ importing-cards-added =
[one] { $count } card added.
*[other] { $count } cards added.
}
importing-file-empty = The file you selected is empty.
## NO NEED TO TRANSLATE. This text is no longer used by Anki, and will be removed in the future.

View File

@ -137,6 +137,7 @@ pub enum ImportError {
TooNew,
MediaImportFailed { info: String },
NoFieldColumn,
EmptyFile,
}
impl ImportError {
@ -148,6 +149,7 @@ impl ImportError {
tr.importing_failed_to_import_media_file(info)
}
ImportError::NoFieldColumn => tr.importing_file_must_contain_field_column(),
ImportError::EmptyFile => tr.importing_file_empty(),
}
.into()
}

View File

@ -46,7 +46,12 @@ impl Collection {
is_html: Option<bool>,
) -> Result<CsvMetadata> {
let mut reader = open_file(path)?;
self.get_reader_metadata(&mut reader, delimiter, notetype_id, deck_id, is_html)
let meta =
self.get_reader_metadata(&mut reader, delimiter, notetype_id, deck_id, is_html)?;
if meta.preview.is_empty() {
return Err(ImportError::EmptyFile.into());
}
Ok(meta)
}
fn get_reader_metadata(