Skip to content

Commit

Permalink
Add a FileDataSource that fetches archive files from local disk.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter committed Oct 19, 2023
1 parent 6417711 commit 65c9eed
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/file_data.rs
Original file line number Diff line number Diff line change
@@ -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<Path>) -> Self {
Self {
basedir: basedir.as_ref().to_owned(),
}
}

fn read_file<T>(&self, path: impl AsRef<Path>) -> 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

Check failure on line 29 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Clippy

returning the result of a `let` binding from a block

Check failure on line 29 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Clippy

returning the result of a `let` binding from a block
}
}

impl DataSource for FileDataSource {
fn fetch_info(&self) -> DataSourceInfo {
let mut path = self.basedir.clone();
path.push("info");
self.read_file::<DataSourceInfo>(&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::<SummaryTile>(&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::<SlotTile>(&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::<SlotMetaTile>(&path)
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 65c9eed

Please sign in to comment.