1use std::time::Duration;
4
5include!("timeouts.generated.rs");
6
7pub fn script_operation(target: &str, payload: &str) -> Option<Operation> {
8 let forwarded = FORWARD_PREFIXES
9 .iter()
10 .find_map(|prefix| target.strip_prefix(prefix));
11 let target = forwarded.unwrap_or(target);
12 if STORED_SCRIPT_SAVE.contains(&target) {
13 return Some(Operation::ScriptSave);
14 }
15 let saving = SOURCE_SCRIPT_SAVE.contains(&target);
16 if !saving && !SOURCE_SCRIPT_VALIDATION.contains(&target) {
17 return None;
18 }
19 let root: serde_json::Value = serde_json::from_str(payload).ok()?;
20 let input = if forwarded.is_some() {
21 root.get("call")?.get("input")?
22 } else if target.starts_with("/app/") {
23 root.get("input")?
24 } else {
25 &root
26 };
27 let scripts = input.get("script")?.get("scripts")?.as_object()?;
28 (!scripts.is_empty()).then_some(if saving {
29 Operation::ScriptSave
30 } else {
31 Operation::ScriptValidation
32 })
33}
34
35pub fn ordinary_operation(target: &str) -> Operation {
36 let target = FORWARD_PREFIXES
37 .iter()
38 .find_map(|prefix| target.strip_prefix(prefix))
39 .unwrap_or(target);
40 ORDINARY_OVERRIDES
41 .iter()
42 .find(|(value, _)| *value == target)
43 .map_or(Operation::CoreRequest, |(_, operation)| *operation)
44}
45
46pub fn request_operation(target: &str, payload: &str) -> Operation {
47 script_operation(target, payload).unwrap_or_else(|| ordinary_operation(target))
48}
49
50pub fn reply_timeout(operation: Operation, profile: WaitProfile) -> Duration {
51 operation.timeout() + profile.grace(operation)
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn shared_selection_and_wait_fixtures() {
60 let cases: serde_json::Value = serde_json::from_str(include_str!(
61 "../fixtures/operation-timeouts.conformance.json"
62 ))
63 .unwrap();
64 for case in cases.as_array().unwrap() {
65 let operation = request_operation(
66 case["target"].as_str().unwrap(),
67 &case["payload"].to_string(),
68 );
69 assert_eq!(
70 operation.id(),
71 case["operation"].as_str().unwrap(),
72 "{case}"
73 );
74 assert_eq!(
75 operation.timeout().as_millis(),
76 case["totalMs"].as_u64().unwrap() as u128
77 );
78 if let Some(wait) = case["lionWaitMs"].as_u64() {
79 assert_eq!(
80 reply_timeout(operation, WaitProfile::LionForward).as_millis(),
81 wait as u128
82 );
83 }
84 }
85 }
86}