use serde::Serialize;
use crate::AnkiRequest;
use crate::entities::{Model, ModelId};
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FindModelsByIdRequest {
pub model_ids: Vec<ModelId>,
}
impl AnkiRequest for FindModelsByIdRequest {
type Response = Vec<Model>;
const ACTION: &'static str = "findModelsById";
const VERSION: u8 = 6;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::entities::{ModelField, ModelRequirement, ModelRequirementKind, ModelTemplate};
#[test]
fn test_serialize() {
let request = FindModelsByIdRequest {
model_ids: vec![1704387367119, 1704387398570],
};
let json = serde_json::to_string_pretty(&request).unwrap();
assert_eq!(
json,
r#"{
"modelIds": [
1704387367119,
1704387398570
]
}"#
);
}
#[test]
fn test_deserialize() {
let json = r#"[
{
"id": 1704387367119,
"name": "Basic",
"type": 0,
"mod": 1704387367,
"usn": -1,
"sortf": 0,
"did": null,
"tmpls": [
{
"name": "Card 1",
"ord": 0,
"qfmt": "{{Front}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": 9176047152973362695
}
],
"flds": [
{
"name": "Front",
"ord": 0,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": 2453723143453745216,
"tag": null,
"preventDeletion": false
},
{
"name": "Back",
"ord": 1,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": -4853200230425436781,
"tag": null,
"preventDeletion": false
}
],
"css": ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
"latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
"latexPost": "\\end{document}",
"latexsvg": false,
"req": [
[
0,
"any",
[
0
]
]
],
"originalStockKind": 1
},
{
"id": 1704387398570,
"name": "Basic (and reversed card)",
"type": 0,
"mod": 1704387398,
"usn": -1,
"sortf": 0,
"did": null,
"tmpls": [
{
"name": "Card 1",
"ord": 0,
"qfmt": "{{Front}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Back}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": 1689886528158874152
},
{
"name": "Card 2",
"ord": 1,
"qfmt": "{{Back}}",
"afmt": "{{FrontSide}}\n\n<hr id=answer>\n\n{{Front}}",
"bqfmt": "",
"bafmt": "",
"did": null,
"bfont": "",
"bsize": 0,
"id": -7839609225644824587
}
],
"flds": [
{
"name": "Front",
"ord": 0,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": -7787837672455357996,
"tag": null,
"preventDeletion": false
},
{
"name": "Back",
"ord": 1,
"sticky": false,
"rtl": false,
"font": "Arial",
"size": 20,
"description": "",
"plainText": false,
"collapsed": false,
"excludeFromSearch": false,
"id": 6364828289839985081,
"tag": null,
"preventDeletion": false
}
],
"css": ".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
"latexPre": "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
"latexPost": "\\end{document}",
"latexsvg": false,
"req": [
[
0,
"any",
[
0
]
],
[
1,
"any",
[
1
]
]
],
"originalStockKind": 1
}
]"#;
let response: <FindModelsByIdRequest as AnkiRequest>::Response =
serde_json::from_str(json).unwrap();
let model1 = Model {
id: 1704387367119,
name: "Basic".to_string(),
r#type: 0,
modified_time: 1704387367,
usn: -1,
sort_field_idx: 0,
deck_id: None,
css: r#".card {
font-family: arial;
font-size: 20px;
text-align: center;
color: black;
background-color: white;
}
"#
.to_string(),
latex_pre: r#"\documentclass[12pt]{article}
\special{papersize=3in,5in}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}
"#
.to_string(),
latex_post: r#"\end{document}"#.to_string(),
latex_svg: false,
original_stock_kind: 1,
fields: vec![
ModelField {
name: "Front".to_string(),
ord: Some(0),
sticky: false,
rtl: false,
font: "Arial".to_string(),
size: 20,
description: "".to_string(),
plain_text: false,
collapsed: false,
exclude_from_search: false,
id: 2453723143453745216,
tag: None,
prevent_deletion: false,
},
ModelField {
name: "Back".to_string(),
ord: Some(1),
sticky: false,
rtl: false,
font: "Arial".to_string(),
size: 20,
description: "".to_string(),
plain_text: false,
collapsed: false,
exclude_from_search: false,
id: -4853200230425436781,
tag: None,
prevent_deletion: false,
},
],
templates: vec![ModelTemplate {
id: 9176047152973362695,
name: "Card 1".to_string(),
ord: Some(0),
question_format: "{{Front}}".to_string(),
answer_format: r#"{{FrontSide}}
<hr id=answer>
{{Back}}"#
.to_string(),
question_format_browser: "".to_string(),
answer_format_browser: "".to_string(),
target_deck_id: None,
bfont: "".to_string(),
bsize: 0,
}],
requirements: vec![ModelRequirement {
card_ord: 0,
kind: ModelRequirementKind::Any,
fields_ords: vec![0],
}],
};
let model2 = Model {
id: 1704387398570,
name: "Basic (and reversed card)".to_string(),
r#type: 0,
modified_time: 1704387398,
usn: -1,
sort_field_idx: 0,
deck_id: None,
css: r#".card {
font-family: arial;
font-size: 20px;
text-align: center;
color: black;
background-color: white;
}
"#
.to_string(),
latex_pre: r#"\documentclass[12pt]{article}
\special{papersize=3in,5in}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\begin{document}
"#
.to_string(),
latex_post: r#"\end{document}"#.to_string(),
latex_svg: false,
original_stock_kind: 1,
fields: vec![
ModelField {
name: "Front".to_string(),
ord: Some(0),
sticky: false,
rtl: false,
font: "Arial".to_string(),
size: 20,
description: "".to_string(),
plain_text: false,
collapsed: false,
exclude_from_search: false,
id: -7787837672455357996,
tag: None,
prevent_deletion: false,
},
ModelField {
name: "Back".to_string(),
ord: Some(1),
sticky: false,
rtl: false,
font: "Arial".to_string(),
size: 20,
description: "".to_string(),
plain_text: false,
collapsed: false,
exclude_from_search: false,
id: 6364828289839985081,
tag: None,
prevent_deletion: false,
},
],
templates: vec![
ModelTemplate {
id: 1689886528158874152,
name: "Card 1".to_string(),
ord: Some(0),
question_format: "{{Front}}".to_string(),
answer_format: r#"{{FrontSide}}
<hr id=answer>
{{Back}}"#
.to_string(),
question_format_browser: "".to_string(),
answer_format_browser: "".to_string(),
target_deck_id: None,
bfont: "".to_string(),
bsize: 0,
},
ModelTemplate {
id: -7839609225644824587,
name: "Card 2".to_string(),
ord: Some(1),
question_format: "{{Back}}".to_string(),
answer_format: r#"{{FrontSide}}
<hr id=answer>
{{Front}}"#
.to_string(),
question_format_browser: "".to_string(),
answer_format_browser: "".to_string(),
target_deck_id: None,
bfont: "".to_string(),
bsize: 0,
},
],
requirements: vec![
ModelRequirement {
card_ord: 0,
kind: ModelRequirementKind::Any,
fields_ords: vec![0],
},
ModelRequirement {
card_ord: 1,
kind: ModelRequirementKind::Any,
fields_ords: vec![1],
},
],
};
assert_eq!(response, vec![model1, model2]);
}
}