typst_as_lib/
cached_file_resolver.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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::{
    borrow::Cow,
    collections::HashMap,
    sync::{Arc, Mutex},
};

use typst::{
    diag::FileResult,
    foundations::Bytes,
    syntax::{FileId, Source},
};

use crate::file_resolver::FileResolver;

pub struct CachedFileResolver<T> {
    pub file_resolver: T,
    pub in_memory_source_cache: Option<Arc<Mutex<HashMap<FileId, Source>>>>,
    pub in_memory_binary_cache: Option<Arc<Mutex<HashMap<FileId, Bytes>>>>,
}

impl<T> CachedFileResolver<T> {
    pub fn new(file_resolver: T) -> Self {
        CachedFileResolver {
            file_resolver,
            in_memory_source_cache: None,
            in_memory_binary_cache: None,
        }
    }

    pub fn with_in_memory_source_cache(self) -> Self {
        Self {
            in_memory_source_cache: Some(Default::default()),
            ..self
        }
    }

    pub fn with_in_memory_binary_cache(self) -> Self {
        Self {
            in_memory_binary_cache: Some(Default::default()),
            ..self
        }
    }
}

impl<T> FileResolver for CachedFileResolver<T>
where
    T: FileResolver,
{
    fn resolve_binary(&self, id: FileId) -> FileResult<Cow<Bytes>> {
        let Self {
            in_memory_binary_cache,
            ..
        } = self;

        if let Some(in_memory_binary_cache) = in_memory_binary_cache {
            if let Ok(in_memory_binary_cache) = in_memory_binary_cache.lock() {
                if let Some(cached) = in_memory_binary_cache.get(&id) {
                    return Ok(Cow::Owned(cached.clone()));
                }
            }
        }
        let resolved = self.file_resolver.resolve_binary(id)?;
        if let Some(in_memory_binary_cache) = in_memory_binary_cache {
            if let Ok(mut in_memory_binary_cache) = in_memory_binary_cache.lock() {
                in_memory_binary_cache.insert(id, resolved.as_ref().clone());
            }
        }
        Ok(resolved)
    }

    fn resolve_source(&self, id: FileId) -> FileResult<Cow<Source>> {
        let Self {
            in_memory_source_cache,
            ..
        } = self;

        if let Some(in_memory_source_cache) = in_memory_source_cache {
            if let Ok(in_memory_source_cache) = in_memory_source_cache.lock() {
                if let Some(cached) = in_memory_source_cache.get(&id) {
                    return Ok(Cow::Owned(cached.clone()));
                }
            }
        }
        let resolved = self.file_resolver.resolve_source(id)?;
        if let Some(in_memory_source_cache) = in_memory_source_cache {
            if let Ok(mut in_memory_source_cache) = in_memory_source_cache.lock() {
                in_memory_source_cache.insert(id, resolved.as_ref().clone());
            }
        }
        Ok(resolved)
    }
}