chatpack 0.5.1

Prepare chat data for RAG / LLM ingestion. Supports Telegram, WhatsApp, Instagram, Discord.
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
//! Unified parser API for chat exports.
//!
//! This module provides a platform-agnostic interface for parsing chat exports.
//! All platform parsers implement the [`Parser`] trait, enabling consistent
//! usage patterns across Telegram, WhatsApp, Instagram, and Discord.
//!
//! # Overview
//!
//! The module provides:
//! - [`Parser`] - Unified trait for all parsers
//! - [`Platform`] - Enum for dynamic parser selection
//! - [`create_parser`] - Factory function for standard parsers
//! - [`create_streaming_parser`] - Factory function for memory-efficient streaming
//!
//! # Examples
//!
//! ## Basic Parsing
//!
//! ```no_run
//! # #[cfg(feature = "telegram")]
//! # fn main() -> chatpack::Result<()> {
//! use chatpack::parser::{Parser, Platform, create_parser};
//!
//! // Create parser dynamically
//! let parser = create_parser(Platform::Telegram);
//! let messages = parser.parse("export.json".as_ref())?;
//!
//! println!("Parsed {} messages", messages.len());
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "telegram"))]
//! # fn main() {}
//! ```
//!
//! ## Parse from String
//!
//! Useful for WASM or testing:
//!
//! ```
//! # #[cfg(feature = "whatsapp")]
//! # fn main() -> chatpack::Result<()> {
//! use chatpack::parser::{Parser, create_parser, Platform};
//!
//! let content = "[1/15/24, 10:30:45 AM] Alice: Hello!";
//! let parser = create_parser(Platform::WhatsApp);
//! let messages = parser.parse_str(content)?;
//!
//! assert_eq!(messages[0].sender, "Alice");
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "whatsapp"))]
//! # fn main() {}
//! ```
//!
//! ## Streaming Large Files
//!
//! Process files that don't fit in memory:
//!
//! ```no_run
//! # #[cfg(all(feature = "telegram", feature = "streaming"))]
//! # fn main() -> chatpack::Result<()> {
//! use chatpack::parser::{Parser, Platform, create_streaming_parser};
//!
//! let parser = create_streaming_parser(Platform::Telegram);
//!
//! for result in parser.stream("10gb_export.json".as_ref())? {
//!     let msg = result?;
//!     // Process one message at a time
//! }
//! # Ok(())
//! # }
//! # #[cfg(not(all(feature = "telegram", feature = "streaming")))]
//! # fn main() {}
//! ```

use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::Message;
use crate::error::ChatpackError;

#[cfg(feature = "streaming")]
use crate::streaming::MessageIterator;

/// Supported messaging platforms for chat export parsing.
///
/// Each variant corresponds to a specific export format and parser implementation.
/// Use with [`create_parser`] or [`create_streaming_parser`] for dynamic parser selection.
///
/// # Aliases
///
/// All platforms support short aliases for convenience:
/// - `telegram` / `tg`
/// - `whatsapp` / `wa`
/// - `instagram` / `ig`
/// - `discord` / `dc`
///
/// # Examples
///
/// ```
/// use chatpack::parser::Platform;
/// use std::str::FromStr;
///
/// // Parse from string (case-insensitive)
/// let platform = Platform::from_str("telegram")?;
/// assert_eq!(platform, Platform::Telegram);
///
/// // Aliases work too
/// let platform = Platform::from_str("tg")?;
/// assert_eq!(platform, Platform::Telegram);
///
/// // Get file extension
/// assert_eq!(Platform::WhatsApp.default_extension(), "txt");
/// assert_eq!(Platform::Telegram.default_extension(), "json");
/// # Ok::<(), String>(())
/// ```
///
/// # Serialization
///
/// Serializes to lowercase strings, deserializes with alias support:
///
/// ```
/// use chatpack::parser::Platform;
///
/// let json = serde_json::to_string(&Platform::Telegram)?;
/// assert_eq!(json, "\"telegram\"");
///
/// // Deserialize with alias
/// let platform: Platform = serde_json::from_str("\"tg\"")?;
/// assert_eq!(platform, Platform::Telegram);
/// # Ok::<(), serde_json::Error>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Platform {
    /// Telegram Desktop JSON exports.
    ///
    /// Parses the `result.json` file from "Export chat history" feature.
    /// Handles service messages, forwarded messages, and reply chains.
    #[serde(alias = "tg")]
    Telegram,

    /// WhatsApp TXT exports from iOS and Android.
    ///
    /// Auto-detects locale-specific date formats (US, EU, RU variants).
    /// Handles multiline messages and system notifications.
    #[serde(alias = "wa")]
    WhatsApp,

    /// Instagram JSON exports from Meta's data download.
    ///
    /// Automatically fixes Mojibake encoding issues in non-ASCII text.
    /// Parses direct messages from the `messages/` directory.
    #[serde(alias = "ig")]
    Instagram,

    /// Discord exports from DiscordChatExporter tool.
    ///
    /// Supports multiple formats: JSON, TXT, and CSV.
    /// Preserves attachments, stickers, and reply references.
    #[serde(alias = "dc")]
    Discord,
}

