use serde::Serialize;
use std::collections::HashMap;
use crate::AnkiRequest;
use crate::entities::NoteId;
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize)]
pub struct UpdateNoteRequest {
pub note: UpdateNoteEntry,
}
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize)]
pub struct UpdateNoteEntry {
pub id: NoteId,
#[serde(serialize_with = "crate::serialize::hashmap")]
pub fields: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
}
impl AnkiRequest for UpdateNoteRequest {
type Response = ();
const ACTION: &'static str = "updateNote";
const VERSION: u8 = 6;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialize() {
let request = UpdateNoteRequest {
note: UpdateNoteEntry {
id: 1514547547030,
fields: HashMap::from([
("Front".to_string(), "new front content".to_string()),
("Back".to_string(), "new back content".to_string()),
]),
tags: Some(vec!["new".to_string(), "tags".to_string()]),
},
};
let json = serde_json::to_string_pretty(&request).unwrap();
assert_eq!(
json,
r#"{
"note": {
"id": 1514547547030,
"fields": {
"Back": "new back content",
"Front": "new front content"
},
"tags": [
"new",
"tags"
]
}
}"#
);
}
#[test]
fn test_serialize_no_fields() {
let request = UpdateNoteRequest {
note: UpdateNoteEntry {
id: 1514547547030,
fields: HashMap::new(),
..Default::default()
},
};
let json = serde_json::to_string_pretty(&request).unwrap();
assert_eq!(
json,
r#"{
"note": {
"id": 1514547547030,
"fields": {}
}
}"#
);
}
#[test]
fn test_serialize_no_tags() {
let request = UpdateNoteRequest {
note: UpdateNoteEntry {
id: 1514547547030,
tags: None,
..Default::default()
},
};
let json = serde_json::to_string_pretty(&request).unwrap();
assert_eq!(
json,
r#"{
"note": {
"id": 1514547547030,
"fields": {}
}
}"#
);
}
}