From 3558106317184cb41367007150f252fe1a9e9eeb Mon Sep 17 00:00:00 2001 From: A Farzat Date: Tue, 3 Mar 2026 05:38:58 +0300 Subject: 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. --- src/main.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src') 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 = fetch_all_pages(&client, epub_data.spine.clone()).await?; let toc_vec: Vec = fetch_direct_array(&client, &epub_data.table_of_contents).await?; + // Sanity check: Every entry in spine exists in chapters. + let chapters: HashMap = + 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 = 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(()) } -- cgit v1.2.3-70-g09d2