use std::collections::HashMap;
use serde::Serialize;
use crate::client::AnkiClient;
use crate::error::Result;
use crate::types::{DeckConfig, DeckStats};
#[derive(Debug)]
pub struct DeckActions<'a> {
pub(crate) client: &'a AnkiClient,
}
#[derive(Serialize)]
struct CreateDeckParams<'a> {
deck: &'a str,
}
#[derive(Serialize)]
struct GetDecksParams<'a> {
cards: &'a [i64],
}
#[derive(Serialize)]
struct ChangeDeckParams<'a> {
cards: &'a [i64],
deck: &'a str,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct DeleteDecksParams<'a> {
decks: &'a [&'a str],
cards_too: bool,
}
#[derive(Serialize)]
struct GetDeckConfigParams<'a> {
deck: &'a str,
}
#[derive(Serialize)]
struct SaveDeckConfigParams<'a> {
config: &'a DeckConfig,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SetDeckConfigIdParams<'a> {
decks: &'a [&'a str],
config_id: i64,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct CloneDeckConfigParams<'a> {
name: &'a str,
clone_from: i64,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct RemoveDeckConfigParams {
config_id: i64,
}
#[derive(Serialize)]
struct GetDeckStatsParams<'a> {
decks: &'a [&'a str],
}
impl<'a> DeckActions<'a> {
pub async fn names(&self) -> Result<Vec<String>> {
self.client.invoke_without_params("deckNames").await
}
pub async fn names_and_ids(&self) -> Result<HashMap<String, i64>> {
self.client.invoke_without_params("deckNamesAndIds").await
}
pub async fn get_for_cards(&self, cards: &[i64]) -> Result<HashMap<String, Vec<i64>>> {
self.client
.invoke("getDecks", GetDecksParams { cards })
.await
}
pub async fn create(&self, name: &str) -> Result<i64> {
self.client
.invoke("createDeck", CreateDeckParams { deck: name })
.await
}
pub async fn move_cards(&self, cards: &[i64], deck: &str) -> Result<()> {
self.client
.invoke_void("changeDeck", ChangeDeckParams { cards, deck })
.await
}
pub async fn delete(&self, decks: &[&str], cards_too: bool) -> Result<()> {
self.client
.invoke_void("deleteDecks", DeleteDecksParams { decks, cards_too })
.await
}
pub async fn config(&self, deck: &str) -> Result<DeckConfig> {
self.client
.invoke("getDeckConfig", GetDeckConfigParams { deck })
.await
}
pub async fn save_config(&self, config: &DeckConfig) -> Result<bool> {
self.client
.invoke("saveDeckConfig", SaveDeckConfigParams { config })
.await
}
pub async fn set_config_id(&self, decks: &[&str], config_id: i64) -> Result<bool> {
self.client
.invoke(
"setDeckConfigId",
SetDeckConfigIdParams { decks, config_id },
)
.await
}
pub async fn clone_config(&self, name: &str, clone_from: i64) -> Result<i64> {
self.client
.invoke(
"cloneDeckConfigId",
CloneDeckConfigParams { name, clone_from },
)
.await
}
pub async fn remove_config(&self, config_id: i64) -> Result<bool> {
self.client
.invoke("removeDeckConfigId", RemoveDeckConfigParams { config_id })
.await
}
pub async fn stats(&self, decks: &[&str]) -> Result<HashMap<String, DeckStats>> {
self.client
.invoke("getDeckStats", GetDeckStatsParams { decks })
.await
}
}