pub trait Parser: Send + Sync {
// Required methods
fn name(&self) -> &'static str;
fn platform(&self) -> Platform;
fn parse(&self, path: &Path) -> Result<Vec<Message>, ChatpackError>;
fn parse_str(&self, content: &str) -> Result<Vec<Message>, ChatpackError>;
// Provided methods
fn parse_file(&self, path: &str) -> Result<Vec<Message>, ChatpackError> { ... }
fn stream(
&self,
path: &Path,
) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError> { ... }
fn stream_file(
&self,
path: &str,
) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError> { ... }
fn supports_streaming(&self) -> bool { ... }
fn recommended_buffer_size(&self) -> usize { ... }
}Expand description
Unified trait for parsing chat exports.
This trait combines the functionality of the previous ChatParser and
StreamingParser traits into a single, cohesive API.
§Implementation Notes
Parsers must implement:
name- Parser identifierplatform- Platform this parser handlesparse- Load entire file into memoryparse_str- Parse from a string
Optionally override:
stream- Streaming for large files (default: falls back to parse)supports_streaming- Whether native streaming is supported
§Example Implementation
impl Parser for MyParser {
fn name(&self) -> &'static str { "MyParser" }
fn platform(&self) -> Platform { Platform::Telegram }
fn parse(&self, path: &Path) -> Result<Vec<Message>, ChatpackError> {
let content = std::fs::read_to_string(path)?;
self.parse_str(&content)
}
fn parse_str(&self, content: &str) -> Result<Vec<Message>, ChatpackError> {
// Parse logic here
Ok(vec![])
}
}Required Methods§
Sourcefn name(&self) -> &'static str
fn name(&self) -> &'static str
Returns the human-readable name of this parser.
§Example
use chatpack::parser::Parser;
use chatpack::parsers::TelegramParser;
let parser = TelegramParser::new();
assert_eq!(parser.name(), "Telegram");Sourcefn parse(&self, path: &Path) -> Result<Vec<Message>, ChatpackError>
fn parse(&self, path: &Path) -> Result<Vec<Message>, ChatpackError>
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.
§Arguments
path- Path to the export file
§Errors
Returns ChatpackError if:
- File cannot be read (
ChatpackError::Io) - Content cannot be parsed (
ChatpackError::Parse)
Sourcefn parse_str(&self, content: &str) -> Result<Vec<Message>, ChatpackError>
fn parse_str(&self, content: &str) -> 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.
Provided Methods§
Sourcefn parse_file(&self, path: &str) -> Result<Vec<Message>, ChatpackError>
fn parse_file(&self, path: &str) -> Result<Vec<Message>, ChatpackError>
Parses a chat export file (convenience method accepting &str path).
This is equivalent to parse(Path::new(path)).
Sourcefn stream(
&self,
path: &Path,
) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError>
fn stream( &self, path: &Path, ) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError>
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
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);
}
}Sourcefn stream_file(
&self,
path: &str,
) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError>
fn stream_file( &self, path: &str, ) -> Result<Box<dyn Iterator<Item = Result<Message, ChatpackError>> + Send>, ChatpackError>
Streams messages (convenience method accepting &str path).
Sourcefn supports_streaming(&self) -> bool
fn supports_streaming(&self) -> bool
Returns whether this parser supports native streaming.
If false, the stream method will load the entire
file into memory before iterating.
Sourcefn recommended_buffer_size(&self) -> usize
fn recommended_buffer_size(&self) -> usize
Returns the recommended buffer size for streaming.
Only relevant if supports_streaming returns true.