1
2pub mod datetime;
3pub mod parser;
4pub mod models;
5
6use crate::parser::{parse_messages};
7use crate::models::{Message, ParseStringOptions};
8
9use std::fs::File;
10use std::io::Result as IoResult;
11use std::path::Path;
12use memmap2::Mmap;
13
14pub fn parse_string(s: &str, options: Option<ParseStringOptions>) -> Result<Vec<Message>, String> {
15 let lines: Vec<&str> = s.split('\n').collect();
16 let opts = options.unwrap_or_default();
17 let debug = opts.debug;
18
19 if debug {
20 println!("🔍 DEBUG: parse_string called with {} characters", s.len());
21 println!("🔍 DEBUG: Split into {} lines", lines.len());
22 println!("🔍 DEBUG: Options: {:?}", opts);
23 println!("🔍 DEBUG: =====================================");
24 }
25
26 Ok(parse_messages(&parser::make_array_of_messages_with_debug(&lines, debug), &opts))
27}
28
29pub fn parse_file<P: AsRef<Path>>(path: P, options: Option<ParseStringOptions>) -> IoResult<Vec<Message>> {
35 let file = File::open(path)?;
36 let mmap = unsafe { Mmap::map(&file)? };
37 let text: &str = std::str::from_utf8(&mmap).expect("Chat file is not valid UTF-8");
38 parse_string(text, options).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
39}