anki_direct 0.0.13

A Simple Rust library for AnkiConnect API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! `anki_direct` is a Rust library for interacting with the AnkiConnect API.
//! It provides a convenient and type-safe way to manage your Anki collection,
//! including notes, models, and decks.
//!
//! The library aims to provide a comprehensive set of functionalities offered by AnkiConnect,
//! with a focus on ease of use and idiomatic Rust.
//!
//! # Features
//!
//! - **Notes Management**: Add, update, find, and delete notes.
//! - **Model Management**: Retrieve model information, including fields and templates.
//! - **Deck Management**: Get deck names and IDs.
//! - **Error Handling**: Robust error handling with custom error types.
//! - **Type-Safe API**: Strongly typed requests and responses for compile-time safety.
//! - **Blocking Client**: Uses `reqwest::blocking` for synchronous API calls.
//!
//! # Getting Started
//!
//! To use `anki_direct`, add it as a dependency in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! anki_direct = "0.0.1"
//! ```
//!
//! # Examples
//!
//! ## Creating an AnkiClient
//!
//! ```no_run
//! use anki_direct::AnkiClient;
//! use anki_direct::error::AnkiResult;
//!
//! fn main() -> AnkiResult<()> {
//!     // Connect to AnkiConnect on the default port (8765)
//!     let client = AnkiClient::default_latest()?;
//!     println!("Successfully connected to AnkiConnect!");
//!     Ok(())
//! }
//! ```
//!
//! ## Adding a Note
//!
//! ```no_run
//! use anki_direct::AnkiClient;
//! use anki_direct::notes::{NoteBuilder, MediaBuilder};
//! use anki_direct::error::AnkiResult;
//!
//! fn main() -> AnkiResult<()> {
//!     let client = AnkiClient::default_latest()?;
//!
//!     let audio = MediaBuilder::create_empty()
//!         .filename("example.mp3".into())
//!         .fields(vec!["myAudio".into()])
//!         .url("http://example.com/audio.mp3")
//!         .build()?;
//!
//!     let note = NoteBuilder::create_empty()
//!         .model_name("Basic".into())
//!         .deck_name("Default".into())
//!         .field("Front", "Hello")
//!         .field("Back", "World")
//!         .audios(vec![audio])
//!         .build(Some(client.reqwest_client()))?;
//!
//!     let new_ids = client.notes().add_notes(&[note])?;
//!     println!("Added note with ID: {:?}", new_ids);
//!     Ok(())
//! }
//! ```

#![allow(clippy::needless_doctest_main)]

pub mod anki;
#[cfg(feature = "cache")]
pub mod cache;
pub mod decks;
pub mod error;
pub mod generic;
pub mod model;
pub mod notes;
pub mod result;
mod str_utils;
mod test_utils;

use std::{ops::Deref, sync::Arc};

use error::{AnkiError, CustomSerdeError};
use getset::{Getters, MutGetters};
use num_traits::PrimInt;
use reqwest::blocking::Client as BlockingClient;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[cfg(feature = "cache")]
use crate::cache::Cache;
use crate::error::AnkiResult;
pub use reqwest::Client as ReqwestClient;

/// `AnkiClient` is the primary entry point for interacting with the AnkiConnect API.
/// It manages the connection to AnkiConnect and provides access to various modules
/// for managing notes, models, and decks.
///
/// # Examples
///
/// ```no_run
/// use anki_direct::AnkiClient;
/// use anki_direct::error::AnkiResult;
///
/// fn main() -> AnkiResult<()> {
///     let client = AnkiClient::default_latest()?;
///     println!("Successfully connected to AnkiConnect!");
///     Ok(())
/// }
/// ```
#[derive(Clone, Debug, Getters, MutGetters)]
pub struct AnkiClient {
    backend: Arc<Backend>,
    modules: Arc<AnkiModules>,
    #[cfg(feature = "cache")]
    #[getset(get = "pub", get_mut = "pub")]
    cache: Cache,
}
impl AnkiClient {
    /// Creates a new [AnkiClient] with the specified port, automatically detecting the AnkiConnect version.
    /// If `ankiconnect` isn't open or running on the port, returns `Err(`[AnkiError::ConnectionNotFound]`)`.
    ///
    /// # Parameters
    ///
    /// * `port`: The port where `ankiconnect` is running.
    ///
    /// # Example
    ///
    /// ```no_run
    /// // AnkiConnect's default is "8765"
    /// let client = AnkiClient::new_port("8765");
    /// ```
    pub fn new_port(port: &str) -> AnkiResult<Self> {
        let backend = Arc::new(Backend::new_port(port)?);
        let modules = Arc::new(AnkiModules::new(backend.clone()));
        Ok(Self {
            backend: backend.clone(),
            modules: modules.clone(),
            #[cfg(feature = "cache")]
            cache: Cache::init(modules),
        })
    }

