1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![cfg(not(feature = "web"))]

use crate::fetch::{FetchCancelReason, FetchEngine, FetchProcess, FetchStatus};
use std::{
    env::var,
    path::{Path, PathBuf},
};

#[derive(Clone)]
pub struct FsFetchEngine {
    root_path: PathBuf,
}

impl Default for FsFetchEngine {
    fn default() -> Self {
        Self {
            root_path: match var("OXY_FETCH_ENGINE_PATH") {
                Ok(value) => value.into(),
                Err(_) => Default::default(),
            },
        }
    }
}

impl FsFetchEngine {
    pub fn new<S: AsRef<Path>>(root_path: &S) -> Self {
        Self {
            root_path: match var("OXY_FETCH_ENGINE_PATH") {
                Ok(value) => value.into(),
                Err(_) => root_path.as_ref().into(),
            },
        }
    }
}

impl FetchEngine for FsFetchEngine {
    fn fetch(&mut self, path: &str) -> Result<Box<FetchProcess>, FetchStatus> {
        #[cfg(feature = "parallel")]
        {
            let path = self.root_path.join(path);
            let process = FetchProcess::new_start();
            let mut p = process.clone();
            rayon::spawn(move || {
                if let Ok(bytes) = std::fs::read(path) {
                    p.done(bytes);
                } else {
                    p.cancel(FetchCancelReason::Error);
                }
            });
            Ok(Box::new(process))
        }
        #[cfg(not(feature = "parallel"))]
        {
            if let Ok(bytes) = std::fs::read(self.root_path.join(path)) {
                Ok(Box::new(FetchProcess::new_done(bytes)))
            } else {
                Err(FetchStatus::Canceled(FetchCancelReason::Error))
            }
        }
    }
}