#![warn(missing_docs)]
#![warn(clippy::all)]
pub mod cloze;
pub mod error;
pub mod markdown;
pub mod schema;
#[cfg(feature = "apkg")]
mod sql;
#[cfg(feature = "apkg")]
mod apkg;
#[cfg(feature = "connect")]
mod connect;
#[cfg(feature = "connect")]
mod diff;
#[cfg(feature = "connect")]
mod export;
#[cfg(feature = "connect")]
mod sync;
pub use error::{Error, Result};
pub use schema::{DeckDef, DeckDefinition, MediaDef, ModelDef, NoteDef, PackageInfo, TemplateDef};
#[cfg(feature = "apkg")]
pub use apkg::ApkgBuilder;
#[cfg(feature = "connect")]
pub use connect::{ConnectImporter, ImportResult};
#[cfg(feature = "connect")]
pub use diff::{DeckDiff, FieldChange, ModifiedNote, NoteDiff, TagChanges};
#[cfg(feature = "connect")]
pub use export::DeckExporter;
#[cfg(feature = "connect")]
pub use sync::{
ConflictResolution, ResolvedConflict, SyncConflict, SyncError, SyncNote, SyncPlan, SyncResult,
SyncStrategy, SyncedNote,
};
pub struct DeckBuilder {
definition: DeckDefinition,
#[cfg(feature = "apkg")]
media_base_path: Option<std::path::PathBuf>,
}
impl DeckBuilder {
pub fn new(definition: DeckDefinition) -> Self {
Self {
definition,
#[cfg(feature = "apkg")]
media_base_path: None,
}
}
pub fn from_file(path: impl AsRef<std::path::Path>) -> Result<Self> {
let definition = DeckDefinition::from_file(path)?;
Ok(Self::new(definition))
}
pub fn parse(content: &str) -> Result<Self> {
let definition = DeckDefinition::parse(content)?;
Ok(Self::new(definition))
}
#[cfg(feature = "apkg")]
pub fn media_base_path(mut self, path: impl AsRef<std::path::Path>) -> Self {
self.media_base_path = Some(path.as_ref().to_path_buf());
self
}
pub fn definition(&self) -> &DeckDefinition {
&self.definition
}
#[cfg(feature = "apkg")]
pub fn write_apkg(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
let mut builder = ApkgBuilder::new(self.definition.clone());
if let Some(ref media_path) = self.media_base_path {
builder = builder.media_base_path(media_path);
}
builder.write_to_file(path)
}
#[cfg(feature = "connect")]
pub async fn import_connect(&self) -> Result<ImportResult> {
let importer = ConnectImporter::new(self.definition.clone());
importer.import().await
}
#[cfg(feature = "connect")]
pub async fn import_connect_batch(&self) -> Result<ImportResult> {
let importer = ConnectImporter::new(self.definition.clone());
importer.import_batch().await
}
#[cfg(feature = "connect")]
pub async fn diff_connect(&self) -> Result<DeckDiff> {
let client = ankit::AnkiClient::new();
self.diff_connect_with_client(&client).await
}
#[cfg(feature = "connect")]
pub async fn diff_connect_with_client(&self, client: &ankit::AnkiClient) -> Result<DeckDiff> {
let differ = diff::DeckDiffer::new(client, &self.definition);
differ.diff().await
}
#[cfg(feature = "connect")]
pub async fn plan_sync(&self) -> Result<SyncPlan> {
let client = ankit::AnkiClient::new();
self.plan_sync_with_client(&client).await
}
#[cfg(feature = "connect")]
pub async fn plan_sync_with_client(&self, client: &ankit::AnkiClient) -> Result<SyncPlan> {
let syncer = sync::DeckSyncer::new(client, self.definition.clone());
syncer.plan().await
}
#[cfg(feature = "connect")]
pub async fn sync(&self, strategy: SyncStrategy) -> Result<SyncResult> {
let client = ankit::AnkiClient::new();
self.sync_with_client(&client, strategy).await
}
#[cfg(feature = "connect")]
pub async fn sync_with_client(
&self,
client: &ankit::AnkiClient,
strategy: SyncStrategy,
) -> Result<SyncResult> {
let syncer = sync::DeckSyncer::new(client, self.definition.clone());
syncer.sync(strategy).await
}
#[cfg(feature = "connect")]
pub async fn from_anki(client: &ankit::AnkiClient, deck_name: &str) -> Result<Self> {
let exporter = DeckExporter::new(client);
let definition = exporter.export_deck(deck_name).await?;
Ok(Self::new(definition))
}
pub fn write_toml(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
self.definition.write_toml(path)
}
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_TOML: &str = r#"
[package]
name = "Test Deck"
version = "1.0.0"
[[models]]
name = "Basic"
fields = ["Front", "Back"]
[[models.templates]]
name = "Card 1"
front = "{{Front}}"
back = "{{FrontSide}}<hr>{{Back}}"
[[decks]]
name = "Test Deck"
description = "A test deck"
[[notes]]
deck = "Test Deck"
model = "Basic"
tags = ["test"]
[notes.fields]
Front = "What is 2+2?"
Back = "4"
[[notes]]
deck = "Test Deck"
model = "Basic"
[notes.fields]
Front = "Capital of France?"
Back = "Paris"
"#;
#[test]
fn test_deck_builder_parse() {
let builder = DeckBuilder::parse(EXAMPLE_TOML).unwrap();
assert_eq!(builder.definition().package.name, "Test Deck");
assert_eq!(builder.definition().notes.len(), 2);
}
#[test]
#[cfg(feature = "apkg")]
fn test_write_apkg() {
let builder = DeckBuilder::parse(EXAMPLE_TOML).unwrap();
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.apkg");
builder.write_apkg(&path).unwrap();
assert!(path.exists());
}
}