    /// Creates a new [AnkiClient] with the default port ("8765"), automatically detecting the AnkiConnect version.
    /// If `ankiconnect` isn't open or running, returns `Err(`[AnkiError::ConnectionNotFound]`)`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let client = AnkiClient::default_latest();
    /// ```
    pub fn default_latest() -> AnkiResult<Self> {
        let backend = Arc::new(Backend::default_latest()?);
        let modules = Arc::new(AnkiModules::new(backend.clone()));
        Ok(Self {
            backend: backend.clone(),
            modules: modules.clone(),
            #[cfg(feature = "cache")]
            cache: Cache::init(modules),
        })
    }

    /// Creates a new [AnkiClient] with the specified port and a hardcoded version.
    /// This function does not perform any checks for AnkiConnect availability or version compatibility.
    /// It is suitable for static initialization where the AnkiConnect instance is guaranteed to be running
    /// on the specified port and version.
    ///
    /// # Parameters
    ///
    /// * `port`: The port where `ankiconnect` is expected to be running.
    /// * `version`: The expected API version of AnkiConnect.
    ///
    /// # Example
    ///
    /// ```
    /// // Create an AnkiClient for AnkiConnect on port 8765 with API version 6
    /// let client = AnkiClient::new_port_version("8765", 6);
    /// ```
    pub fn new_port_version(port: &str, version: u8) -> Self {
        let backend = Arc::new(Backend::new_port_version(port, version));
        let modules = Arc::new(AnkiModules::new(backend.clone()));
        Self {
            backend: backend.clone(),
            modules: modules.clone(),
            #[cfg(feature = "cache")]
            cache: Cache::init(modules),
        }
    }

    /// Provides access to notes-related AnkiConnect API calls.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use anki_direct::AnkiClient;
    /// use anki_direct::anki::AnkiQuery;
    /// use anki_direct::error::AnkiResult;
    ///
    /// fn main() -> AnkiResult<()> {
    ///     let client = AnkiClient::default_latest()?;
    ///     let new_notes = client.notes().find_notes(AnkiQuery::from("is:new"))?;
    ///     println!("Found {} new notes.", new_notes.len());
    ///     Ok(())
    /// }
    /// ```
    pub fn notes(&self) -> &NotesProxy {
        &self.modules.notes
    }

    /// Provides access to model-related AnkiConnect API calls.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use anki_direct::AnkiClient;
    /// use anki_direct::error::AnkiResult;
    ///
    /// fn main() -> AnkiResult<()> {
    ///     let client = AnkiClient::default_latest()?;
    ///     let models = client.models().get_all_models_less()?;
    ///     for (name, details) in models {
    ///         println!("Model: {}, Fields: {:?}", name, details.fields);
    ///     }
    ///     Ok(())
    /// }
    /// ```
    pub fn models(&self) -> &ModelsProxy {
        &self.modules.models
    }

    /// Provides access to deck-related AnkiConnect API calls.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use anki_direct::AnkiClient;
    /// use anki_direct::error::AnkiResult;
    ///
    /// fn main() -> AnkiResult<()> {
    ///     let client = AnkiClient::default_latest()?;
    ///     let decks = client.decks().get_all_deck_names_and_ids()?;
    ///     for (name, id) in decks {
    ///         println!("Deck: {}, ID: {}", name, id);
    ///     }
    ///     Ok(())
    /// }
    /// ```
    pub fn decks(&self) -> &DecksProxy {
        &self.modules.decks
    }

    /// Returns a reference to the internal `reqwest::blocking::Client` used by `anki_direct`.
    /// This can be useful if you need to perform custom HTTP requests to AnkiConnect
    /// or other services using the same client configuration.
    pub fn reqwest_client(&self) -> &BlockingClient {
        &self.backend.client
    }
}