impl Platform {
    /// Returns the default file extension for exports from this platform.
    ///
    /// # Examples
    ///
    /// ```
    /// use chatpack::parser::Platform;
    ///
    /// assert_eq!(Platform::Telegram.default_extension(), "json");
    /// assert_eq!(Platform::WhatsApp.default_extension(), "txt");
    /// ```
    pub fn default_extension(&self) -> &'static str {
        match self {
            Platform::WhatsApp => "txt",
            Platform::Telegram | Platform::Instagram | Platform::Discord => "json",
        }
    }

    /// Returns all valid platform names and aliases.
    ///
    /// Useful for CLI help text or validation messages.
    ///
    /// # Examples
    ///
    /// ```
    /// use chatpack::parser::Platform;
    ///
    /// let names = Platform::all_names();
    /// assert!(names.contains(&"telegram"));
    /// assert!(names.contains(&"tg")); // alias
    /// ```
    pub fn all_names() -> &'static [&'static str] {
        &[
            "telegram",
            "tg",
            "whatsapp",
            "wa",
            "instagram",
            "ig",
            "discord",
            "dc",
        ]
    }

    /// Returns all available platform variants.
    ///
    /// # Examples
    ///
    /// ```
    /// use chatpack::parser::Platform;
    ///
    /// for platform in Platform::all() {
    ///     println!("{}: .{}", platform, platform.default_extension());
    /// }
    /// ```
    pub fn all() -> &'static [Platform] {
        &[
            Platform::Telegram,
            Platform::WhatsApp,
            Platform::Instagram,
            Platform::Discord,
        ]
    }
}

impl std::fmt::Display for Platform {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Platform::Telegram => write!(f, "Telegram"),
            Platform::WhatsApp => write!(f, "WhatsApp"),
            Platform::Instagram => write!(f, "Instagram"),
            Platform::Discord => write!(f, "Discord"),
        }
    }
}

impl std::str::FromStr for Platform {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "telegram" | "tg" => Ok(Platform::Telegram),
            "whatsapp" | "wa" => Ok(Platform::WhatsApp),
            "instagram" | "ig" => Ok(Platform::Instagram),
            "discord" | "dc" => Ok(Platform::Discord),
            _ => Err(format!(
                "Unknown platform: '{}'. Expected one of: {}",
                s,
                Platform::all_names().join(", ")
            )),
        }
    }
}

/// Iterator over parsed messages with progress tracking.
///
/// Wraps a streaming parser's [`MessageIterator`]
/// and converts errors to [`ChatpackError`]. Provides progress information for
/// long-running operations.
///
/// # Examples
///
/// ```no_run
/// # #[cfg(all(feature = "telegram", feature = "streaming"))]
/// # fn main() -> chatpack::Result<()> {
/// use chatpack::parser::{Parser, Platform, create_streaming_parser};
///
/// let parser = create_streaming_parser(Platform::Telegram);
/// let mut count = 0;
///
/// for result in parser.stream("export.json".as_ref())? {
///     let msg = result?;
///     count += 1;
/// }
///
/// println!("Processed {} messages", count);
/// # Ok(())
/// # }
/// # #[cfg(not(all(feature = "telegram", feature = "streaming")))]
/// # fn main() {}
/// ```
#[cfg(feature = "streaming")]
pub struct ParseIterator {
    inner: Box<dyn MessageIterator>,
}

