Skip to main content

ankit/types/
model.rs

1//! Model-related types.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Information about a model (note type).
7#[derive(Debug, Clone, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub struct ModelInfo {
10    /// Model name.
11    pub name: String,
12    /// Model ID.
13    #[serde(rename = "id")]
14    pub model_id: i64,
15}
16
17/// Field information for a model.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct ModelField {
20    /// Field name.
21    pub name: String,
22    /// Field ordinal position.
23    #[serde(default)]
24    pub ord: i32,
25    /// Whether this is a sticky field.
26    #[serde(default)]
27    pub sticky: bool,
28    /// Whether to use right-to-left text.
29    #[serde(default)]
30    pub rtl: bool,
31    /// Font name for editing.
32    #[serde(default)]
33    pub font: String,
34    /// Font size for editing.
35    #[serde(default)]
36    pub size: i32,
37    /// Field description.
38    #[serde(default)]
39    pub description: String,
40}
41
42/// Card template for a model (used in responses from modelTemplates).
43///
44/// Note: When retrieved via `modelTemplates`, the template name is the HashMap key,
45/// not a field in this struct.
46#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct CardTemplate {
48    /// Front template HTML.
49    #[serde(rename = "Front")]
50    pub front: String,
51    /// Back template HTML.
52    #[serde(rename = "Back")]
53    pub back: String,
54}
55
56/// Card template for creating a model (includes name).
57#[derive(Debug, Clone, Serialize)]
58pub struct CreateCardTemplate {
59    /// Template name.
60    #[serde(rename = "Name")]
61    pub name: String,
62    /// Front template HTML.
63    #[serde(rename = "Front")]
64    pub front: String,
65    /// Back template HTML.
66    #[serde(rename = "Back")]
67    pub back: String,
68}
69
70/// Font information for a field.
71#[derive(Debug, Clone, Deserialize)]
72pub struct FieldFont {
73    /// Font name.
74    pub font: String,
75    /// Font size.
76    pub size: i32,
77}
78
79/// Parameters for creating a new model.
80#[derive(Debug, Clone, Serialize)]
81#[serde(rename_all = "camelCase")]
82pub struct CreateModelParams {
83    /// Model name.
84    pub model_name: String,
85    /// Field names for the model.
86    pub in_order_fields: Vec<String>,
87    /// CSS styling for the model.
88    pub css: String,
89    /// Whether this is a cloze model.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub is_cloze: Option<bool>,
92    /// Card templates.
93    pub card_templates: Vec<CreateCardTemplate>,
94}
95
96impl CreateModelParams {
97    /// Create new model parameters.
98    pub fn new(name: impl Into<String>) -> Self {
99        Self {
100            model_name: name.into(),
101            in_order_fields: Vec::new(),
102            css: String::new(),
103            is_cloze: None,
104            card_templates: Vec::new(),
105        }
106    }
107
108    /// Add a field to the model.
109    pub fn field(mut self, name: impl Into<String>) -> Self {
110        self.in_order_fields.push(name.into());
111        self
112    }
113
114    /// Set the CSS styling.
115    pub fn css(mut self, css: impl Into<String>) -> Self {
116        self.css = css.into();
117        self
118    }
119
120    /// Set whether this is a cloze model.
121    pub fn cloze(mut self, is_cloze: bool) -> Self {
122        self.is_cloze = Some(is_cloze);
123        self
124    }
125
126    /// Add a card template.
127    pub fn template(
128        mut self,
129        name: impl Into<String>,
130        front: impl Into<String>,
131        back: impl Into<String>,
132    ) -> Self {
133        self.card_templates.push(CreateCardTemplate {
134            name: name.into(),
135            front: front.into(),
136            back: back.into(),
137        });
138        self
139    }
140}
141
142/// Parameters for find and replace in models.
143#[derive(Debug, Clone, Serialize)]
144#[serde(rename_all = "camelCase")]
145pub struct FindReplaceParams {
146    /// Notes to search in (empty for all notes).
147    #[serde(skip_serializing_if = "Vec::is_empty")]
148    pub notes: Vec<i64>,
149    /// Action (usually "findAndReplaceInModels").
150    #[serde(skip_serializing_if = "Option::is_none")]
151    pub action: Option<String>,
152    /// Model name.
153    pub model_name: String,
154    /// Field name.
155    pub field_name: String,
156    /// Search pattern.
157    pub find_text: String,
158    /// Replacement text.
159    pub replace_text: String,
160    /// Use regex.
161    #[serde(skip_serializing_if = "Option::is_none")]
162    pub regex: Option<bool>,
163    /// Match case.
164    #[serde(skip_serializing_if = "Option::is_none")]
165    pub match_case: Option<bool>,
166}
167
168impl FindReplaceParams {
169    /// Create new find and replace parameters.
170    pub fn new(
171        model_name: impl Into<String>,
172        field_name: impl Into<String>,
173        find: impl Into<String>,
174        replace: impl Into<String>,
175    ) -> Self {
176        Self {
177            notes: Vec::new(),
178            action: None,
179            model_name: model_name.into(),
180            field_name: field_name.into(),
181            find_text: find.into(),
182            replace_text: replace.into(),
183            regex: None,
184            match_case: None,
185        }
186    }
187
188    /// Limit to specific notes.
189    pub fn notes(mut self, notes: Vec<i64>) -> Self {
190        self.notes = notes;
191        self
192    }
193
194    /// Enable regex matching.
195    pub fn regex(mut self, enabled: bool) -> Self {
196        self.regex = Some(enabled);
197        self
198    }
199
200    /// Enable case-sensitive matching.
201    pub fn match_case(mut self, enabled: bool) -> Self {
202        self.match_case = Some(enabled);
203        self
204    }
205}
206
207/// Model styling information.
208#[derive(Debug, Clone, Deserialize)]
209pub struct ModelStyling {
210    /// CSS styling.
211    pub css: String,
212}
213
214/// Fields on templates response.
215pub type FieldsOnTemplates = HashMap<String, Vec<Vec<String>>>;