kobo_db_tools/export/
mod.rs

1use crate::model::Bookmark;
2use chrono::{DateTime, Utc};
3use std::error::Error;
4use std::fs::File;
5use std::io::Write;
6use std::str::FromStr;
7
8pub enum ExportFormat {
9    Csv,
10    Markdown,
11}
12
13pub fn export_bookmarks(
14    bookmarks: &[Bookmark],
15    format: ExportFormat,
16    path: &str,
17) -> Result<(), Box<dyn Error>> {
18    match format {
19        ExportFormat::Csv => {
20            let file = File::create(path)?;
21            let mut wtr = csv::Writer::from_writer(file);
22            for bookmark in bookmarks {
23                wtr.serialize(bookmark)?;
24            }
25            wtr.flush()?;
26        }
27        ExportFormat::Markdown => {
28            let mut file = File::create(path)?;
29            for bookmark in bookmarks {
30                writeln!(file, "### {}", bookmark.book_title)?;
31                writeln!(file, "\n> {}", bookmark.content)?;
32                writeln!(
33                    file,
34                    "\n**Chapter Progress:** {:.2}%",
35                    bookmark.chapter_progress * 100.0
36                )?;
37                let formatted_date = DateTime::<Utc>::from_str(&bookmark.create_date)
38                    .map(|dt| dt.format("%Y-%m-%d %H:%M:%S UTC").to_string())
39                    .unwrap_or_else(|_| bookmark.create_date.clone());
40                writeln!(file, "**Created:** {}", formatted_date)?;
41                writeln!(file, "\n---\n")?;
42            }
43        }
44    }
45    Ok(())
46}