duoload 0.1.2

Export vocabulary from Duocards
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use crate::duocards::DuocardsClientTrait;
use crate::error::Result;
use crate::output::{OutputBuilder, OutputDestination};
use crate::transfer::DuplicateHandler;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use tokio::time::sleep;

#[derive(Debug, Default, PartialEq)]
pub struct TransferStats {
    pub total_cards: usize,
    pub duplicates: usize,
}

pub struct TransferProcessor<C>
where
    C: DuocardsClientTrait,
{
    client: C,
    deck_id: String,
}

pub struct TransferProcessorWithBuilder<C, B>
where
    C: DuocardsClientTrait,
    B: OutputBuilder,
{
    client: C,
    builder: B,
    duplicates: DuplicateHandler,
    stats: TransferStats,
    deck_id: String,
    start_time: Instant,
    output_path: PathBuf,
}

impl<C> TransferProcessor<C>
where
    C: DuocardsClientTrait,
{
    pub fn new(client: C, deck_id: String) -> Self {
        Self { client, deck_id }
    }

    pub fn output<B: OutputBuilder, P: AsRef<Path>>(
        self,
        builder: B,
        path: P,
    ) -> TransferProcessorWithBuilder<C, B> {
        TransferProcessorWithBuilder {
            client: self.client,
            builder,
            duplicates: DuplicateHandler::new(),
            stats: TransferStats::default(),
            deck_id: self.deck_id,
            start_time: Instant::now(),
            output_path: path.as_ref().to_path_buf(),
        }
    }
}

