lemma/evaluator/
timeout.rs1use crate::{LemmaError, ResourceLimits};
8
9#[cfg(not(target_arch = "wasm32"))]
10use std::time::Instant;
11
12pub struct TimeoutTracker {
17 #[cfg(not(target_arch = "wasm32"))]
18 start_time: Instant,
19}
20
21impl TimeoutTracker {
22 #[cfg(not(target_arch = "wasm32"))]
24 pub fn new() -> Self {
25 Self {
26 start_time: Instant::now(),
27 }
28 }
29
30 #[cfg(target_arch = "wasm32")]
32 pub fn new() -> Self {
33 Self {}
34 }
35
36 #[cfg(not(target_arch = "wasm32"))]
41 pub fn check_timeout(&self, limits: &ResourceLimits) -> Result<(), LemmaError> {
42 let elapsed_ms = self.start_time.elapsed().as_millis() as u64;
43 if elapsed_ms > limits.max_evaluation_time_ms {
44 return Err(LemmaError::ResourceLimitExceeded {
45 limit_name: "max_evaluation_time_ms".to_string(),
46 limit_value: limits.max_evaluation_time_ms.to_string(),
47 actual_value: elapsed_ms.to_string(),
48 suggestion: format!(
49 "Evaluation took {}ms, exceeding the limit of {}ms. Simplify the document or increase the timeout.",
50 elapsed_ms, limits.max_evaluation_time_ms
51 ),
52 });
53 }
54 Ok(())
55 }
56
57 #[cfg(target_arch = "wasm32")]
59 pub fn check_timeout(&self, _limits: &ResourceLimits) -> Result<(), LemmaError> {
60 Ok(())
62 }
63}
64
65impl Default for TimeoutTracker {
66 fn default() -> Self {
67 Self::new()
68 }
69}