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
//! Parse and convert chat exports from messaging platforms into LLM-friendly formats.
//!
//! # Overview
//!
//! Chatpack provides a unified API for parsing chat exports from popular messaging
//! platforms and converting them into formats optimized for Large Language Models.
//! It handles platform-specific quirks (encoding issues, date formats, message types)
//! and provides tools for filtering, merging, and exporting messages.
//!
//! **Supported platforms:**
//!
//! | Platform | Export Format | Special Handling |
//! |----------|---------------|------------------|
//! | Telegram | JSON | Service messages, forwarded messages |
//! | WhatsApp | TXT | Auto-detects 4 locale-specific date formats |
//! | Instagram | JSON | Fixes Mojibake encoding from Meta exports |
//! | Discord | JSON/TXT/CSV | Attachments, stickers, replies |
//!
//! # Quick Start
//!
//! ```no_run
//! use chatpack::prelude::*;
//!
//! # #[cfg(all(feature = "telegram", feature = "csv-output"))]
//! # fn main() -> chatpack::Result<()> {
//! // Parse Telegram export
//! let parser = create_parser(Platform::Telegram);
//! let messages = parser.parse("export.json".as_ref())?;
//!
//! // Filter, merge, and export
//! let filtered = apply_filters(messages, &FilterConfig::new().with_sender("Alice"));
//! let merged = merge_consecutive(filtered);
//! write_csv(&merged, "output.csv", &OutputConfig::default())?;
//! # Ok(())
//! # }
//! # #[cfg(not(all(feature = "telegram", feature = "csv-output")))]
//! # fn main() {}
//! ```
//!
//! # Core Concepts
//!
//! ## Message
//!
//! [`Message`] is the universal representation of a chat message across all platforms:
//!
//! ```
//! use chatpack::Message;
//!
//! let msg = Message::new("Alice", "Hello, world!");
//! assert_eq!(msg.sender, "Alice");
//! assert_eq!(msg.content, "Hello, world!");
//! ```
//!
//! ## Parser Trait
//!
//! All platform parsers implement the [`Parser`](parser::Parser) trait, providing
//! a consistent interface:
//!
//! ```no_run
//! # #[cfg(feature = "whatsapp")]
//! # fn main() -> chatpack::Result<()> {
//! use chatpack::parser::Parser;
//! use chatpack::parsers::WhatsAppParser;
//!
//! let parser = WhatsAppParser::new();
//!
//! // Parse from file
//! let messages = parser.parse("chat.txt".as_ref())?;
//!
//! // Or parse from string
//! let content = "[1/15/24, 10:30:45 AM] Alice: Hello";
//! let messages = parser.parse_str(content)?;
//! # Ok(())
//! # }
//! # #[cfg(not(feature = "whatsapp"))]
//! # fn main() {}
//! ```
//!
//! # Common Patterns
//!
//! ## Filter by Date Range
//!
//! ```
//! use chatpack::prelude::*;
//!
//! # fn main() -> chatpack::Result<()> {
//! let messages = vec![
//! Message::new("Alice", "Old message"),
//! Message::new("Bob", "Recent message"),
//! ];
//!
//! let filter = FilterConfig::new()
//! .with_date_from("2024-01-01")?
//! .with_date_to("2024-12-31")?;
//!
//! let filtered = apply_filters(messages, &filter);
//! # Ok(())
//! # }
//! ```
//!
//! ## Merge Consecutive Messages
//!
//! Combine messages from the same sender within a time window:
//!
//! ```
//! use chatpack::prelude::*;
//!
//! let messages = vec![
//! Message::new("Alice", "Hello"),
//! Message::new("Alice", "How are you?"),
//! Message::new("Bob", "I'm fine!"),
//! ];
//!
//! let merged = merge_consecutive(messages);
//! assert_eq!(merged.len(), 2); // Alice's messages merged
//! assert!(merged[0].content.contains("Hello"));
//! assert!(merged[0].content.contains("How are you?"));
//! ```
//!
//! ## Stream Large Files
//!
//! Process files larger than available memory:
//!
//! ```no_run
//! # #[cfg(all(feature = "telegram", feature = "streaming"))]
//! # fn main() -> chatpack::Result<()> {
//! use chatpack::prelude::*;
//!
//! let parser = create_streaming_parser(Platform::Telegram);
//!
//! for result in parser.stream("huge_export.json".as_ref())? {
//! let msg = result?;
//! println!("{}: {}", msg.sender, msg.content);
//! }
//! # Ok(())
//! # }
//! # #[cfg(not(all(feature = "telegram", feature = "streaming")))]
//! # fn main() {}
//! ```
//!
//! ## Export to Multiple Formats
//!
//! ```no_run
//! # #[cfg(all(feature = "csv-output", feature = "json-output"))]
//! # fn main() -> chatpack::Result<()> {
//! use chatpack::prelude::*;
//!
//! let messages = vec![Message::new("Alice", "Hello!")];
//! let config = OutputConfig::new().with_timestamps();
//!
//! // CSV - best for LLM context (13x token compression)
//! write_csv(&messages, "output.csv", &config)?;
//!
//! // JSON - structured array for APIs
//! write_json(&messages, "output.json", &config)?;
//!
//! // JSONL - one object per line for RAG pipelines
//! write_jsonl(&messages, "output.jsonl", &config)?;
//! # Ok(())
//! # }
//! # #[cfg(not(all(feature = "csv-output", feature = "json-output")))]
//! # fn main() {}
//! ```
//!
//! # Module Structure
//!
//! | Module | Description |
//! |--------|-------------|
//! | [`parser`] | Unified parser API with [`Parser`](parser::Parser) trait and [`Platform`](parser::Platform) enum |
//! | [`parsers`] | Platform-specific implementations: [`TelegramParser`](parsers::TelegramParser), [`WhatsAppParser`](parsers::WhatsAppParser), etc. |
//! | [`config`] | Parser configurations: [`TelegramConfig`](config::TelegramConfig), [`WhatsAppConfig`](config::WhatsAppConfig), etc. |
//! | [`core`] | Core types: [`Message`], [`OutputConfig`](core::OutputConfig), [`FilterConfig`](core::FilterConfig) |
//! | [`streaming`] | Memory-efficient streaming parsers for large files |
//! | [`mod@format`] | Output formats: [`OutputFormat`](format::OutputFormat), [`write_to_format`](format::write_to_format) |
//! | [`error`] | Error types: [`ChatpackError`], [`Result`] |
//! | [`prelude`] | Convenient re-exports for common usage |
//!
//! # Feature Flags
//!
//! Enable only the features you need to minimize compile time and dependencies:
//!
//! | Feature | Description | Dependencies |
//! |---------|-------------|--------------|
//! | `telegram` | Telegram JSON parser | `serde_json` |
//! | `whatsapp` | WhatsApp TXT parser | `regex` |
//! | `instagram` | Instagram JSON parser | `serde_json` |
//! | `discord` | Discord multi-format parser | `serde_json`, `regex`, `csv` |
//! | `csv-output` | CSV output writer | `csv` |
//! | `json-output` | JSON/JSONL output writers | `serde_json` |
//! | `streaming` | Streaming parsers for large files | - |
//! | `async` | Async parser support | `tokio` |
//! | `full` | All features (default) | all above |
//!
//! ```toml
//! # Cargo.toml - minimal configuration
//! [dependencies]
//! chatpack = { version = "0.5", default-features = false, features = ["telegram", "csv-output"] }
//! ```
//!
//! # Serialization
//!
//! All public types implement [`serde::Serialize`] and [`serde::Deserialize`]:
//!
//! ```
//! use chatpack::Message;
//!
//! let msg = Message::new("Alice", "Hello!");
//! let json = serde_json::to_string(&msg).unwrap();
//! let parsed: Message = serde_json::from_str(&json).unwrap();
//! assert_eq!(msg.content, parsed.content);
//! ```
// Core modules (always available)
// Shared parsing utilities (DRY - used by both parsers and streaming)
// Parser modules - require at least one parser feature
// Streaming module (requires streaming feature and at least one parser)
// Async parser module (requires async feature and at least one parser)
// Re-export the main types at the crate root for convenience
pub use ;
pub use Message;
/// Convenient re-exports for common usage patterns.
///
/// This module provides a single import for the most commonly used types
/// and functions. It's designed to cover 90% of use cases with minimal imports.
///
/// # Example
///
/// ```
/// use chatpack::prelude::*;
///
/// // Now you have access to:
/// // - Message, ChatpackError, Result
/// // - Platform, Parser, create_parser, create_streaming_parser
/// // - FilterConfig, apply_filters
/// // - OutputConfig, merge_consecutive
/// // - write_csv, write_json, write_jsonl (with features)
/// // - All platform parsers (with features)
///
/// let msg = Message::new("Alice", "Hello!");
/// ```