#[cfg(feature = "streaming")]
impl ParseIterator {
    /// Creates a new parse iterator from a message iterator.
    pub fn new(inner: Box<dyn MessageIterator>) -> Self {
        Self { inner }
    }

    /// Returns the progress as a percentage (0.0 to 100.0).
    ///
    /// Returns `None` if progress cannot be determined (e.g., unknown file size).
    pub fn progress(&self) -> Option<f64> {
        self.inner.progress()
    }

    /// Returns the number of bytes processed so far.
    pub fn bytes_processed(&self) -> u64 {
        self.inner.bytes_processed()
    }

    /// Returns the total file size in bytes, if known.
    pub fn total_bytes(&self) -> Option<u64> {
        self.inner.total_bytes()
    }
}

#[cfg(feature = "streaming")]
impl Iterator for ParseIterator {
    type Item = Result<Message, ChatpackError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner
            .next()
            .map(|result| result.map_err(ChatpackError::from))
    }
}

/// Unified trait for parsing chat exports from any platform.
///
/// All platform-specific parsers implement this trait, providing a consistent
/// interface for parsing chat exports regardless of the source platform.
///
/// # Required Methods
///
/// | Method | Description |
/// |--------|-------------|
/// | [`name`](Parser::name) | Human-readable parser name |
/// | [`platform`](Parser::platform) | Platform enum variant |
/// | [`parse`](Parser::parse) | Parse file into memory |
/// | [`parse_str`](Parser::parse_str) | Parse from string |
///
/// # Optional Methods
///
/// | Method | Default | Description |
/// |--------|---------|-------------|
/// | [`stream`](Parser::stream) | Falls back to `parse` | Memory-efficient streaming |
/// | [`supports_streaming`](Parser::supports_streaming) | `false` | Native streaming support |
/// | [`recommended_buffer_size`](Parser::recommended_buffer_size) | 64KB | Buffer size hint |
///
/// # Examples
///
/// Using a parser directly:
///
/// ```no_run
/// # #[cfg(feature = "telegram")]
/// # fn main() -> chatpack::Result<()> {
/// use chatpack::parser::Parser;
/// use chatpack::parsers::TelegramParser;
///
/// let parser = TelegramParser::new();
///
/// // Parse from file
/// let messages = parser.parse("export.json".as_ref())?;
///
/// // Parse from string
/// let json = r#"{"messages": []}"#;
/// let messages = parser.parse_str(json)?;
/// # Ok(())
/// # }
/// # #[cfg(not(feature = "telegram"))]
/// # fn main() {}
/// ```
///
/// Using the factory function:
///
/// ```no_run
/// # #[cfg(feature = "telegram")]
/// # fn main() -> chatpack::Result<()> {
/// use chatpack::parser::{Parser, Platform, create_parser};
///
/// let parser = create_parser(Platform::Telegram);
/// let messages = parser.parse("export.json".as_ref())?;
/// # Ok(())
/// # }
/// # #[cfg(not(feature = "telegram"))]
/// # fn main() {}
/// ```
pub trait Parser: Send + Sync {
    /// Returns the human-readable name of this parser.
    ///
    /// # Example
    ///
    /// ```rust
    /// # #[cfg(feature = "telegram")]
    /// # fn main() {
    /// use chatpack::parser::Parser;
    /// use chatpack::parsers::TelegramParser;
    ///
    /// let parser = TelegramParser::new();
    /// assert_eq!(parser.name(), "Telegram");
    /// # }
    /// # #[cfg(not(feature = "telegram"))]
    /// # fn main() {}
    /// ```
    fn name(&self) -> &'static str;

    /// Returns the platform this parser handles.
    fn platform(&self) -> Platform;

