1use std::sync::Arc;
2
3use derive_getters::Getters;
4use scratch_test_interpreter::Limits;
5use scratch_test_model::{Id, ProjectDoc};
6
7use crate::{
8 LogPfx,
9 catchable::cerr,
10 evaluation::{
11 AllowNetData, Context, PSpec, SelectableSource, process_spec::SpecRunError,
12 single_evaluation::AllowedNetClosure,
13 },
14 spec::{Bast3StSpec, RealMapping},
15};
16
17pub trait ReportBuilderState {
18 type Spec<'a>;
19}
20
21#[derive(derive_more::Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
22pub struct WithoutSpec(());
23#[derive(derive_more::Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
24pub struct WithSpec(());
25
26impl ReportBuilderState for WithoutSpec {
27 type Spec<'a> = ();
28}
29impl ReportBuilderState for WithSpec {
30 type Spec<'a> = &'a Bast3StSpec;
31}
32
33#[derive(derive_more::Debug, Clone)]
34pub struct ReportBuilder<'a, Fallback = (), State: ReportBuilderState = WithoutSpec> {
35 limits: EvalLimitations,
36 #[debug("allowed_network: ...")]
37 allowed_network: Option<AllowedNetClosure>,
38 log_pfx: Option<LogPfx>,
39 param_my: Option<RealMapping>,
40 fallback: Option<Fallback>,
41 spec: State::Spec<'a>,
42 _phantom: std::marker::PhantomData<State>,
43}
44impl Default for ReportBuilder<'static, (), WithoutSpec> {
45 fn default() -> Self {
46 Self::no_limits()
47 }
48}
49
50impl<'a, Fallback: SelectableSource, X: ReportBuilderState> ReportBuilder<'a, Fallback, X> {
51 pub const fn with_max_list_length(mut self, max: Option<u32>) -> Self {
52 self.limits.max_list_length = max;
53 self
54 }
55 pub const fn with_max_string_length(mut self, max: Option<usize>) -> Self {
56 self.limits.max_string_length = max;
57 self
58 }
59 pub const fn with_max_executed_stmts(mut self, max: Option<usize>) -> Self {
60 self.limits.max_executed_stmts = max;
61 self
62 }
63 pub const fn with_max_statement_stack_height(mut self, max: Option<usize>) -> Self {
64 self.limits.max_statement_stack_height = max;
65 self
66 }
67 pub const fn with_eval_limits(mut self, limits: EvalLimitations) -> Self {
68 self.limits = limits;
69 self
70 }
71
72 pub fn with_allowed_network(mut self, net: Option<AllowedNetClosure>) -> Self {
73 self.allowed_network = net;
74 self
75 }
76 pub fn with_log_pfx(mut self, log_pfx: Option<LogPfx>) -> Self {
77 self.log_pfx = log_pfx;
78 self
79 }
80 pub fn with_fallback(mut self, fb: Option<Fallback>) -> Self {
81 self.fallback = fb;
82 self
83 }
84
85 pub fn add_allow_network_map_check(
86 self,
87 closure: impl Fn(AllowNetData) -> Result<AllowNetData, (AllowNetData, cerr)>
88 + Send
89 + Sync
90 + 'static,
91 ) -> Self {
92 if let Some(first_check) = self.allowed_network.clone() {
93 self.only_allow_network_map(move |input| {
94 let input = first_check(input)?;
95 closure(input)
96 })
97 } else {
98 self.only_allow_network_map(closure)
99 }
100 }
101 fn only_allow_network_map(
104 mut self,
105 closure: impl Fn(AllowNetData) -> Result<AllowNetData, (AllowNetData, cerr)>
106 + Send
107 + Sync
108 + 'static,
109 ) -> Self {
110 self.allowed_network = Some(Arc::from(closure));
111 self
112 }
113 #[allow(unused)]
114 fn only_allow_network_if(
117 mut self,
118 closure: impl Fn(&AllowNetData) -> bool + Send + Sync + 'static,
119 ) -> Self {
120 self.allowed_network = None;
121 self.add_allow_network_if_check(closure)
122 }
123 pub fn add_allow_network_if_check(
124 self,
125 closure: impl Fn(&AllowNetData) -> bool + Send + Sync + 'static,
126 ) -> Self {
127 self.add_allow_network_map_check(move |x| {
128 if closure(&x) {
129 Ok(x)
130 } else {
131 Err((x, cerr::network_policy_other))
132 }
133 })
134 }
135
136 pub fn add_allow_network_cerr_check(
137 self,
138 closure: impl Fn(&AllowNetData) -> Result<(), cerr> + Send + Sync + 'static,
139 ) -> Self {
140 self.add_allow_network_map_check(move |x| match closure(&x) {
141 Ok(()) => Ok(x),
142 Err(err) => Err((x, err)),
143 })
144 }
145
146 pub fn disallow_network(mut self) -> Self {
148 self.allowed_network = None;
149 self
150 }
151}
152
153impl ReportBuilder<'static, (), WithoutSpec> {
154 pub const fn no_limits() -> Self {
155 Self {
156 limits: EvalLimitations::no_limits(),
157 allowed_network: None,
158 log_pfx: None,
159 param_my: None,
160 fallback: None,
161 spec: (),
162 _phantom: std::marker::PhantomData,
163 }
164 }
165 pub const fn new_good_limits() -> Self {
166 Self::no_limits().with_eval_limits(EvalLimitations::NICELY_RESTRICTIVE)
167 }
168}
169
170impl<Fallback: SelectableSource> ReportBuilder<'static, Fallback, WithoutSpec> {
171 pub fn with_spec<'a>(self, spec: &'a Bast3StSpec) -> ReportBuilder<'a, Fallback, WithSpec> {
172 ReportBuilder {
173 limits: self.limits,
174 allowed_network: self.allowed_network,
175 log_pfx: self.log_pfx,
176 param_my: self.param_my,
177 fallback: self.fallback,
178 spec,
179 _phantom: std::marker::PhantomData,
180 }
181 }
182}
183impl<'a, Fallback: SelectableSource> ReportBuilder<'a, Fallback, WithSpec> {
184 pub fn build(
185 self,
186 doc: ProjectDoc,
187 initial_block: Id,
188 ) -> (Context, ExtraBuilderInfo<'a, Fallback>) {
189 (
190 Context {
191 max_list_length: self.limits.max_list_length.unwrap_or(u32::MAX),
192 limits: self.limits.into(),
193 doc,
194 initial_block,
195 entities: self.spec.entities().clone(),
196 allowed_network: self
197 .allowed_network
198 .unwrap_or(Arc::new(|x| Err((x, cerr::network_policy_other)))),
199 },
200 (
201 self.log_pfx.unwrap_or(LogPfx::new("")),
202 self.param_my,
203 self.fallback,
204 self.spec,
205 ),
206 )
207 }
208 pub async fn run(
209 self,
210 doc: ProjectDoc,
211 initial_block: Option<Id>,
212 ) -> Result<PSpec, SpecRunError>
213 where
214 Fallback: Clone + 'static,
215 {
216 PSpec::new(self, doc, initial_block).await
217 }
218
219 pub async fn run_from_unique_flag(self, doc: ProjectDoc) -> Result<PSpec, SpecRunError>
220 where
221 Fallback: Clone + 'static,
222 {
223 PSpec::new(self, doc, None).await
224 }
225}
226pub type ExtraBuilderInfo<'a, Fallback> = (
227 LogPfx,
228 Option<RealMapping>,
229 Option<Fallback>,
230 &'a Bast3StSpec,
231);
232
233#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Default, Getters)]
234pub struct EvalLimitations {
235 max_list_length: Option<u32>,
236 max_string_length: Option<usize>,
237 max_executed_stmts: Option<usize>,
238 max_statement_stack_height: Option<usize>,
239}
240
241impl EvalLimitations {
242 pub const NICELY_RESTRICTIVE: EvalLimitations = EvalLimitations {
243 max_list_length: Some(255),
244 max_string_length: Some(255),
245 max_executed_stmts: Some(10 * 1024),
246 max_statement_stack_height: Some(128),
247 };
248
249 pub const fn no_limits() -> Self {
250 Self {
251 max_list_length: None,
252 max_string_length: None,
253 max_executed_stmts: None,
254 max_statement_stack_height: None,
255 }
256 }
257 pub const fn from_limits(max_list_length: Option<u32>, limits: Limits) -> Self {
258 Self {
259 max_list_length,
260 max_string_length: *limits.max_string_length(),
261 max_executed_stmts: *limits.max_executed_stmts(),
262 max_statement_stack_height: *limits.max_statement_stack_height(),
263 }
264 }
265}
266
267impl From<Limits> for EvalLimitations {
268 fn from(value: Limits) -> Self {
269 Self::from_limits(None, value)
270 }
271}
272impl From<EvalLimitations> for Limits {
273 fn from(value: EvalLimitations) -> Self {
274 Self::new()
275 .with_max_string_length(value.max_string_length)
276 .with_max_executed_stmts(value.max_executed_stmts)
277 .with_max_statement_stack_height(value.max_statement_stack_height)
278 }
279}