/// `AnkiModules` is an internal struct that holds references to the various API modules
/// (notes, models, decks) and the shared `Backend`.
/// It's primarily used internally by `AnkiClient` to organize and provide access to
/// different parts of the AnkiConnect API.
#[derive(Clone, Debug, Getters)]
pub struct AnkiModules {
    backend: Arc<Backend>,
    notes: NotesProxy,
    models: ModelsProxy,
    #[getset(get = "pub")]
    decks: DecksProxy,
}
impl PartialEq for AnkiModules {
    fn eq(&self, other: &Self) -> bool {
        let Self { backend, .. } = other;
        self.backend == *backend
    }
}

impl AnkiModules {
    fn new(backend: Arc<Backend>) -> Self {
        Self {
            backend: backend.clone(),
            notes: NotesProxy(backend.clone()),
            models: ModelsProxy(backend.clone()),
            decks: DecksProxy(backend.clone()),
        }
    }
}

/// `NotesProxy` provides methods for interacting with notes in Anki.
/// It's a thin wrapper around the `Backend` that exposes notes-related AnkiConnect API calls.
/// You can access this through `AnkiClient::notes()`.
#[derive(Clone, Debug)]
pub struct NotesProxy(Arc<Backend>);
impl Deref for NotesProxy {
    type Target = Arc<Backend>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// `ModelsProxy` provides methods for interacting with models (note types) in Anki.
/// It's a thin wrapper around the `Backend` that exposes model-related AnkiConnect API calls.
/// You can access this through `AnkiClient::models()`.
#[derive(Clone, Debug)]
pub struct ModelsProxy(Arc<Backend>);
impl Deref for ModelsProxy {
    type Target = Arc<Backend>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// `DecksProxy` provides methods for interacting with decks in Anki.
/// It's a thin wrapper around the `Backend` that exposes deck-related AnkiConnect API calls.
/// You can access this through `AnkiClient::decks()`.
#[derive(Clone, Debug)]
pub struct DecksProxy(Arc<Backend>);
impl Deref for DecksProxy {
    type Target = Arc<Backend>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Default for AnkiClient {
    fn default() -> Self {
        let backend = Arc::new(Backend::default());
        let modules = Arc::new(AnkiModules::new(backend.clone()));
        Self {
            backend: backend.clone(),
            modules: modules.clone(),
            #[cfg(feature = "cache")]
            cache: Cache::init(modules.clone()),
        }
    }
}
impl Deref for AnkiClient {
    type Target = Arc<Backend>;
    fn deref(&self) -> &Self::Target {
        &self.backend
    }
}

/// `Backend` is a struct that allows you to communicate with the AnkiConnect API.
/// It handles the underlying HTTP requests and response parsing.
///
/// It contains the following fields:
/// - `endpoint`: The URL where AnkiConnect is running. Defaults to `http://localhost:8765`.
/// - `client`: The HTTP client used to send requests (`reqwest::blocking::Client`).
/// - `version`: The API version of the AnkiConnect plugin that the backend is configured to use.
#[derive(Clone, Debug)]
pub struct Backend {
    pub endpoint: String,
    pub client: BlockingClient,
    pub version: u8,
}

impl PartialEq for Backend {
    fn eq(&self, other: &Self) -> bool {
        let Self {
            endpoint, version, ..
        } = self;
        other.endpoint == *endpoint && other.version == *version
    }
}
impl Eq for Backend {}

impl Default for Backend {
    /// Creates a new `Backend` with the default port ("8765") and a hardcoded version of 6.
    /// This is a synchronous function and does not check for AnkiConnect availability or version compatibility.
    ///
    /// # Example
    ///
    /// ```
    /// use anki_direct::Backend;
    /// let backend = Backend::default();
    /// ```
    fn default() -> Self {
        Self::new_port_version("8765", 6)
    }
}

impl Backend {
    /// Creates a new `Backend` with the specified port, automatically detecting the AnkiConnect version.
    /// Returns an `Err(`[AnkiError::ConnectionNotFound]`)` if AnkiConnect isn't open or reachable on the given port.
    ///
    /// # Parameters
    ///
    /// * `port`: The port where AnkiConnect is running.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use anki_direct::Backend;
    /// use anki_direct::error::AnkiResult;
    ///
    /// fn main() -> AnkiResult<()> {
    ///     // AnkiConnect's default port is "8765"
    ///     let backend = Backend::new_port("8765")?;
    ///     Ok(())
    /// }
    /// ```
    pub fn new_port(port: &str) -> Result<Self, AnkiError> {
        let client = BlockingClient::new();
        let endpoint = Self::format_url(port);
        let version = Backend::get_version_internal(&client, &endpoint)?;
        let ac = Self {
            endpoint,
            client,
            version,
        };
        Ok(ac)
    }

