aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-03-03 05:38:58 +0300
committerA Farzat <a@farzat.xyz>2026-03-03 05:38:58 +0300
commit3558106317184cb41367007150f252fe1a9e9eeb (patch)
treeecccef3476d7164ee3b8cf13d804ab7277b893b5 /src
parent7ff459dc3bc5b9d0ed8c067cce443b2ee55186c8 (diff)
downloadoreilly-epub-3558106317184cb41367007150f252fe1a9e9eeb.tar.gz
oreilly-epub-3558106317184cb41367007150f252fe1a9e9eeb.zip
Convert chapters and file_entries to HashMaps
This makes them more useful as each will be referenced by either spine_items or toc_vec later on. This is demonstrated by the temporary sanity checks, which shall be replaced by code that actually use fields from the corresponding items.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index 8bc25ea..34cc200 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,9 @@
mod http_client;
mod models;
-use anyhow::{Context, Result};
+use std::collections::HashMap;
+
+use anyhow::{Context, Result, ensure};
use clap::Parser;
use http_client::build_authenticated_client;
use models::{Chapter, EpubResponse, FileEntry, Paginated, SearchResult, SpineItem, TocNode};
@@ -121,5 +123,24 @@ async fn main() -> Result<()> {
let spine_items: Vec<SpineItem> = fetch_all_pages(&client, epub_data.spine.clone()).await?;
let toc_vec: Vec<TocNode> = fetch_direct_array(&client, &epub_data.table_of_contents).await?;
+ // Sanity check: Every entry in spine exists in chapters.
+ let chapters: HashMap<String, Chapter> =
+ chapters.into_iter().map(|c| (c.ourn.clone(), c)).collect();
+ for s in spine_items {
+ ensure!(chapters.contains_key(&s.ourn), "{} not in chapters", s.ourn);
+ }
+ // Sanity check: Every node in the ToC references a file entry.
+ let file_entries: HashMap<String, FileEntry> = file_entries
+ .into_iter()
+ .map(|f| (f.ourn.clone(), f))
+ .collect();
+ for i in toc_vec {
+ ensure!(
+ file_entries.contains_key(&i.ourn),
+ "{} not in files",
+ i.ourn
+ );
+ }
+
Ok(())
}