From 65c9eed1c61bf121ff2f034aad3dc48a67a8454c Mon Sep 17 00:00:00 2001 From: Elliott Slaughter Date: Thu, 19 Oct 2023 12:17:09 -0700 Subject: [PATCH 1/2] Add a FileDataSource that fetches archive files from local disk. --- src/file_data.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 2 files changed, 70 insertions(+) create mode 100644 src/file_data.rs diff --git a/src/file_data.rs b/src/file_data.rs new file mode 100644 index 0000000..376c38b --- /dev/null +++ b/src/file_data.rs @@ -0,0 +1,68 @@ +use std::fs::File; +use std::path::{Path, PathBuf}; + +use serde::Deserialize; + +use crate::data::{ + DataSource, DataSourceInfo, EntryID, SlotMetaTile, SlotTile, SummaryTile, TileID, +}; +use crate::http::schema::TileRequestRef; + +pub struct FileDataSource { + pub basedir: PathBuf, +} + +impl FileDataSource { + pub fn new(basedir: impl AsRef) -> Self { + Self { + basedir: basedir.as_ref().to_owned(), + } + } + + fn read_file(&self, path: impl AsRef) -> T + where + T: for<'a> Deserialize<'a>, + { + let f = File::open(path).expect("opening file failed"); + let f = zstd::Decoder::new(f).expect("zstd decompression failed"); + let result = ciborium::from_reader(f).expect("cbor decoding failed"); + result + } +} + +impl DataSource for FileDataSource { + fn fetch_info(&self) -> DataSourceInfo { + let mut path = self.basedir.clone(); + path.push("info"); + self.read_file::(&path) + } + + fn fetch_summary_tile(&self, entry_id: &EntryID, tile_id: TileID, _full: bool) -> SummaryTile { + let req = TileRequestRef { entry_id, tile_id }; + let mut path = self.basedir.clone(); + path.push("summary_tile"); + path.push(&req.to_slug()); + self.read_file::(&path) + } + + fn fetch_slot_tile(&self, entry_id: &EntryID, tile_id: TileID, _full: bool) -> SlotTile { + let req = TileRequestRef { entry_id, tile_id }; + let mut path = self.basedir.clone(); + path.push("slot_tile"); + path.push(&req.to_slug()); + self.read_file::(&path) + } + + fn fetch_slot_meta_tile( + &self, + entry_id: &EntryID, + tile_id: TileID, + _full: bool, + ) -> SlotMetaTile { + let req = TileRequestRef { entry_id, tile_id }; + let mut path = self.basedir.clone(); + path.push("slot_meta_tile"); + path.push(&req.to_slug()); + self.read_file::(&path) + } +} diff --git a/src/lib.rs b/src/lib.rs index f9fbec0..fb87ed2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,8 @@ pub mod app; pub mod archive_data; pub mod data; pub mod deferred_data; +#[cfg(not(target_arch = "wasm32"))] +pub mod file_data; pub mod http; #[cfg(not(target_arch = "wasm32"))] pub mod parallel_data; From 3676bc9e9ee596cdaf2dca2fe5a5388d1ce11dd9 Mon Sep 17 00:00:00 2001 From: Elliott Slaughter Date: Thu, 19 Oct 2023 13:54:51 -0700 Subject: [PATCH 2/2] Clean up. --- src/file_data.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/file_data.rs b/src/file_data.rs index 376c38b..af680b2 100644 --- a/src/file_data.rs +++ b/src/file_data.rs @@ -25,30 +25,26 @@ impl FileDataSource { { let f = File::open(path).expect("opening file failed"); let f = zstd::Decoder::new(f).expect("zstd decompression failed"); - let result = ciborium::from_reader(f).expect("cbor decoding failed"); - result + ciborium::from_reader(f).expect("cbor decoding failed") } } impl DataSource for FileDataSource { fn fetch_info(&self) -> DataSourceInfo { - let mut path = self.basedir.clone(); - path.push("info"); + let path = self.basedir.join("info"); self.read_file::(&path) } fn fetch_summary_tile(&self, entry_id: &EntryID, tile_id: TileID, _full: bool) -> SummaryTile { let req = TileRequestRef { entry_id, tile_id }; - let mut path = self.basedir.clone(); - path.push("summary_tile"); + let mut path = self.basedir.join("summary_tile"); path.push(&req.to_slug()); self.read_file::(&path) } fn fetch_slot_tile(&self, entry_id: &EntryID, tile_id: TileID, _full: bool) -> SlotTile { let req = TileRequestRef { entry_id, tile_id }; - let mut path = self.basedir.clone(); - path.push("slot_tile"); + let mut path = self.basedir.join("slot_tile"); path.push(&req.to_slug()); self.read_file::(&path) } @@ -60,8 +56,7 @@ impl DataSource for FileDataSource { _full: bool, ) -> SlotMetaTile { let req = TileRequestRef { entry_id, tile_id }; - let mut path = self.basedir.clone(); - path.push("slot_meta_tile"); + let mut path = self.basedir.join("slot_meta_tile"); path.push(&req.to_slug()); self.read_file::(&path) }