impl<C, B> TransferProcessorWithBuilder<C, B>
where
    C: DuocardsClientTrait,
    B: OutputBuilder,
{
    pub async fn process(&mut self) -> Result<()> {
        let mut cursor = None;
        let mut page_count = 0;
        let mut total_processed = 0;

        // Print initial message with page limit info if set
        if let Some(limit) = self.client.page_limit() {
            eprintln!("Starting export (limited to {} pages)...", limit);
        } else {
            eprintln!("Starting export...");
        }

        loop {
            page_count += 1;

            // Check if we should continue based on page limit
            if !self.client.should_continue(page_count) {
                eprintln!("Page limit reached ({} pages)", page_count - 1);
                break;
            }

            eprintln!("Fetching page {}...", page_count);

            // Add a delay between page fetches (1 second)
            if page_count > 1 {
                sleep(Duration::from_secs(1)).await;
            }

            // Fetch a page of cards
            let response = self.client.fetch_page(&self.deck_id, cursor).await?;
            let cards = self.client.convert_to_vocabulary_cards(&response);
            let cards_len = cards.len();
            eprintln!("Page {} fetched with {} cards", page_count, cards_len);

            // Process each card
            for card in cards.into_iter() {
                if self.duplicates.is_duplicate(&card.word) {
                    self.stats.duplicates += 1;
                    continue;
                }

                if self.builder.add_note(card)? {
                    self.stats.total_cards += 1;
                }

                total_processed += 1;
                if total_processed % 100 == 0 {
                    eprintln!(
                        "Processed {} cards so far ({} added, {} duplicates) at {:?}",
                        total_processed,
                        self.stats.total_cards,
                        self.stats.duplicates,
                        self.start_time.elapsed()
                    );
                }
            }

            // Check if there are more pages
            if !response.data.node.cards.page_info.has_next_page {
                eprintln!("No more pages to process");
                break;
            }

            cursor = response.data.node.cards.page_info.end_cursor;
        }

        // Print completion message with appropriate context
        if let Some(limit) = self.client.page_limit() {
            eprintln!(
                "Page limit reached ({} pages). Total cards: {}, Duplicates: {} in {:?}",
                limit,
                self.stats.total_cards,
                self.stats.duplicates,
                self.start_time.elapsed()
            );
        } else {
            eprintln!(
                "All pages processed. Total cards: {}, Duplicates: {} in {:?}",
                self.stats.total_cards,
                self.stats.duplicates,
                self.start_time.elapsed()
            );
        }

        // Write the processed data to output
        self.write_output()?;

        // Print final statistics to stderr
        self.print_stats();

        Ok(())
    }

    #[cfg(test)] // Used in tests
    pub fn stats(&self) -> &TransferStats {
        &self.stats
    }

    pub fn print_stats(&self) {
        eprintln!("Export completed successfully!");
        eprintln!("Total cards saved: {}", self.stats.total_cards);
        eprintln!("Duplicates skipped: {}", self.stats.duplicates);
        eprintln!("Total execution time: {:?}", self.start_time.elapsed());
    }

    pub fn write_output(&self) -> Result<()> {
        eprintln!("Writing deck to output...");

        let result = if self.output_path.as_os_str() == "-" {
            // Write to stdout, ensure progress messages go to stderr
            let stdout = io::stdout();
            let mut writer = stdout.lock();
            self.builder.write(OutputDestination::Writer(&mut writer))
        } else {
            // Write to file
            self.builder
                .write(OutputDestination::File(&self.output_path))
        };

        match result {
            Ok(_) => {
                eprintln!("Deck written successfully");
                Ok(())
            }
            Err(e) => {
                eprintln!("Error writing deck: {}", e);
                Err(e)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::duocards::models::{
        Card, CardConnection, CardEdge, Deck, DuocardsResponse, Extensions, LearningStatus,
        PageInfo, ResponseData, VocabularyCard,
    };
    use crate::output::OutputBuilder;
    use std::io::{Cursor, Write};
    use std::sync::Arc;
    use std::sync::Mutex;

    // Test-specific implementations
    #[derive(Clone)]
    struct TestDuocardsClient {
        responses: Arc<Mutex<Vec<DuocardsResponse>>>,
        page_limit: Option<u32>,
    }

    impl TestDuocardsClient {
        fn new(responses: Vec<DuocardsResponse>) -> Self {
            Self {
                responses: Arc::new(Mutex::new(responses)),
                page_limit: None,
            }
        }

        fn with_page_limit(mut self, limit: u32) -> Self {
            self.page_limit = Some(limit);
            self
        }
    }

    #[async_trait::async_trait]
    impl crate::duocards::DuocardsClientTrait for TestDuocardsClient {
        async fn fetch_page(
            &self,
            _deck_id: &str,
            _cursor: Option<String>,
        ) -> Result<DuocardsResponse> {
            let mut responses = self.responses.lock().unwrap();
            if responses.is_empty() {
                panic!("No more test responses available");
            }
            Ok(responses.remove(0))
        }

        fn convert_to_vocabulary_cards(&self, response: &DuocardsResponse) -> Vec<VocabularyCard> {
            response
                .data
                .node
                .cards
                .edges
                .iter()
                .map(|edge| VocabularyCard {
                    word: edge.node.front.clone(),
                    translation: edge.node.back.clone(),
                    example: edge.node.hint.clone(),
                    status: if edge.node.known_count >= 5 {
                        LearningStatus::Known
                    } else if edge.node.known_count > 0 {
                        LearningStatus::Learning
                    } else {
                        LearningStatus::New
                    },
                })
                .collect()
        }

        fn should_continue(&self, current_page: u32) -> bool {
            match self.page_limit {
                Some(limit) => current_page <= limit,
                None => true,
            }
        }

        fn page_limit(&self) -> Option<u32> {
            self.page_limit
        }
    }

    #[derive(Clone)]
    struct TestOutputBuilder {
        added_cards: Arc<Mutex<Vec<VocabularyCard>>>,
    }

    impl TestOutputBuilder {
        fn new() -> Self {
            Self {
                added_cards: Arc::new(Mutex::new(Vec::new())),
            }
        }

        fn get_added_cards(&self) -> Vec<VocabularyCard> {
            self.added_cards.lock().unwrap().clone()
        }
    }

    impl OutputBuilder for TestOutputBuilder {
        fn add_note(&mut self, card: VocabularyCard) -> Result<bool> {
            let mut added_cards = self.added_cards.lock().unwrap();
            if added_cards.iter().any(|c| c.word == card.word) {
                Ok(false)
            } else {
                added_cards.push(card);
                Ok(true)
            }
        }

        fn write(&self, dest: OutputDestination<'_>) -> Result<()> {
            match dest {
                OutputDestination::Writer(writer) => {
                    writer.write_all(b"TEST_OUTPUT")?;
                    Ok(())
                }
                OutputDestination::File(path) => {
                    let file = std::fs::File::create(path)?;
                    let mut writer = std::io::BufWriter::new(file);
                    writer.write_all(b"TEST_OUTPUT")?;
                    writer.flush()?;
                    Ok(())
                }
            }
        }
    }

    fn create_test_response(
        cards: Vec<VocabularyCard>,
        has_next_page: bool,
        end_cursor: Option<String>,
    ) -> DuocardsResponse {
        let card_edges: Vec<CardEdge> = cards
            .into_iter()
            .map(|card| CardEdge {
                node: Card {
                    id: "test-id".to_string(),
                    front: card.word,
                    back: card.translation,
                    hint: card.example,
                    waiting: None,
                    known_count: match card.status {
                        LearningStatus::Known => 5,
                        LearningStatus::Learning => 2,
                        LearningStatus::New => 0,
                    },
                    svg: None,
                    typename: "Card".to_string(),
                },
                cursor: "0".to_string(),
            })
            .collect();

        DuocardsResponse {
            data: ResponseData {
                node: Deck {
                    __typename: "Deck".to_string(),
                    cards: CardConnection {
                        edges: card_edges,
                        page_info: PageInfo {
                            end_cursor,
                            has_next_page,
                        },
                    },
                    id: "test-deck".to_string(),
                },
            },
            extensions: Extensions {
                release_id: Some("test-release".to_string()),
            },
        }
    }

    #[tokio::test]
    async fn test_process_single_page() -> Result<()> {
        // Create test cards
        let cards = vec![
            VocabularyCard {
                word: "hello".to_string(),
                translation: "hola".to_string(),
                example: Some("Hello, world!".to_string()),
                status: LearningStatus::New,
            },
            VocabularyCard {
                word: "world".to_string(),
                translation: "mundo".to_string(),
                example: None,
                status: LearningStatus::Known,
            },
        ];

        // Create test response
        let response = create_test_response(cards.clone(), false, None);

        // Create test client and builder
        let client = TestDuocardsClient::new(vec![response]);
        let builder = TestOutputBuilder::new();

        // Create processor and process cards
        let mut processor = TransferProcessor::new(client, "test-deck".to_string())
            .output(builder, Path::new("test_output.txt"));

        processor.process().await?;
        processor.write_output()?;

        // Verify results
        let stats = processor.stats();
        assert_eq!(stats.total_cards, 2);
        assert_eq!(stats.duplicates, 0);

        // Verify cards were added
        let added_cards = processor.builder.get_added_cards();
        assert_eq!(added_cards.len(), 2);
        assert_eq!(added_cards[0].word, "hello");
        assert_eq!(added_cards[1].word, "world");

        Ok(())
    }

    #[tokio::test]
    async fn test_process_multiple_pages() -> Result<()> {
        // Create test cards for two pages
        let page1_cards = vec![VocabularyCard {
            word: "hello".to_string(),
            translation: "hola".to_string(),
            example: Some("Hello, world!".to_string()),
            status: LearningStatus::New,
        }];

        let page2_cards = vec![VocabularyCard {
            word: "world".to_string(),
            translation: "mundo".to_string(),
            example: None,
            status: LearningStatus::Known,
        }];

        // Create test responses
        let response1 =
            create_test_response(page1_cards.clone(), true, Some("cursor1".to_string()));
        let response2 = create_test_response(page2_cards.clone(), false, None);

        // Create test client and builder
        let client = TestDuocardsClient::new(vec![response1, response2]);
        let builder = TestOutputBuilder::new();

        // Create processor and process cards
        let mut processor = TransferProcessor::new(client, "test-deck".to_string())
            .output(builder, Path::new("test_output.txt"));

        processor.process().await?;
        processor.write_output()?;

        // Verify results
        let stats = processor.stats();
        assert_eq!(stats.total_cards, 2);
        assert_eq!(stats.duplicates, 0);

        // Verify cards were added in correct order
        let added_cards = processor.builder.get_added_cards();
        assert_eq!(added_cards.len(), 2);
        assert_eq!(added_cards[0].word, "hello");
        assert_eq!(added_cards[1].word, "world");

        Ok(())
    }

    #[tokio::test]
    async fn test_process_with_duplicates() -> Result<()> {
        // Create test cards with duplicates
        let cards = vec![
            VocabularyCard {
                word: "hello".to_string(),
                translation: "hola".to_string(),
                example: Some("Hello, world!".to_string()),
                status: LearningStatus::New,
            },
            VocabularyCard {
                word: "hello".to_string(), // duplicate
                translation: "hola".to_string(),
                example: Some("Hello again!".to_string()),
                status: LearningStatus::Learning,
            },
            VocabularyCard {
                word: "world".to_string(),
                translation: "mundo".to_string(),
                example: None,
                status: LearningStatus::Known,
            },
        ];

        // Create test response
        let response = create_test_response(cards.clone(), false, None);

        // Create test client and builder
        let client = TestDuocardsClient::new(vec![response]);
        let builder = TestOutputBuilder::new();

        // Create processor and process cards
        let mut processor = TransferProcessor::new(client, "test-deck".to_string())
            .output(builder, Path::new("test_output.txt"));

        processor.process().await?;
        processor.write_output()?;

        // Verify results
        let stats = processor.stats();
        assert_eq!(stats.total_cards, 2);
        assert_eq!(stats.duplicates, 1);

        // Verify cards were added correctly
        let added_cards = processor.builder.get_added_cards();
        assert_eq!(added_cards.len(), 2);
        assert_eq!(added_cards[0].word, "hello");
        assert_eq!(added_cards[1].word, "world");

        Ok(())
    }

    #[test]
    fn test_write_to_stdout() -> Result<()> {
        let builder = TestOutputBuilder::new();
        let processor =
            TransferProcessor::new(TestDuocardsClient::new(vec![]), "test-deck".to_string())
                .output(builder, Path::new("-"));

        let mut output = Vec::new();
        {
            let mut writer = Cursor::new(&mut output);
            processor
                .builder
                .write(OutputDestination::Writer(&mut writer))?;
        }
        assert_eq!(output, b"TEST_OUTPUT");
        Ok(())
    }

    #[test]
    fn test_write_to_file() -> Result<()> {
        let builder = TestOutputBuilder::new();
        let temp_file = tempfile::NamedTempFile::new()?;
        let processor =
            TransferProcessor::new(TestDuocardsClient::new(vec![]), "test-deck".to_string())
                .output(builder, temp_file.path());

        processor.write_output()?;
        let contents = std::fs::read(temp_file.path())?;
        assert_eq!(contents, b"TEST_OUTPUT");
        Ok(())
    }

    #[tokio::test]
    async fn test_process_with_page_limit() -> Result<()> {
        // Create test cards for three pages
        let page1_cards = vec![VocabularyCard {
            word: "hello".to_string(),
            translation: "hola".to_string(),
            example: Some("Hello, world!".to_string()),
            status: LearningStatus::New,
        }];

        let page2_cards = vec![VocabularyCard {
            word: "world".to_string(),
            translation: "mundo".to_string(),
            example: None,
            status: LearningStatus::Known,
        }];

        let page3_cards = vec![VocabularyCard {
            word: "goodbye".to_string(),
            translation: "adiós".to_string(),
            example: None,
            status: LearningStatus::New,
        }];

        // Create test responses
        let response1 =
            create_test_response(page1_cards.clone(), true, Some("cursor1".to_string()));
        let response2 =
            create_test_response(page2_cards.clone(), true, Some("cursor2".to_string()));
        let response3 = create_test_response(page3_cards.clone(), false, None);

        // Create test client with page limit and builder
        let client =
            TestDuocardsClient::new(vec![response1, response2, response3]).with_page_limit(2);
        let builder = TestOutputBuilder::new();

        // Create processor and process cards
        let mut processor = TransferProcessor::new(client, "test-deck".to_string())
            .output(builder, Path::new("test_output.txt"));

        processor.process().await?;
        processor.write_output()?;

        // Verify results
        let stats = processor.stats();
        assert_eq!(stats.total_cards, 2); // Only first two pages should be processed
        assert_eq!(stats.duplicates, 0);

        // Verify cards were added in correct order
        let added_cards = processor.builder.get_added_cards();
        assert_eq!(added_cards.len(), 2);
        assert_eq!(added_cards[0].word, "hello");
        assert_eq!(added_cards[1].word, "world");

        Ok(())
    }
}