Crate ankiconnect_rs

Source
Expand description

§ankiconnect-rs

A Rust crate for interacting with AnkiConnect, enabling convenient programmatic control of Anki from within Rust. Provides type-safe abstractions for common Anki operations.

§Features

  • 🃏 Card Management: Create notes, find cards, browse cards via GUI
  • 🗃️ Deck Operations: Create decks, list existing decks
  • 📦 Media Handling: Store media files from paths/URLs/base64 data
  • 🧩 Model Support: Fetch field names, validate note structures
  • 🔄 Error Handling: Comprehensive error types for AnkiConnect-specific issues
  • Tested: Mock server integration tests for all major operations

§Example

use ankiconnect_rs::{AnkiClient, NoteBuilder, Field, MediaSource};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    // Create a client with default connection (localhost:8765)
    let client = AnkiClient::new();

    // Get available decks and models
    let decks = client.decks().get_all()?;
    let models = client.models().get_all()?;

    // Get fields for the selected model
    let selected_model = &models[0];
    let fields = client.models().get_fields(selected_model)?;

    // Build a note with the selected model
    let front_field = selected_model.field_ref("Front").unwrap();
    let back_field = selected_model.field_ref("Back").unwrap();

    let note = NoteBuilder::new(selected_model.clone())
        .with_field(front_field, "¿Dónde está la biblioteca?")
        .with_field(back_field, "Where is the library?")
        .with_tag("spanish-vocab")
        .with_image(
            front_field,
            MediaSource::Url("https://cdn.pixabay.com/photo/2023/08/18/15/02/dog-8198719_640.jpg".to_string()),
            "test_dog.jpg"
        )
        .build()?;

    // Add the note to the first deck
    let note_id = client.cards().add_note(&decks[0], note, false, None)?;
    println!("Added note with ID: {}", note_id.value());

    Ok(())
}

Re-exports§

pub use builders::NoteBuilder;
pub use builders::QueryBuilder;
pub use client::AnkiClient;
pub use client::DuplicateScope;
pub use error::AnkiConnectError;
pub use error::AnkiError;
pub use error::NoteError;
pub use error::Result;
pub use models::Card;
pub use models::CardId;
pub use models::Deck;
pub use models::DeckId;
pub use models::Field;
pub use models::FieldMedia;
pub use models::Media;
pub use models::MediaSource;
pub use models::MediaType;
pub use models::Model;
pub use models::Note;
pub use models::NoteId;

Modules§

builders
Builders for creating complex Anki objects
client
Client interfaces for interacting with Anki through AnkiConnect
error
Error types for the ankiconnect-rs crate
models
Domain models for Anki entities