-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a FileDataSource that fetches archive files from local disk (#34)
- Loading branch information
1 parent
6417711
commit 9d587fb
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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"); | ||
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::<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.join("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.join("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.join("slot_meta_tile"); | ||
path.push(&req.to_slug()); | ||
self.read_file::<SlotMetaTile>(&path) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters