Skip to main content

basic_usage/
basic_usage.rs

1use epubie_lib::Epub;
2
3fn main() {
4    // Example of how to use the epubie-lib library
5    let epub_path = "./example-files/iia.epub";
6
7    println!("Attempting to parse EPUB: {}", epub_path);
8
9    match Epub::new(epub_path.to_string()) {
10        Ok(epub) => {
11            // Display basic metadata
12            println!("\n=== EPUB Metadata ===");
13            println!("Title: {}", epub.get_title());
14            println!("Creator: {}", epub.get_creator());
15            println!("Language: {}", epub.get_language());
16            println!("Identifier: {}", epub.get_identifier());
17            println!("Date: {}", epub.get_date());
18
19            if let Some(publisher) = epub.get_publisher() {
20                println!("Publisher: {}", publisher);
21            }
22
23            if let Some(description) = epub.get_description() {
24                println!("Description: {}", description);
25            }
26
27            // Display chapter information
28            println!("\n=== Chapters ({}) ===", epub.get_chapter_count());
29            for (i, chapter) in epub.get_chapters().iter().enumerate() {
30                println!(
31                    "Chapter {}: {} ({} file{})",
32                    i + 1,
33                    chapter.get_title(),
34                    chapter.get_file_count(),
35                    if chapter.get_file_count() == 1 {
36                        ""
37                    } else {
38                        "s"
39                    }
40                );
41
42                // Display files in each chapter
43                for (j, file) in chapter.get_files().iter().enumerate() {
44                    println!(
45                        "  File {}: {} ({})",
46                        j + 1,
47                        file.get_title().unwrap_or("No title"),
48                        file.get_href()
49                    );
50
51                    // Show if it's an HTML file
52                    if file.is_html() {
53                        println!("    [HTML file - {} bytes]", file.get_html_bytes().len());
54                    }
55                }
56            }
57
58            // Display table of contents
59            let toc = epub.get_table_of_contents();
60            println!(
61                "\n=== Table of Contents ({} entries) ===",
62                toc.get_entry_count()
63            );
64            for (i, entry) in toc.get_entries().iter().enumerate() {
65                let indent = "  ".repeat(entry.get_level() as usize);
66                println!(
67                    "{}{}: {} ({})",
68                    indent,
69                    i + 1,
70                    entry.get_title(),
71                    entry.get_href()
72                );
73            }
74
75            // Display total file count
76            println!("\n=== File Summary ===");
77            println!("Total files: {}", epub.get_file_count());
78            println!("All files:");
79            for (i, file) in epub.get_all_files().iter().enumerate() {
80                println!(
81                    "  {}: {} ({}) - {}",
82                    i + 1,
83                    file.get_title().unwrap_or("No title"),
84                    file.get_href(),
85                    file.get_media_type()
86                );
87            }
88        }
89        Err(e) => {
90            eprintln!("Error parsing EPUB: {}", e);
91            eprintln!("Make sure the file exists and is a valid EPUB file.");
92        }
93    }
94}