/*
* The MIT License (MIT)
*
* Copyright (c) 2023 Codecrafter_404 <codecrafter_404@t-online.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use std::collections::{BTreeMap, HashMap};
use serde::{Serialize, Serializer};
use crate::AnkiRequest;
use crate::entities::Model;
/// Parameters for the "createModel" action in `AnkiConnect`.
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateModelRequest {
pub model_name: String,
pub in_order_fields: Vec<String>,
pub css: String,
pub is_cloze: bool,
#[serde(serialize_with = "vec_with_sorted_map")]
pub card_templates: Vec<HashMap<String, String>>,
}
fn vec_with_sorted_map<S: Serializer, K: Serialize + Ord, V: Serialize>(
value: &[HashMap<K, V>],
serializer: S,
) -> Result<S::Ok, S::Error> {
let ordered: Vec<BTreeMap<_, _>> = value.iter().map(|map| map.iter().collect()).collect();
ordered.serialize(serializer)
}
pub type CreateModelResponse = Model;
impl AnkiRequest for CreateModelRequest {
type Response = CreateModelResponse;
const ACTION: &'static str = "createModel";
const VERSION: u8 = 6;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::entities::{ModelField, ModelRequirement, ModelRequirementKind, ModelTemplate};
#[test]
fn test_serialize() {
let request = CreateModelRequest {
model_name: "My Model".to_string(),
in_order_fields: vec![
"word".to_string(),
"meaning".to_string(),
"audio".to_string(),
"video".to_string(),
"image".to_string(),
],
css: r#".card {
color: red;
}"#
.to_string(),
is_cloze: false,
card_templates: vec![HashMap::from([
("Name".to_string(), "My Template".to_string()),
("Front".to_string(), "{{word}}".to_string()),
("Back".to_string(), "{{word}}\n{{meaning}}".to_string()),
])],
};
let json = serde_json::to_string_pretty(&request).unwrap();
assert_eq!(
json,
r#"{
"modelName": "My Model",
"inOrderFields": [
"word",
"meaning",
"audio",
"video",
"image"
],
"css": ".card {\n color: red;\n}",
"isCloze": false,
"cardTemplates": [
{
"Back": "{{word}}\n{{meaning}}",
"Front": "{{word}}",
"Name": "My Template"
}
]
}"#
);
}
#[test]
fn test_deserialize() {
let json = r#"{"id": 1740653102808, "name": "My Model", "type": 0, "mod": 1740653102, "usn": -1, "sortf": 0, "did": null, "tmpls": [{"name": "My Template", "ord": 0, "qfmt": "{{word}}", "afmt": "{{word}}\n{{meaning}}", "bqfmt": "", "bafmt": "", "did": null, "bfont": "", "bsize": 0, "id": 2190061454389606393}], "flds": [{"name": "word", "ord": 0, "sticky": false, "rtl": false, "font": "Arial", "size": 20, "description": "", "plainText": false, "collapsed": false, "excludeFromSearch": false, "id": 918700784278127929, "tag": null, "preventDeletion": false}, {"name": "meaning", "ord": 1, "sticky": false, "rtl": false, "font": "Arial", "size": 20, "description": "", "plainText": false, "collapsed": false, "excludeFromSearch": false, "id": 1816658205486584225, "tag": null, "preventDeletion": false}], "css": ".card {\n color: red;\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, 5]]], "originalStockKind": 1}"#;
let response: Model = serde_json::from_str(json).unwrap();
assert_eq!(
response,
Model {
id: 1740653102808,
name: "My Model".to_string(),
r#type: 0,
modified_time: 1740653102,
usn: -1,
sort_field_idx: 0,
deck_id: None,
css: ".card {\n color: red;\n}".to_string(),
latex_pre: "\\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".to_string(),
latex_post: "\\end{document}".to_string(),
latex_svg: false,
original_stock_kind: 1,
fields: vec![
ModelField {
id: 918700784278127929,
name: "word".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,
tag: None,
prevent_deletion: false,
},
ModelField {
id: 1816658205486584225,
name: "meaning".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,
tag: None,
prevent_deletion: false,
}
],
templates: vec![ModelTemplate {
id: 2190061454389606393,
name: "My Template".to_string(),
ord: Some(0),
question_format: "{{word}}".to_string(),
answer_format: "{{word}}\n{{meaning}}".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,5],
}],
}
);
}
}