ankit-builder 0.1.0

TOML-based Anki deck builder with .apkg generation and AnkiConnect import
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Export decks from Anki to TOML format.
//!
//! This module provides functionality to pull decks from Anki via AnkiConnect
//! and export them to the TOML format used by ankit-builder.
//!
//! # Example
//!
//! ```no_run
//! use ankit::AnkiClient;
//! use ankit_builder::DeckExporter;
//!
//! # async fn example() -> ankit_builder::Result<()> {
//! let client = AnkiClient::new();
//! let exporter = DeckExporter::new(&client);
//!
//! // Export a deck to DeckDefinition
//! let definition = exporter.export_deck("Japanese::Vocabulary").await?;
//!
//! // Write to TOML file
//! definition.write_toml("japanese.toml")?;
//! # Ok(())
//! # }
//! ```

use std::collections::{HashMap, HashSet};
use std::path::Path;

use ankit::AnkiClient;

use crate::error::{Error, Result};
use crate::schema::{DeckDef, DeckDefinition, ModelDef, NoteDef, PackageInfo, TemplateDef};

/// Exports decks from Anki to TOML format.
///
/// Uses AnkiConnect to fetch deck contents and convert them to a
/// [`DeckDefinition`] that can be serialized to TOML.
pub struct DeckExporter<'a> {
    client: &'a AnkiClient,
}

impl<'a> DeckExporter<'a> {
    /// Create a new exporter with the given AnkiConnect client.
    pub fn new(client: &'a AnkiClient) -> Self {
        Self { client }
    }

    /// Export a deck to a [`DeckDefinition`].
    ///
    /// Fetches all notes in the specified deck, along with the models (note types)
    /// they use, and converts them to a TOML-compatible definition.
    ///
    /// # Arguments
    ///
    /// * `deck_name` - The name of the deck to export (supports hierarchical names like "Parent::Child")
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ankit::AnkiClient;
    /// use ankit_builder::DeckExporter;
    ///
    /// # async fn example() -> ankit_builder::Result<()> {
    /// let client = AnkiClient::new();
    /// let exporter = DeckExporter::new(&client);
    ///
    /// let definition = exporter.export_deck("My Deck").await?;
    /// println!("Exported {} notes", definition.notes.len());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn export_deck(&self, deck_name: &str) -> Result<DeckDefinition> {
        // Find all notes in the deck
        let query = format!("deck:\"{}\"", deck_name);
        let note_ids = self.client.notes().find(&query).await?;

        if note_ids.is_empty() {
            // Return empty definition with just the deck
            return Ok(DeckDefinition {
                package: PackageInfo {
                    name: deck_name.to_string(),
                    version: "1.0.0".to_string(),
                    author: None,
                    description: None,
                },
                models: Vec::new(),
                decks: vec![DeckDef {
                    name: deck_name.to_string(),
                    description: None,
                    id: None,
                }],
                notes: Vec::new(),
                media: Vec::new(),
            });
        }

        // Get note info
        let note_infos = self.client.notes().info(&note_ids).await?;

        // Collect unique model names
        let model_names: HashSet<String> =
            note_infos.iter().map(|n| n.model_name.clone()).collect();

        // Fetch model details
        let mut models = Vec::new();
        for model_name in &model_names {
            let model_def = self.fetch_model(model_name).await?;
            models.push(model_def);
        }

        // Convert notes to NoteDef
        let notes: Vec<NoteDef> = note_infos
            .iter()
            .map(|note| {
                let fields: HashMap<String, String> = note
                    .fields
                    .iter()
                    .map(|(name, field)| (name.clone(), field.value.clone()))
                    .collect();

                NoteDef {
                    deck: deck_name.to_string(),
                    model: note.model_name.clone(),
                    fields,
                    tags: note.tags.clone(),
                    guid: None,
                    note_id: Some(note.note_id),
                }
            })
            .collect();

        Ok(DeckDefinition {
            package: PackageInfo {
                name: deck_name.to_string(),
                version: "1.0.0".to_string(),
                author: None,
                description: None,
            },
            models,
            decks: vec![DeckDef {
                name: deck_name.to_string(),
                description: None,
                id: None,
            }],
            notes,
            media: Vec::new(),
        })
    }

    /// Export multiple decks to a single [`DeckDefinition`].
    ///
    /// Useful for exporting a parent deck and its children, or a set of related decks.
    ///
    /// # Arguments
    ///
    /// * `deck_names` - The names of the decks to export
    /// * `package_name` - The name for the package (used in `[package]` section)
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ankit::AnkiClient;
    /// use ankit_builder::DeckExporter;
    ///
    /// # async fn example() -> ankit_builder::Result<()> {
    /// let client = AnkiClient::new();
    /// let exporter = DeckExporter::new(&client);
    ///
    /// let definition = exporter
    ///     .export_decks(&["Japanese::N5", "Japanese::N4"], "Japanese JLPT")
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn export_decks(
        &self,
        deck_names: &[&str],
        package_name: &str,
    ) -> Result<DeckDefinition> {
        let mut all_notes = Vec::new();
        let mut model_names: HashSet<String> = HashSet::new();
        let mut decks = Vec::new();

        for deck_name in deck_names {
            // Find all notes in this deck
            let query = format!("deck:\"{}\"", deck_name);
            let note_ids = self.client.notes().find(&query).await?;

            // Add deck to list
            decks.push(DeckDef {
                name: deck_name.to_string(),
                description: None,
                id: None,
            });

            if note_ids.is_empty() {
                continue;
            }

            // Get note info
            let note_infos = self.client.notes().info(&note_ids).await?;

            // Collect model names
            for note in &note_infos {
                model_names.insert(note.model_name.clone());
            }

            // Convert notes
            for note in note_infos {
                let fields: HashMap<String, String> = note
                    .fields
                    .iter()
                    .map(|(name, field)| (name.clone(), field.value.clone()))
                    .collect();

                all_notes.push(NoteDef {
                    deck: deck_name.to_string(),
                    model: note.model_name.clone(),
                    fields,
                    tags: note.tags,
                    guid: None,
                    note_id: Some(note.note_id),
                });
            }
        }

        // Fetch model details
        let mut models = Vec::new();
        for model_name in &model_names {
            let model_def = self.fetch_model(model_name).await?;
            models.push(model_def);
        }

        Ok(DeckDefinition {
            package: PackageInfo {
                name: package_name.to_string(),
                version: "1.0.0".to_string(),
                author: None,
                description: None,
            },
            models,
            decks,
            notes: all_notes,
            media: Vec::new(),
        })
    }

    /// Fetch model definition from Anki.
    async fn fetch_model(&self, model_name: &str) -> Result<ModelDef> {
        // Get field names
        let fields = self.client.models().field_names(model_name).await?;

        // Get templates
        let templates_map = self.client.models().templates(model_name).await?;
        let templates: Vec<TemplateDef> = templates_map
            .into_iter()
            .map(|(name, template)| TemplateDef {
                name,
                front: template.front,
                back: template.back,
            })
            .collect();

        // Get CSS styling
        let styling = self.client.models().styling(model_name).await?;
        let css = if styling.css.is_empty() {
            None
        } else {
            Some(styling.css)
        };

        Ok(ModelDef {
            name: model_name.to_string(),
            fields,
            templates,
            css,
            sort_field: None,
            id: None,
            markdown_fields: vec![],
            model_type: None,
        })
    }
}

