tinymist_vfs/
resolve.rs

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
use std::{fmt::Debug, sync::Arc};

use tinymist_std::ImmutPath;
use typst::diag::FileResult;

use crate::{path_mapper::RootResolver, AccessModel, Bytes, FileId, PathAccessModel};

/// Provides resolve access model.
#[derive(Clone)]
pub struct ResolveAccessModel<M> {
    pub resolver: Arc<dyn RootResolver + Send + Sync>,
    pub inner: M,
}

impl<M> Debug for ResolveAccessModel<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResolveAccessModel").finish()
    }
}

impl<M: PathAccessModel> AccessModel for ResolveAccessModel<M> {
    #[inline]
    fn reset(&mut self) {
        self.inner.reset();
    }

    fn content(&self, fid: FileId) -> (Option<ImmutPath>, FileResult<Bytes>) {
        let resolved = Ok(()).and_then(|_| self.resolver.path_for_id(fid)?.to_err());

        match resolved {
            Ok(path) => (Some(path.as_path().into()), self.inner.content(&path)),
            Err(e) => (None, Err(e)),
        }
    }
}