use serde::Serialize;
use std::collections::HashMap;
use crate::AnkiRequest;
use crate::entities::NoteId;
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize)]
pub struct UpdateNoteModelRequest {
pub note: UpdateNoteModelEntry,
}
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateNoteModelEntry {
pub id: NoteId,
pub model_name: String,
#[serde(serialize_with = "crate::serialize::hashmap")]
pub fields: HashMap<String, String>,
pub tags: Vec<String>,
}
impl AnkiRequest for UpdateNoteModelRequest {
type Response = ();
const ACTION: &'static str = "updateNoteModel";
const VERSION: u8 = 6;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize() {
let request = UpdateNoteModelRequest {
note: UpdateNoteModelEntry {
id: 1514547547030,
model_name: "New Model".to_string(),
fields: HashMap::from([
("New Field 1".to_string(), "Value 1".to_string()),
("New Field 2".to_string(), "Value 2".to_string()),
("New Field 3".to_string(), "Value 3".to_string()),
]),
tags: vec!["new".to_string(), "updated".to_string(), "tags".to_string()],
},
};
let json = serde_json::to_string_pretty(&request).unwrap();
assert_eq!(
json,
r#"{
"note": {
"id": 1514547547030,
"modelName": "New Model",
"fields": {
"New Field 1": "Value 1",
"New Field 2": "Value 2",
"New Field 3": "Value 3"
},
"tags": [
"new",
"updated",
"tags"
]
}
}"#
);
}
}