vvv_engine/capabilities/
session.rs1use crate::protocol::Response;
3use crate::{Answer, Call, Engine, EngineError, Reply};
4
5impl Call {
6 pub fn execute(self, engine: &Engine) -> Reply<Answer> {
9 self.execute_in(engine)
10 }
11
12 pub fn execute_with_cancellation(
14 self,
15 engine: &Engine,
16 cancellation: &crate::ReadCancellation,
17 ) -> Reply<Answer> {
18 let result = if !self.request.is_cancellable() {
19 Err(EngineError::MutationCancellation)
20 } else {
21 cancellation.claim()
22 };
23 if let Err(error) = result {
24 return Reply {
25 id: self.id,
26 response: Response::error(crate::Failure::from(&error)),
27 };
28 }
29 self.execute_in(&engine.with_cancellation(cancellation.clone()))
30 }
31
32 fn execute_in(mut self, engine: &Engine) -> Reply<Answer> {
33 let result = (|| {
34 if let Some(max_bytes) = self.max_output_bytes {
35 if !(crate::ContextBudget::MIN_BYTES..=crate::ContextBudget::MAX_BYTES)
36 .contains(&max_bytes)
37 {
38 return Err(EngineError::InvalidBudget);
39 }
40 if !self.request.is_read_only() {
41 return Err(EngineError::MutationBudget);
42 }
43 }
44 if let (Some(limit), crate::Request::Context(query)) =
45 (self.max_output_bytes, &mut self.request)
46 {
47 query.budget.max_bytes = query.budget.max_bytes.min(limit);
48 }
49 if let Some(limit) = self.max_output_bytes {
50 match &mut self.request {
51 crate::Request::PrepareRewrite(query) => {
52 crate::PlanReviewReply::budget(query.max_bytes, query.page.as_ref())?;
53 query.max_bytes = query.max_bytes.min(limit);
54 }
55 crate::Request::ReviewPlan(query) => {
56 query.page.validate()?;
57 query.page.max_bytes = query.page.max_bytes.min(limit);
58 }
59 crate::Request::PrepareRename(query) => {
60 crate::PageBudget {
61 max_bytes: query.max_bytes,
62 max_items: 1,
63 }
64 .validate()?;
65 query.max_bytes = query.max_bytes.min(limit);
66 }
67 crate::Request::InspectPlan(query) => {
68 crate::PageBudget {
69 max_bytes: query.max_bytes,
70 max_items: 1,
71 }
72 .validate()?;
73 query.max_bytes = query.max_bytes.min(limit);
74 }
75 crate::Request::Relationships(query) => {
76 query.budget.validate()?;
77 query.budget.max_bytes = query.budget.max_bytes.min(limit);
78 }
79 crate::Request::SearchPage(query) => {
80 query.page.validate()?;
81 query.page.max_bytes = query.page.max_bytes.min(limit);
82 }
83 crate::Request::ContextPage(query) => {
84 query.page.validate()?;
85 query.page.max_bytes = query.page.max_bytes.min(limit);
86 }
87 crate::Request::Continue(query) => {
88 query.page.validate()?;
89 query.page.max_bytes = query.page.max_bytes.min(limit);
90 }
91 crate::Request::Expand(query) => {
92 crate::PageBudget {
93 max_items: 1,
94 max_bytes: query.max_bytes,
95 }
96 .validate()?;
97 query.max_bytes = query.max_bytes.min(limit);
98 }
99 _ => {}
100 }
101 }
102 let answer = engine.run(self.request)?.into_answer();
103 if let Some(max_bytes) = self.max_output_bytes {
104 let required_bytes = serde_json::to_vec(&answer)
105 .expect("answer serializes")
106 .len();
107 if required_bytes > max_bytes {
108 return Err(EngineError::OutputLimit {
109 max_bytes,
110 required_bytes,
111 });
112 }
113 }
114 engine.publish_read(|| Ok(answer))
115 })();
116 Reply {
117 id: self.id,
118 response: match result {
119 Ok(answer) => Response::ok(answer),
120 Err(error) => Response::error(crate::Failure::from(&error)),
121 },
122 }
123 }
124}