    /// Parses a chat export file and returns all messages.
    ///
    /// This method loads the entire file into memory, which is suitable
    /// for files up to ~500MB. For larger files, use [`stream`](Parser::stream).
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the export file
    ///
    /// # Errors
    ///
    /// Returns [`ChatpackError`] if:
    /// - File cannot be read ([`ChatpackError::Io`])
    /// - Content cannot be parsed ([`ChatpackError::Parse`])
    fn parse(&self, path: &Path) -> Result<Vec<Message>, ChatpackError>;

    /// Parses chat content from a string.
    ///
    /// This is useful for:
    /// - WASM environments without file system access
    /// - Testing with inline data
    /// - Processing content already in memory
    ///
    /// # Arguments
    ///
    /// * `content` - Raw export content as a string
    ///
    /// # Errors
    ///
    /// Returns [`ChatpackError::Parse`] if content cannot be parsed.
    fn parse_str(&self, content: &str) -> Result<Vec<Message>, ChatpackError>;

    /// Parses a chat export file (convenience method accepting &str path).
    ///
    /// This is equivalent to `parse(Path::new(path))`.
    fn parse_file(&self, path: &str) -> Result<Vec<Message>, ChatpackError> {
        self.parse(Path::new(path))
    }

    /// Streams messages from a file for memory-efficient processing.
    ///
    /// By default, this falls back to loading the entire file and returning
    /// an iterator over the messages. Parsers that support native streaming
    /// should override this method.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the export file
    ///
    /// # Returns
    ///
    /// An iterator that yields `Result<Message, ChatpackError>` for each message.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # #[cfg(feature = "telegram")]
    /// # fn main() -> chatpack::Result<()> {
    /// use chatpack::parser::Parser;
    /// use chatpack::parsers::TelegramParser;
    /// use std::path::Path;
    ///
    /// let parser = TelegramParser::new();
    /// for result in parser.stream(Path::new("large_file.json"))? {
    ///     if let Ok(msg) = result {
    ///         println!("{}: {}", msg.sender, msg.content);
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// # #[cfg(not(feature = "telegram"))]
    /// # fn main() {}
    /// ```
    fn stream(
        &self,
        path: &Path,
    ) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError>
    {
        // Default implementation: load everything into memory
        let messages = self.parse(path)?;
        Ok(Box::new(messages.into_iter().map(Ok)))
    }

    /// Streams messages (convenience method accepting &str path).
    fn stream_file(
        &self,
        path: &str,
    ) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError>
    {
        self.stream(Path::new(path))
    }

    /// Returns whether this parser supports native streaming.
    ///
    /// If `false`, the [`stream`](Parser::stream) method will load the entire
    /// file into memory before iterating.
    fn supports_streaming(&self) -> bool {
        false
    }

    /// Returns the recommended buffer size for streaming.
    ///
    /// Only relevant if [`supports_streaming`](Parser::supports_streaming) returns `true`.
    fn recommended_buffer_size(&self) -> usize {
        64 * 1024 // 64KB default
    }
}

/// Creates a parser for the specified platform with default configuration.
///
/// This is the primary factory function for creating parsers dynamically.
/// The returned parser loads the entire file into memory, which is suitable
/// for files up to ~500MB.
///
/// For larger files, use [`create_streaming_parser`] instead.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "telegram")]
/// # fn main() {
/// use chatpack::parser::{Platform, create_parser};
///
/// let parser = create_parser(Platform::Telegram);
/// assert_eq!(parser.name(), "Telegram");
/// assert_eq!(parser.platform(), Platform::Telegram);
/// # }
/// # #[cfg(not(feature = "telegram"))]
/// # fn main() {}
/// ```
///
/// # Panics
///
/// Panics if the corresponding feature is not enabled. Enable features in `Cargo.toml`:
///
/// ```toml
/// [dependencies]
/// chatpack = { version = "0.5", features = ["telegram"] }
/// ```
pub fn create_parser(platform: Platform) -> Box<dyn Parser> {
    match platform {
        #[cfg(feature = "telegram")]
        Platform::Telegram => Box::new(crate::parsers::TelegramParser::new()),
        #[cfg(feature = "whatsapp")]
        Platform::WhatsApp => Box::new(crate::parsers::WhatsAppParser::new()),
        #[cfg(feature = "instagram")]
        Platform::Instagram => Box::new(crate::parsers::InstagramParser::new()),
        #[cfg(feature = "discord")]
        Platform::Discord => Box::new(crate::parsers::DiscordParser::new()),
        // Fallback for when features are disabled
        #[allow(unreachable_patterns)]
        _ => panic!(
            "Parser for {:?} is not enabled. Enable the corresponding feature.",
            platform
        ),
    }
}

