use serde::{Deserialize, Serialize};
use crate::client::AnkiClient;
use crate::error::Result;
#[derive(Debug)]
pub struct MiscActions<'a> {
pub(crate) client: &'a AnkiClient,
}
#[derive(Serialize)]
struct LoadProfileParams<'a> {
name: &'a str,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ExportPackageParams<'a> {
deck: &'a str,
path: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
include_sched_data: Option<bool>,
}
#[derive(Serialize)]
struct ImportPackageParams<'a> {
path: &'a str,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiReflectParams<'a> {
scopes: &'a [&'a str],
actions: Option<&'a [&'a str]>,
}
#[derive(Serialize)]
struct MultiParams<'a> {
actions: &'a [MultiAction<'a>],
}
#[derive(Debug, Clone, Serialize)]
pub struct MultiAction<'a> {
pub action: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
impl<'a> MultiAction<'a> {
pub fn new(action: &'a str) -> Self {
Self {
action,
params: None,
}
}
pub fn with_params(action: &'a str, params: serde_json::Value) -> Self {
Self {
action,
params: Some(params),
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct PermissionResult {
pub permission: String,
#[serde(default)]
pub require_api_key: bool,
#[serde(default)]
pub version: Option<u8>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ApiReflectResult {
#[serde(default)]
pub scopes: Vec<String>,
#[serde(default)]
pub actions: Vec<String>,
}
impl<'a> MiscActions<'a> {
pub async fn version(&self) -> Result<u8> {
self.client.invoke_without_params("version").await
}
pub async fn request_permission(&self) -> Result<PermissionResult> {
self.client.invoke_without_params("requestPermission").await
}
pub async fn sync(&self) -> Result<()> {
self.client.invoke_void_without_params("sync").await
}
pub async fn profiles(&self) -> Result<Vec<String>> {
self.client.invoke_without_params("getProfiles").await
}
pub async fn load_profile(&self, name: &str) -> Result<bool> {
self.client
.invoke("loadProfile", LoadProfileParams { name })
.await
}
pub async fn export_package(
&self,
deck: &str,
path: &str,
include_sched_data: Option<bool>,
) -> Result<bool> {
self.client
.invoke(
"exportPackage",
ExportPackageParams {
deck,
path,
include_sched_data,
},
)
.await
}
pub async fn import_package(&self, path: &str) -> Result<bool> {
self.client
.invoke("importPackage", ImportPackageParams { path })
.await
}
pub async fn reload_collection(&self) -> Result<()> {
self.client
.invoke_void_without_params("reloadCollection")
.await
}
pub async fn api_reflect(
&self,
scopes: &[&str],
actions: Option<&[&str]>,
) -> Result<ApiReflectResult> {
self.client
.invoke("apiReflect", ApiReflectParams { scopes, actions })
.await
}
pub async fn multi(&self, actions: &[MultiAction<'_>]) -> Result<Vec<serde_json::Value>> {
self.client.invoke("multi", MultiParams { actions }).await
}
}