From 9d587fb642516fc894ea24fefc788463326b0b28 Mon Sep 17 00:00:00 2001 From: Elliott Slaughter Date: Thu, 19 Oct 2023 14:47:40 -0700 Subject: [PATCH] Add a FileDataSource that fetches archive files from local disk (#34) --- src/file_data.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 2 files changed, 65 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..af680b2 --- /dev/null +++ b/src/file_data.rs @@ -0,0 +1,63 @@ +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"); + ciborium::from_reader(f).expect("cbor decoding failed") + } +} + +impl DataSource for FileDataSource { + fn fetch_info(&self) -> DataSourceInfo { + 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.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.join("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.join("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;