chunk_reader/
file.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/piot/swamp-render
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use crate::{ChunkReader, ChunkReaderError, ResourceId};
7use async_trait::async_trait;
8use std::fs;
9use std::path::PathBuf;
10
11pub struct FileChunkReader {
12    prefix_path: PathBuf,
13}
14
15impl FileChunkReader {
16    #[must_use]
17    pub fn new(prefix: impl Into<PathBuf>) -> Self {
18        Self {
19            prefix_path: prefix.into(),
20        }
21    }
22}
23
24#[async_trait(?Send)]
25impl ChunkReader for FileChunkReader {
26    async fn fetch_octets(&self, id: ResourceId) -> Result<Vec<u8>, ChunkReaderError> {
27        let full_path = self.prefix_path.join(id.as_str());
28        fs::read(&full_path).map_err(|e| ChunkReaderError::IoError {
29            resource: id,
30            source: e,
31        })
32    }
33}