/// Creates a parser optimized for streaming large files.
///
/// The returned parser is configured for memory-efficient processing of files
/// that may be larger than available RAM. Use the [`stream`](Parser::stream)
/// method to process messages one at a time.
///
/// # When to Use
///
/// - Files larger than 500MB
/// - Memory-constrained environments
/// - When you need progress tracking
///
/// For smaller files, [`create_parser`] is simpler and often faster.
///
/// # Examples
///
/// ```no_run
/// # #[cfg(all(feature = "telegram", feature = "streaming"))]
/// # fn main() -> chatpack::Result<()> {
/// use chatpack::parser::{Parser, Platform, create_streaming_parser};
///
/// let parser = create_streaming_parser(Platform::Telegram);
/// assert!(parser.supports_streaming());
///
/// let mut count = 0;
/// for result in parser.stream("10gb_export.json".as_ref())? {
///     let _msg = result?;
///     count += 1;
///     if count % 100_000 == 0 {
///         println!("Processed {} messages", count);
///     }
/// }
/// # Ok(())
/// # }
/// # #[cfg(not(all(feature = "telegram", feature = "streaming")))]
/// # fn main() {}
/// ```
///
/// # Panics
///
/// Panics if the corresponding feature is not enabled.
pub fn create_streaming_parser(platform: Platform) -> Box<dyn Parser> {
    match platform {
        #[cfg(feature = "telegram")]
        Platform::Telegram => Box::new(crate::parsers::TelegramParser::with_streaming()),
        #[cfg(feature = "whatsapp")]
        Platform::WhatsApp => Box::new(crate::parsers::WhatsAppParser::with_streaming()),
        #[cfg(feature = "instagram")]
        Platform::Instagram => Box::new(crate::parsers::InstagramParser::with_streaming()),
        #[cfg(feature = "discord")]
        Platform::Discord => Box::new(crate::parsers::DiscordParser::with_streaming()),
        // Fallback for when features are disabled
        #[allow(unreachable_patterns)]
        _ => panic!(
            "Streaming parser for {:?} is not enabled. Enable the corresponding feature.",
            platform
        ),
    }
}

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

    // =========================================================================
    // Platform::from_str tests
    // =========================================================================

    #[test]
    fn test_platform_from_str() {
        assert_eq!(Platform::from_str("telegram").unwrap(), Platform::Telegram);
        assert_eq!(Platform::from_str("tg").unwrap(), Platform::Telegram);
        assert_eq!(Platform::from_str("TELEGRAM").unwrap(), Platform::Telegram);
        assert_eq!(Platform::from_str("whatsapp").unwrap(), Platform::WhatsApp);
        assert_eq!(Platform::from_str("wa").unwrap(), Platform::WhatsApp);
        assert_eq!(
            Platform::from_str("instagram").unwrap(),
            Platform::Instagram
        );
        assert_eq!(Platform::from_str("ig").unwrap(), Platform::Instagram);
        assert_eq!(Platform::from_str("discord").unwrap(), Platform::Discord);
        assert_eq!(Platform::from_str("dc").unwrap(), Platform::Discord);
    }

    #[test]
    fn test_platform_from_str_case_insensitive() {
        // Test various case combinations
        assert_eq!(Platform::from_str("TeLegRaM").unwrap(), Platform::Telegram);
        assert_eq!(Platform::from_str("TG").unwrap(), Platform::Telegram);
        assert_eq!(Platform::from_str("WhAtSaPp").unwrap(), Platform::WhatsApp);
        assert_eq!(Platform::from_str("WA").unwrap(), Platform::WhatsApp);
        assert_eq!(
            Platform::from_str("InStAgRaM").unwrap(),
            Platform::Instagram
        );
        assert_eq!(Platform::from_str("IG").unwrap(), Platform::Instagram);
        assert_eq!(Platform::from_str("DiScOrD").unwrap(), Platform::Discord);
        assert_eq!(Platform::from_str("DC").unwrap(), Platform::Discord);
    }

    #[test]
    fn test_platform_from_str_error() {
        let err = Platform::from_str("unknown").unwrap_err();
        assert!(err.contains("Unknown platform"));
        assert!(err.contains("unknown"));

        let err = Platform::from_str("").unwrap_err();
        assert!(err.contains("Unknown platform"));

        let err = Platform::from_str("telegramx").unwrap_err();
        assert!(err.contains("Unknown platform"));
    }

    // =========================================================================
    // Platform display tests
    // =========================================================================

    #[test]
    fn test_platform_display() {
        assert_eq!(Platform::Telegram.to_string(), "Telegram");
        assert_eq!(Platform::WhatsApp.to_string(), "WhatsApp");
        assert_eq!(Platform::Instagram.to_string(), "Instagram");
        assert_eq!(Platform::Discord.to_string(), "Discord");
    }

    // =========================================================================
    // Platform default_extension tests
    // =========================================================================

    #[test]
    fn test_platform_default_extension() {
        assert_eq!(Platform::Telegram.default_extension(), "json");
        assert_eq!(Platform::WhatsApp.default_extension(), "txt");
        assert_eq!(Platform::Instagram.default_extension(), "json");
        assert_eq!(Platform::Discord.default_extension(), "json");
    }

    // =========================================================================
    // Platform::all and Platform::all_names tests
    // =========================================================================

    #[test]
    fn test_platform_all() {
        let all = Platform::all();
        assert_eq!(all.len(), 4);
        assert!(all.contains(&Platform::Telegram));
        assert!(all.contains(&Platform::WhatsApp));
        assert!(all.contains(&Platform::Instagram));
        assert!(all.contains(&Platform::Discord));
    }

    #[test]
    fn test_platform_all_names() {
        let names = Platform::all_names();
        assert!(names.contains(&"telegram"));
        assert!(names.contains(&"tg"));
        assert!(names.contains(&"whatsapp"));
        assert!(names.contains(&"wa"));
        assert!(names.contains(&"instagram"));
        assert!(names.contains(&"ig"));
        assert!(names.contains(&"discord"));
        assert!(names.contains(&"dc"));
    }

    // =========================================================================
    // Platform serde tests
    // =========================================================================

    #[test]
    fn test_platform_serde() {
        let platform = Platform::Telegram;
        let json = serde_json::to_string(&platform).expect("serialize failed");
        assert_eq!(json, "\"telegram\"");

        let parsed: Platform = serde_json::from_str("\"telegram\"").expect("deserialize failed");
        assert_eq!(parsed, Platform::Telegram);

        // Test alias deserialization
        let parsed: Platform = serde_json::from_str("\"tg\"").expect("deserialize failed");
        assert_eq!(parsed, Platform::Telegram);

        let parsed: Platform = serde_json::from_str("\"wa\"").expect("deserialize failed");
        assert_eq!(parsed, Platform::WhatsApp);
    }

    #[test]
    fn test_platform_serde_all_variants() {
        for platform in Platform::all() {
            let json = serde_json::to_string(platform).expect("serialize failed");
            let parsed: Platform = serde_json::from_str(&json).expect("deserialize failed");
            assert_eq!(parsed, *platform);
        }
    }

    // =========================================================================
    // Platform traits tests
    // =========================================================================

    #[test]
    fn test_platform_clone_copy() {
        let p1 = Platform::Telegram;
        let p2 = p1; // Copy
        let p3 = p1;
        assert_eq!(p1, p2);
        assert_eq!(p1, p3);
    }

    #[test]
    fn test_platform_debug() {
        let debug = format!("{:?}", Platform::Telegram);
        assert!(debug.contains("Telegram"));
    }

    #[test]
    fn test_platform_eq_hash() {
        use std::collections::HashSet;
        let mut set = HashSet::new();
        set.insert(Platform::Telegram);
        set.insert(Platform::WhatsApp);
        set.insert(Platform::Telegram); // Duplicate
        assert_eq!(set.len(), 2);
        assert!(set.contains(&Platform::Telegram));
        assert!(set.contains(&Platform::WhatsApp));
    }

    // =========================================================================
    // create_parser tests
    // =========================================================================

    #[cfg(feature = "telegram")]
    #[test]
    fn test_create_parser_telegram() {
        let parser = create_parser(Platform::Telegram);
        assert_eq!(parser.name(), "Telegram");
        assert_eq!(parser.platform(), Platform::Telegram);
        assert!(!parser.supports_streaming());
    }

    #[cfg(feature = "whatsapp")]
    #[test]
    fn test_create_parser_whatsapp() {
        let parser = create_parser(Platform::WhatsApp);
        assert_eq!(parser.name(), "WhatsApp");
        assert_eq!(parser.platform(), Platform::WhatsApp);
    }

    #[cfg(feature = "instagram")]
    #[test]
    fn test_create_parser_instagram() {
        let parser = create_parser(Platform::Instagram);
        assert_eq!(parser.name(), "Instagram");
        assert_eq!(parser.platform(), Platform::Instagram);
    }

    #[cfg(feature = "discord")]
    #[test]
    fn test_create_parser_discord() {
        let parser = create_parser(Platform::Discord);
        assert_eq!(parser.name(), "Discord");
        assert_eq!(parser.platform(), Platform::Discord);
    }

    // =========================================================================
    // create_streaming_parser tests
    // =========================================================================

    #[cfg(feature = "telegram")]
    #[test]
    fn test_create_streaming_parser_telegram() {
        let parser = create_streaming_parser(Platform::Telegram);
        assert_eq!(parser.name(), "Telegram");
        assert!(parser.supports_streaming());
        assert!(parser.recommended_buffer_size() >= 64 * 1024);
    }

    #[cfg(feature = "whatsapp")]
    #[test]
    fn test_create_streaming_parser_whatsapp() {
        let parser = create_streaming_parser(Platform::WhatsApp);
        assert_eq!(parser.name(), "WhatsApp");
        assert!(parser.supports_streaming());
    }

    #[cfg(feature = "instagram")]
    #[test]
    fn test_create_streaming_parser_instagram() {
        let parser = create_streaming_parser(Platform::Instagram);
        assert_eq!(parser.name(), "Instagram");
        assert!(parser.supports_streaming());
    }

    #[cfg(feature = "discord")]
    #[test]
    fn test_create_streaming_parser_discord() {
        let parser = create_streaming_parser(Platform::Discord);
        assert_eq!(parser.name(), "Discord");
        assert!(parser.supports_streaming());
    }

    // =========================================================================
    // Parser trait method tests
    // =========================================================================

    #[cfg(feature = "telegram")]
    #[test]
    fn test_parser_parse_str() {
        let parser = create_parser(Platform::Telegram);
        let json = r#"{"messages": [{"id": 1, "type": "message", "date_unixtime": "1234567890", "from": "Alice", "text": "Hello"}]}"#;
        let messages = parser.parse_str(json).expect("parse failed");
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].sender, "Alice");
        assert_eq!(messages[0].content, "Hello");
    }

    #[cfg(feature = "telegram")]
    #[test]
    fn test_parser_parse_file() {
        use std::io::Write;
        let dir = tempfile::tempdir().expect("create temp dir");
        let file_path = dir.path().join("test.json");
        let mut file = std::fs::File::create(&file_path).expect("create file");
        write!(file, r#"{{"messages": [{{"id": 1, "type": "message", "date_unixtime": "1234567890", "from": "Bob", "text": "Hi"}}]}}"#).expect("write");

        let parser = create_parser(Platform::Telegram);
        let messages = parser
            .parse_file(file_path.to_str().unwrap())
            .expect("parse failed");
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].sender, "Bob");
    }

    #[cfg(all(feature = "telegram", feature = "streaming"))]
    #[test]
    fn test_parser_stream_file() {
        use std::io::Write;
        let dir = tempfile::tempdir().expect("create temp dir");
        let file_path = dir.path().join("test.json");
        let mut file = std::fs::File::create(&file_path).expect("create file");
        // Streaming parser needs newlines for line-by-line reading
        writeln!(file, r"{{").expect("write");
        writeln!(file, r#"  "messages": ["#).expect("write");
        writeln!(file, r#"    {{"id": 1, "type": "message", "date_unixtime": "1234567890", "from": "Charlie", "text": "Hello"}}"#).expect("write");
        writeln!(file, r"  ]").expect("write");
        writeln!(file, r"}}").expect("write");
        file.flush().expect("flush");
        drop(file);

        let parser = create_streaming_parser(Platform::Telegram);
        let iter = parser
            .stream_file(file_path.to_str().unwrap())
            .expect("stream failed");
        let messages: Vec<_> = iter.filter_map(|r| r.ok()).collect();
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].sender, "Charlie");
    }

    // =========================================================================
    // Parser default implementations
    // =========================================================================

    #[cfg(feature = "telegram")]
    #[test]
    fn test_parser_default_supports_streaming() {
        let parser = create_parser(Platform::Telegram);
        // Default parser (non-streaming config) should return false
        assert!(!parser.supports_streaming());
    }

    #[cfg(feature = "telegram")]
    #[test]
    fn test_parser_default_recommended_buffer_size() {
        let parser = create_parser(Platform::Telegram);
        // Should return at least 64KB
        assert!(parser.recommended_buffer_size() >= 64 * 1024);
    }

    // =========================================================================
    // ParseIterator tests
    // =========================================================================

    #[cfg(all(feature = "telegram", feature = "streaming"))]
    #[test]
    fn test_parse_iterator_wrapper() {
        use crate::streaming::StreamingParser;
        use crate::streaming::TelegramStreamingParser;
        use std::io::Write;

        let dir = tempfile::tempdir().expect("create temp dir");
        let file_path = dir.path().join("test.json");
        let mut file = std::fs::File::create(&file_path).expect("create file");
        writeln!(file, r"{{").expect("write");
        writeln!(file, r#"  "messages": ["#).expect("write");
        writeln!(file, r#"    {{"id": 1, "type": "message", "date_unixtime": "1234567890", "from": "Alice", "text": "Hello"}}"#).expect("write");
        writeln!(file, r"  ]").expect("write");
        writeln!(file, r"}}").expect("write");
        file.flush().expect("flush");
        drop(file);

        let streaming_parser = TelegramStreamingParser::new();
        let inner = streaming_parser
            .stream(file_path.to_str().unwrap())
            .expect("stream failed");

        let mut parse_iter = ParseIterator::new(inner);

        // Test progress methods
        assert!(parse_iter.progress().is_some() || parse_iter.progress().is_none());
        assert!(parse_iter.total_bytes().is_some());

        // Test iterator
        let msg = parse_iter.next().unwrap().expect("should parse");
        assert_eq!(msg.sender, "Alice");
        assert!(parse_iter.next().is_none());
    }

    #[cfg(feature = "telegram")]
    #[test]
    fn test_parser_stream_default_impl() {
        use std::io::Write;

        let dir = tempfile::tempdir().expect("create temp dir");
        let file_path = dir.path().join("test.json");
        let mut file = std::fs::File::create(&file_path).expect("create file");
        write!(file, r#"{{"messages": [{{"id": 1, "type": "message", "date_unixtime": "1234567890", "from": "Bob", "text": "Hi"}}]}}"#).expect("write");
        file.flush().expect("flush");
        drop(file);

        // Use non-streaming parser to test default stream() implementation
        let parser = create_parser(Platform::Telegram);
        let iter = parser.stream(file_path.as_ref()).expect("stream failed");
        let messages: Vec<_> = iter.filter_map(|r| r.ok()).collect();
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].sender, "Bob");
    }
}