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
use crate::context::SharedContext;
use crate::{dump_json, FileListerError};
use std::path::PathBuf;
use std::sync::PoisonError;
use warp::reject::Reject;
use warp::{Filter, Rejection, Reply};

impl Reject for Error {}

pub fn filter(
    context: SharedContext,
    path: PathBuf,
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
    let with_context = warp::any().map(move || context.clone());

    let root = warp::path!("files" / ..).boxed();

    let get = warp::path("get").and(warp::fs::dir(path));
    let list = warp::path!("list")
        .and(warp::get())
        .and(with_context)
        .and_then(files_handler);
    root.and(get.or(list)).boxed()
}

pub async fn files_handler(context: SharedContext) -> Result<impl Reply, Rejection> {
    Ok(dump_json(context.read().await.working_directory()).map(|r| warp::reply::json(&r))?)
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    File(#[from] FileListerError),
    #[error("cannot acquire lock on context")]
    Poison,
}

impl<T> From<PoisonError<T>> for Error {
    fn from(_err: PoisonError<T>) -> Self {
        Self::Poison
    }
}