chunk_reader/
debug.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::ResourceId;
7use crate::{ChunkReader, ChunkReaderError};
8use async_trait::async_trait;
9use std::path::PathBuf;
10use std::sync::Arc;
11use std::time::Duration;
12use std::{fs, thread};
13use tracing::{debug, warn};
14
15#[derive(Debug)]
16pub struct FileChunkReaderDebug {
17    prefix: PathBuf,
18    /// Duration to sleep before fetching the asset.
19    delay: Duration,
20}
21
22impl FileChunkReaderDebug {
23    /// Creates a new `FileAssetReaderDebug` with the specified delay.
24    ///
25    /// # Arguments
26    ///
27    /// * `delay` - The duration to sleep before fetching each asset.
28    #[must_use]
29    pub fn new(prefix: String, delay: Duration) -> Self {
30        Self {
31            prefix: prefix.into(),
32            delay,
33        }
34    }
35}
36
37#[async_trait(?Send)]
38impl ChunkReader for FileChunkReaderDebug {
39    async fn fetch_octets(&self, id: ResourceId) -> Result<Vec<u8>, ChunkReaderError> {
40        debug!("Starting fetch_octets for path: {}", id.as_str());
41        let complete_path = self.prefix.join(id.as_str());
42        if !complete_path.exists() {
43            warn!("Path does not exist: {:?}", complete_path);
44            return Err(ChunkReaderError::ResourceNotFound(id));
45        }
46
47        thread::sleep(self.delay);
48
49        let path_arc = Arc::new(complete_path);
50        let path_for_task = Arc::clone(&path_arc);
51
52        fs::read(&*path_for_task).map_err(|e| ChunkReaderError::IoError {
53            resource: id,
54            source: e,
55        })
56    }
57}