anki_bridge 0.10.2

AnkiBridge is a Rust library that provides a bridge between your Rust code and the Anki application, enabling HTTP communication and seamless data transmission.
Documentation
/*
* 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 serde::Serialize;

use crate::AnkiRequest;

/// Parameters for the "findAndReplaceInModels" action in `AnkiConnect`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FindAndReplaceInModelsRequest {
    /// Model to search in.
    ///
    /// Leave empty to include all models.
    pub model_name: String,
    pub find_text: String,
    pub replace_text: String,
    /// Whether to replace text in the _Front Template_ of the card type.
    pub front: bool,
    /// Whether to replace text in the _Back Template_ of the card type.
    pub back: bool,
    /// Whether to replace text in the _Styling_ of the card type.
    pub css: bool,
}

impl Default for FindAndReplaceInModelsRequest {
    fn default() -> Self {
        Self {
            model_name: "".to_string(),
            find_text: "".to_string(),
            replace_text: "".to_string(),
            front: true,
            back: true,
            css: true,
        }
    }
}

impl AnkiRequest for FindAndReplaceInModelsRequest {
    type Response = usize;

    const ACTION: &'static str = "findAndReplaceInModels";
    const VERSION: u8 = 6;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_serialize() {
        let request = FindAndReplaceInModelsRequest {
            model_name: "My Model".to_string(),
            find_text: "old_text".to_string(),
            replace_text: "new_text".to_string(),
            front: true,
            back: true,
            css: false,
        };

        let json = serde_json::to_string_pretty(&request).unwrap();
        assert_eq!(
            json,
            r#"{
  "modelName": "My Model",
  "findText": "old_text",
  "replaceText": "new_text",
  "front": true,
  "back": true,
  "css": false
}"#
        );
    }

    #[test]
    fn test_serialize_no_model_name() {
        let request = FindAndReplaceInModelsRequest {
            model_name: "".to_string(),
            ..Default::default()
        };

        let json = serde_json::to_string_pretty(&request).unwrap();
        assert_eq!(
            json,
            r#"{
  "modelName": "",
  "findText": "",
  "replaceText": "",
  "front": true,
  "back": true,
  "css": true
}"#
        );
    }

    #[test]
    fn test_deserialize() {
        let json = "2";
        let response: <FindAndReplaceInModelsRequest as AnkiRequest>::Response =
            serde_json::from_str(json).unwrap();

        assert_eq!(response, 2);
    }
}