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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::path::Path;
use std::sync::Arc;
use model::ImageMacro;
use resources::{CachingLoader, Font, FontLoader, Loader, Template, TemplateLoader};
use util::cache::ThreadSafeCache;
use super::error::CaptionError;
use super::output::CaptionOutput;
use super::task::CaptionTask;
const DEFAULT_TEMPLATE_CAPACITY: usize = 128;
const DEFAULT_FONT_CAPACITY: usize = 16;
#[derive(Clone, Debug)]
pub struct Engine<Tl = TemplateLoader, Fl = FontLoader>
where Tl: Loader<Item=Template>, Fl: Loader<Item=Font>
{
inner: Arc<Inner<Tl, Fl>>,
}
#[derive(Debug)]
pub(super) struct Inner<Tl, Fl>
where Tl: Loader<Item=Template>, Fl: Loader<Item=Font>
{
pub template_loader: CachingLoader<Tl>,
pub font_loader: CachingLoader<Fl>,
}
impl<Tl, Fl> From<Inner<Tl, Fl>> for Engine<Tl, Fl>
where Tl: Loader<Item=Template>, Fl: Loader<Item=Font>
{
fn from(inner: Inner<Tl, Fl>) -> Self {
Engine{inner: Arc::new(inner)}
}
}
impl Engine<TemplateLoader, FontLoader> {
#[inline]
pub fn new<Dt, Df>(template_directory: Dt, font_directory: Df) -> Self
where Dt: AsRef<Path>, Df: AsRef<Path>
{
Engine::with_loaders(
TemplateLoader::new(template_directory),
FontLoader::new(font_directory),
)
}
}
impl<Tl, Fl> Engine<Tl, Fl>
where Tl: Loader<Item=Template>, Fl: Loader<Item=Font>
{
#[inline]
pub fn with_loaders(template_loader: Tl, font_loader: Fl) -> Self {
Engine::from(Inner{
template_loader: CachingLoader::new(template_loader, DEFAULT_TEMPLATE_CAPACITY),
font_loader: CachingLoader::new(font_loader, DEFAULT_FONT_CAPACITY),
})
}
#[inline]
pub fn with_raw_loaders(template_loader: Tl, font_loader: Fl) -> Self {
Engine::from(Inner{
template_loader: CachingLoader::phony(template_loader),
font_loader: CachingLoader::phony(font_loader),
})
}
}
impl<Tl, Fl> Engine<Tl, Fl>
where Tl: Loader<Item=Template>, Fl: Loader<Item=Font>
{
#[inline]
pub fn caption(&self, image_macro: ImageMacro) -> Result<CaptionOutput, CaptionError<Tl, Fl>> {
CaptionTask::new(image_macro, self.inner.clone()).perform()
}
}
impl<Tl, Fl> Engine<Tl, Fl>
where Tl: Loader<Item=Template>, Fl: Loader<Item=Font>
{
pub fn preload_template(&self, name: &str) -> Result<(), Tl::Err> {
if !self.inner.template_loader.phony {
self.inner.template_loader.load(name)?;
}
Ok(())
}
pub fn preload_font(&self, name: &str) -> Result<(), Fl::Err> {
if !self.inner.font_loader.phony {
self.inner.font_loader.load(name)?;
}
Ok(())
}
pub fn template_cache(&self) -> Option<&ThreadSafeCache<String, Tl::Item>> {
if self.inner.template_loader.phony {
None
} else {
Some(self.inner.template_loader.cache())
}
}
pub fn font_cache(&self) -> Option<&ThreadSafeCache<String, Fl::Item>> {
if self.inner.font_loader.phony {
None
} else {
Some(self.inner.font_loader.cache())
}
}
}
#[cfg(test)]
mod tests {
use super::Engine;
#[test]
fn thread_safe() {
fn assert_sync<T: Sync>() {}
fn assert_send<T: Send>() {}
assert_sync::<Engine>();
assert_send::<Engine>();
}
}