Skip to content

Commit

Permalink
substudy: Give better errors earlier for Anki export (fixes #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
emk committed May 4, 2024
1 parent 6bc3e3a commit 0c3cc90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 14 additions & 0 deletions substudy/src/export/anki/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ impl AnkiRequestParams for VersionRequest {
}
}

/// Get all deck names.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct DeckNamesRequest;

impl AnkiRequestParams for DeckNamesRequest {
const ACTION: &'static str = "deckNames";
type Response = Vec<String>;

fn omit_params(&self) -> bool {
true
}
}

/// Get all model names.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down
27 changes: 20 additions & 7 deletions substudy/src/export/anki/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{
export::{
anki::connect::{
AddNotesRequest, AnkiConnect, CanAddNotesWithErrorDetailRequest,
ModelNamesRequest, Note, NoteOptions, StoreMediaFileRequest,
VersionRequest,
DeckNamesRequest, ModelNamesRequest, Note, NoteOptions,
StoreMediaFileRequest, VersionRequest,
},
Exporter,
},
Expand Down Expand Up @@ -175,17 +175,30 @@ pub async fn export_anki(
exporter: &mut Exporter,
options: AnkiExportOptions,
) -> Result<()> {
let exporting = export_anki_helper(exporter, options.skip_duplicates)?;
exporter.finish_exports(ui).await?;
let model = &models::AUDIO_MODEL;

// Make sure we can talk to Anki-Connect.
let client = AnkiConnect::new();
let version = client.request(VersionRequest).await.context(
"Could not connect to Anki (Anki not running, Anki-Connect plugin not installed, or blocked by local firewall?)",
"Could not connect to AnkiConnect (see `substudy export anki --help` for troubleshooting)",
)?;
debug!("Anki-Connect version: {}", version);

// Make sure our deck exists.
let deck_names = client
.request(DeckNamesRequest)
.await
.context("error fetching deck names from Anki-Connect")?;
if !deck_names.iter().any(|n| n == &options.deck) {
return Err(anyhow!(
"deck not found: {:?} (please create it in Anki first)",
options.deck
));
}

// Export our notes and media (to a temporary directory).
let exporting = export_anki_helper(exporter, options.skip_duplicates)?;
exporter.finish_exports(ui).await?;
let model = &models::AUDIO_MODEL;

// Make sure our model exists.
let model_names = client
.request(ModelNamesRequest)
Expand Down

0 comments on commit 0c3cc90

Please sign in to comment.