    /// Creates a new `Backend` with the default port ("8765"), automatically detecting the AnkiConnect version.
    /// This is equivalent to calling `Backend::new_port("8765")`.
    /// Returns an `Err(`[AnkiError::ConnectionNotFound]`)` if AnkiConnect isn't open or reachable.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use anki_direct::Backend;
    /// use anki_direct::error::AnkiResult;
    ///
    /// fn main() -> AnkiResult<()> {
    ///     let backend = Backend::default_latest()?;
    ///     Ok(())
    /// }
    /// ```
    pub fn default_latest() -> Result<Self, AnkiError> {
        Self::new_port("8765")
    }

    /// Creates a new `Backend` with the specified port and a hardcoded version.
    /// This function does not perform any checks for AnkiConnect availability or version compatibility.
    /// It is suitable for static initialization where the AnkiConnect instance is guaranteed to be running
    /// on the specified port and version.
    ///
    /// # Parameters
    ///
    /// * `port`: The port where AnkiConnect is expected to be running.
    /// * `version`: The expected API version of AnkiConnect.
    ///
    /// # Example
    ///
    /// ```
    /// use anki_direct::Backend;
    /// // Create a backend for AnkiConnect on port 8765 with API version 6
    /// let backend = Backend::new_port_version("8765", 6);
    /// ```
    pub fn new_port_version(port: &str, version: u8) -> Self {
        Self {
            endpoint: Self::format_url(port),
            client: BlockingClient::new(),
            version,
        }
    }

    /// Formats the URL from the provided port.
    ///
    /// # Parameters
    ///
    /// * `port`: The port where AnkiConnect is running.
    ///
    /// # Example
    ///
    /// ```
    /// use anki_direct::Backend;
    /// let url = Backend::format_url("8765");
    /// ```
    pub fn format_url(port: &str) -> String {
        format!("http://localhost:{port}")
    }

    /// Makes a GET request to AnkiConnect to retrieve its version.
    /// This is an internal helper function used during backend initialization.
    pub fn get_version_internal(c: &BlockingClient, url: &str) -> Result<u8, AnkiError> {
        let res = match c.get(url).send() {
            Ok(response) => response,
            Err(_) => return Err(AnkiError::ConnectionNotFound(url.to_string())),
        };
        let val: Value = res.json().unwrap();
        let Some(res) = val.as_object() else {
            let cse = CustomSerdeError::expected(None, val, None);
            return Err(AnkiError::CustomSerde(cse));
        };
        let version: String = res.get("apiVersion").unwrap().to_string();
        let mut version_str = version
            .split_once(".")
            .expect("no delimiter `.` found")
            .1
            .to_string();
        version_str.remove(1);
        let version = version_str
            .parse::<u8>()
            .map_err(|_| AnkiError::ParseIntError(version_str.to_string()))?;
        Ok(version)
    }
}

/// A wrapper struct for integer types, providing a consistent way to handle numbers
/// that can be used as IDs or counts in AnkiConnect API calls.
/// It dereferences to an `isize`.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Number(isize);
impl Number {
    /// Creates a new `Number` from any primitive integer type.
    /// Panics if the conversion to `isize` fails (e.g., for very large `u128`).
    pub fn new(int: impl PrimInt) -> Self {
        Self(
            int.to_isize()
                .unwrap_or_else(|| panic!("num cannot be converted to isize")),
        )
    }

    /// Converts a slice of primitive integers into a vector of `Number`.
    pub fn from_slice_to_vec(slice: &[impl PrimInt]) -> Vec<Number> {
        slice.iter().map(|int| Number::new(*int)).collect()
    }
}
impl Deref for Number {
    type Target = isize;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}