impl DeckDefinition {
    /// Write the deck definition to a TOML file.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ankit_builder::DeckDefinition;
    ///
    /// # fn example() -> ankit_builder::Result<()> {
    /// let definition = DeckDefinition::from_file("input.toml")?;
    /// // ... modify definition ...
    /// definition.write_toml("output.toml")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn write_toml(&self, path: impl AsRef<Path>) -> Result<()> {
        let content = self.to_toml()?;
        std::fs::write(path, content)?;
        Ok(())
    }

    /// Serialize the deck definition to a TOML string.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use ankit_builder::DeckDefinition;
    ///
    /// # fn example() -> ankit_builder::Result<()> {
    /// let definition = DeckDefinition::from_file("input.toml")?;
    /// let toml_string = definition.to_toml()?;
    /// println!("{}", toml_string);
    /// # Ok(())
    /// # }
    /// ```
    pub fn to_toml(&self) -> Result<String> {
        toml::to_string_pretty(self).map_err(|e| Error::TomlSerialize(e.to_string()))
    }
}

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

    #[test]
    fn test_to_toml_roundtrip() {
        let toml_input = r#"
[package]
name = "Test Deck"
version = "1.0.0"

[[models]]
name = "Basic"
fields = ["Front", "Back"]

[[models.templates]]
name = "Card 1"
front = "{{Front}}"
back = "{{Back}}"

[[decks]]
name = "Test Deck"

[[notes]]
deck = "Test Deck"
model = "Basic"
tags = ["test"]

[notes.fields]
Front = "Question"
Back = "Answer"
"#;

        let definition = DeckDefinition::parse(toml_input).unwrap();
        let toml_output = definition.to_toml().unwrap();

        // Parse the output and verify it matches
        let definition2 = DeckDefinition::parse(&toml_output).unwrap();
        assert_eq!(definition.package.name, definition2.package.name);
        assert_eq!(definition.models.len(), definition2.models.len());
        assert_eq!(definition.notes.len(), definition2.notes.len());
    }

    #[test]
    fn test_note_id_serialization() {
        let toml_input = r#"
[package]
name = "Test"
version = "1.0.0"

[[models]]
name = "Basic"
fields = ["Front", "Back"]

[[models.templates]]
name = "Card 1"
front = "{{Front}}"
back = "{{Back}}"

[[decks]]
name = "Test"

[[notes]]
deck = "Test"
model = "Basic"
note_id = 1234567890

[notes.fields]
Front = "Q"
Back = "A"
"#;

        // Parse TOML with note_id
        let definition = DeckDefinition::parse(toml_input).unwrap();
        assert_eq!(definition.notes[0].note_id, Some(1234567890));

        // Serialize and verify note_id is preserved
        let toml_output = definition.to_toml().unwrap();
        assert!(
            toml_output.contains("note_id = 1234567890"),
            "note_id should be in output: {}",
            toml_output
        );

        // Roundtrip
        let definition2 = DeckDefinition::parse(&toml_output).unwrap();
        assert_eq!(definition2.notes[0].note_id, Some(1234567890));
    }

    #[test]
    fn test_note_id_omitted_when_none() {
        let toml_input = r#"
[package]
name = "Test"
version = "1.0.0"

[[models]]
name = "Basic"
fields = ["Front", "Back"]

[[models.templates]]
name = "Card 1"
front = "{{Front}}"
back = "{{Back}}"

[[decks]]
name = "Test"

[[notes]]
deck = "Test"
model = "Basic"

[notes.fields]
Front = "Q"
Back = "A"
"#;

        let definition = DeckDefinition::parse(toml_input).unwrap();
        assert_eq!(definition.notes[0].note_id, None);

        // Serialize and verify note_id is NOT in output
        let toml_output = definition.to_toml().unwrap();
        assert!(
            !toml_output.contains("note_id"),
            "note_id should be omitted when None: {}",
            toml_output
        );
    }
}