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
use std::time::Duration;
use serde::Serialize;
/// Configuration for `Resolve` instances
///
/// Can be used to increase the internal ping timeout and or if it should reset globals after every execution.
#[derive(Debug, Clone, PartialEq)]
pub struct ResolveConfig {
/// The default timeout for `Script`s if they don't specify their own timeout
pub timeout: Duration,
/// If the module should reset the lua globals between every request.
/// For short, small requests, this can increase performance by a good bit.
/// You just need to make sure you use `local` variables in lua and don't clutter the global table to mess with different scripts
pub reset_globals: bool,
/// Global variables that will always exist.
///
/// These globals won't care for `reset_globals` as that is only for script executed globals.
pub globals: Globals,
/// Enables trace logging in the lua module, logs will be written to a file in the instance's temporary directory ([`dir`](crate::Resolve::dir))
pub tracing: bool,
/// Every so often when you create a [`Resolve`](crate::Resolve) instance, it will run a background cleanup job to remove stale files created by the crate.
///
/// This skips this cleanup and never runs it when creating this instance
pub skip_cleanup: bool,
}
impl ResolveConfig {
/// Default configuration except that globals don't get reset
#[inline]
#[must_use]
pub fn keep_globals() -> Self {
Self {
timeout: Duration::from_secs(30),
reset_globals: false,
globals: Globals::default(),
tracing: false,
skip_cleanup: false,
}
}
}
// builders >
impl ResolveConfig {
/// Set's `timeout` to the specified value.
#[inline]
#[must_use]
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
/// Set's `reset_globals` to the specified value.
#[inline]
#[must_use]
pub fn reset_globals(mut self, reset_globals: bool) -> Self {
self.reset_globals = reset_globals;
self
}
/// Set's `globals` to the specified value.
#[inline]
#[must_use]
pub fn globals(mut self, globals: Globals) -> Self {
self.globals = globals;
self
}
/// Set's `tracing` to the specified value.
#[inline]
#[must_use]
pub fn tracing(mut self, tracing: bool) -> Self {
self.tracing = tracing;
self
}
/// Set's `skip_cleanup` to the specified value.
#[inline]
#[must_use]
pub fn skip_cleanup(mut self, skip_cleanup: bool) -> Self {
self.skip_cleanup = skip_cleanup;
self
}
}
impl Default for ResolveConfig {
fn default() -> Self {
Self {
timeout: Duration::from_secs(30),
reset_globals: true,
globals: Globals::default(),
tracing: false,
skip_cleanup: false,
}
}
}
/// Used to add global variables to [`ResolveConfig`].
///
/// These globals won't ever get reset no matter what and always exist when executing scripts
#[derive(Clone, PartialEq)]
pub struct Globals {
pub(crate) list: Vec<(String, Vec<u8>)>,
}
impl Globals {
/// A new [`Globals`] with no globals specified
#[inline]
#[must_use]
pub fn new() -> Self {
Self { list: Vec::new() }
}
/// A new [`Globals`] with `n` capacity for globals
#[inline]
#[must_use]
pub fn with_capacity(n: usize) -> Self {
Self {
list: Vec::with_capacity(n),
}
}
/// How many globals have been added already
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.list.len()
}
/// Adds a new global `value` with it's name being `key`
///
/// # Errors
/// If the serializing of the value fails
pub fn add<T: Serialize, S: Into<String>>(
&mut self,
key: S,
value: &T,
) -> Result<(), rmp_serde::encode::Error> {
self.list.push((key.into(), rmp_serde::to_vec(value)?));
Ok(())
}
}
impl std::fmt::Debug for Globals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Globals")
.field(
"list",
&format!(
"{:?}",
self.list.iter().map(|s| &s.0).collect::<Vec<&String>>()
),
)
.finish()
}
}
impl Default for Globals {
fn default() -> Self {
Self::new()
}
}