fetchium_core/export/
mod.rs1pub mod bibtex;
10pub mod pandoc;
11
12pub use bibtex::generate_bibtex;
13pub use pandoc::{check_pandoc, export_docx, export_pdf};
14
15use crate::error::FetchiumError;
16use std::path::Path;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum ExportFormat {
21 Pdf,
22 Docx,
23 Bibtex,
24 Markdown,
25}
26
27impl ExportFormat {
28 pub fn parse(s: &str) -> Option<Self> {
30 match s.to_lowercase().as_str() {
31 "pdf" => Some(Self::Pdf),
32 "docx" | "word" => Some(Self::Docx),
33 "bibtex" | "bib" => Some(Self::Bibtex),
34 "md" | "markdown" => Some(Self::Markdown),
35 _ => None,
36 }
37 }
38}
39
40pub fn export(
42 content: &str,
43 format: ExportFormat,
44 output_path: &Path,
45 title: Option<&str>,
46) -> Result<(), FetchiumError> {
47 match format {
48 ExportFormat::Pdf => export_pdf(content, output_path, title),
49 ExportFormat::Docx => export_docx(content, output_path),
50 ExportFormat::Markdown => {
51 std::fs::write(output_path, content)?;
52 Ok(())
53 }
54 ExportFormat::Bibtex => {
55 Err(FetchiumError::Internal(
57 "BibTeX export requires sources list — use generate_bibtex() directly".into(),
58 ))
59 }
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn format_from_str() {
69 assert_eq!(ExportFormat::parse("pdf"), Some(ExportFormat::Pdf));
70 assert_eq!(ExportFormat::parse("PDF"), Some(ExportFormat::Pdf));
71 assert_eq!(ExportFormat::parse("docx"), Some(ExportFormat::Docx));
72 assert_eq!(ExportFormat::parse("word"), Some(ExportFormat::Docx));
73 assert_eq!(ExportFormat::parse("bibtex"), Some(ExportFormat::Bibtex));
74 assert_eq!(ExportFormat::parse("bib"), Some(ExportFormat::Bibtex));
75 assert_eq!(
76 ExportFormat::parse("markdown"),
77 Some(ExportFormat::Markdown)
78 );
79 assert_eq!(ExportFormat::parse("unknown"), None);
80 }
81
82 #[test]
83 fn export_markdown_writes_file() {
84 let dir = tempfile::tempdir().unwrap();
85 let path = dir.path().join("output.md");
86 export("# Hello\n\nContent.", ExportFormat::Markdown, &path, None).unwrap();
87 let content = std::fs::read_to_string(&path).unwrap();
88 assert!(content.contains("Hello"));
89 }
90}