#flashcards #http-request #anki #bridge #api-bindings #add-on

anki_bridge

AnkiBridge is a Rust library that provides a bridge between your Rust code and the Anki application, enabling HTTP communication and seamless data transmission

21 releases (7 breaking)

0.8.0 Mar 16, 2024
0.7.1 Jul 22, 2023
0.6.1 Jul 22, 2023

#780 in Network programming

22 downloads per month

MIT license

195KB
1.5K SLoC

AnkiBridge

AnkiBridge is a Rust library that serves as a bridge between your Rust code and the Anki application, leveraging the AnkiConnect add-on to establish an HTTP connection. This library enables seamless transmission of data and facilitates interaction with Anki through Rust.

Features

AnkiBridge provides the following features:

  • Establishing a connection with Anki through the AnkiConnect add-on.
  • Sending requests to Anki for various actions.
  • Retrieving data and statistics from Anki.
  • Interacting with cards and decks in Anki.

Installation

To use AnkiBridge in your Rust project, add the following line to your Cargo.toml file:

[dependencies]
anki_bridge = { version = "0.7", features = ["ureq_blocking"] }

Additionally, ensure that you have the Anki application installed on your system and that the AnkiConnect add-on is installed within Anki.

Please note that Anki must be opened and running on your computer for AnkiBridge to establish a connection successfully.

Usage

To establish a connection and perform actions with Anki, you can utilize the functions and structs provided by the AnkiBridge library in your Rust code. Here's a basic example:

use std::collections::HashMap;

use anki_bridge::prelude::*;

fn main() {
    let client = AnkiClient::default();
    let decks: Vec<String> = client.request(DeckNamesRequest {}).unwrap();
    println!("{decks:#?}");
    let deck_stats: HashMap<usize, GetDeckStatsResponse> =
        client.request(GetDeckStatsRequest { decks }).unwrap();
    println!("{deck_stats:#?}");
}

Mocking Data

use anki_bridge::{mock::*, prelude::*};

let client = MockAnkiClient::<FindCardsRequest, _>::new_mock(|params| {
    Ok(vec![123, params.query.len()])
});
let response = client.request(FindCardsRequest {
    query: "Card Deck Name".to_string(),
});
assert_eq!(
    vec![123, "Card Deck Name".len()],
    response.unwrap()
);

Todo

AnkiBridge is an ongoing project with planned future developments. Here are the upcoming items on the to-do list:

  • Card Actions
  • Deck Actions
  • Graphical Actions
  • Media Actions
  • Miscellaneous Actions
  • Model Actions
  • Note Actions
  • Statistic Actions
  • ureq synchronous HTTP client
  • reqwest asynchronous HTTP client
  • Mockable Client
  • Tests (real json test, mocking is already working)

Contributions to AnkiBridge are welcome. Feel free to contribute by opening issues or submitting pull requests on the GitLab repository.

Changelog

The entire changelog can be found in the CHANGELOG.md file.

Special Thanks

Thanks to VaiTon for having the idea to just implement a trait, instead of writing a function. That helped me to write a mockable client and clean everything up. You can find his implementation of the AnkiConnect bridge on GitHub. :)

License

AnkiBridge is distributed under the MIT License. For more information, see the LICENSE file.

Contact

For any questions or inquiries, please contact the project maintainer at daniel@kerkmann.dev.

Dependencies

~0.7–13MB
~158K SLoC