predicator_rs/execution.rs
1//! What a statement program answers: the run it completed, or the run that
2//! stopped, each with the context it left and the roots it stored into.
3
4use crate::{Context, Error, Key, Value};
5use core::fmt;
6
7/// A statement program that ran to its end ([`execute`](crate::execute),
8/// [`execute_value`](crate::execute_value)): the context it left, the roots
9/// it stored into and, from [`execute_value`](crate::execute_value), the
10/// value of its last expression statement.
11///
12/// ```
13/// use predicator_rs::{Context, Instructions, Key, Options, Value, execute};
14///
15/// let program = Instructions::from_json(r#"[["lit","x"],["lit",1],["store",1]]"#)?;
16/// let execution = execute(&program, &Context::new(), &Options::new())?;
17/// assert_eq!(execution.context().get(&Key::from("x")), Some(&Value::from(1)));
18/// assert_eq!(execution.stored_roots(), [Key::from("x")]);
19/// assert_eq!(execution.value(), None);
20/// # Ok::<(), predicator_rs::Error>(())
21/// ```
22#[derive(Debug, Clone, PartialEq)]
23pub struct Execution {
24 pub(crate) context: Context,
25 pub(crate) value: Option<Value>,
26 pub(crate) stored_roots: Vec<Key>,
27}
28
29impl Execution {
30 /// The context the program left: the one it was given with every write
31 /// it made. The context passed in is never changed.
32 ///
33 /// ```
34 /// use predicator_rs::{Context, Key, Options, Value, execute};
35 ///
36 /// let execution = execute("x = 1", &Context::new(), &Options::new())?;
37 /// let x = execution.context().get(&Key::from("x"));
38 /// assert_eq!(x, Some(&Value::from(1)));
39 /// # Ok::<(), predicator_rs::Error>(())
40 /// ```
41 pub fn context(&self) -> &Context {
42 &self.context
43 }
44
45 /// The context the program left, taken.
46 ///
47 /// ```
48 /// use predicator_rs::{Context, Key, Options, Value, execute};
49 ///
50 /// let execution = execute("x = 1", &Context::new(), &Options::new())?;
51 /// let mut context = execution.into_context();
52 /// context.insert("y", Value::from(2));
53 /// assert_eq!(context.get(&Key::from("x")), Some(&Value::from(1)));
54 /// # Ok::<(), predicator_rs::Error>(())
55 /// ```
56 pub fn into_context(self) -> Context {
57 self.context
58 }
59
60 /// The value of the program's last expression statement (what its last
61 /// `pop` discarded, undefined when it popped nothing), when
62 /// [`execute_value`](crate::execute_value) made the run; `None` when
63 /// [`execute`](crate::execute) made it.
64 ///
65 /// ```
66 /// use predicator_rs::{Context, Options, Value, execute, execute_value};
67 ///
68 /// let options = Options::new();
69 /// let kept = execute_value("x = 1; x + 1", &Context::new(), &options)?;
70 /// assert_eq!(kept.value(), Some(&Value::from(2)));
71 /// let dropped = execute("x = 1; x + 1", &Context::new(), &options)?;
72 /// assert_eq!(dropped.value(), None);
73 /// # Ok::<(), predicator_rs::Error>(())
74 /// ```
75 pub fn value(&self) -> Option<&Value> {
76 self.value.as_ref()
77 }
78
79 /// The roots the program stored into, each once, in the order of its
80 /// first store, whether or not the store changed the value there.
81 ///
82 /// ```
83 /// use predicator_rs::{Context, Key, Options, execute};
84 ///
85 /// let program = "b = 1; a = 2; b = 3";
86 /// let execution = execute(program, &Context::new(), &Options::new())?;
87 /// assert_eq!(execution.stored_roots(), [Key::from("b"), Key::from("a")]);
88 /// # Ok::<(), predicator_rs::Error>(())
89 /// ```
90 pub fn stored_roots(&self) -> &[Key] {
91 &self.stored_roots
92 }
93}
94
95/// A statement program that stopped before its end: the [`Error`] it
96/// stopped on, the context as of the last statement it completed (every
97/// earlier write kept, the failing write not made) and the roots it had
98/// stored into by then. The context passed in is never changed; a caller
99/// that wants all or nothing keeps the one it has.
100///
101/// It converts into its [`Error`], so `?` takes a stopped run where only the
102/// error matters.
103///
104/// ```
105/// use predicator_rs::{Context, Instructions, Key, Options, Value, execute};
106///
107/// let program = Instructions::from_json(
108/// r#"[["lit","a"],["lit",1],["store",1],["lit","a"],["lit","b"],["lit",2],["store",2]]"#,
109/// )?;
110/// let stopped = execute(&program, &Context::new(), &Options::new()).unwrap_err();
111/// assert_eq!(stopped.error().reason(), "not_a_container");
112/// assert_eq!(stopped.context().get(&Key::from("a")), Some(&Value::from(1)));
113/// assert_eq!(stopped.stored_roots(), [Key::from("a")]);
114/// # Ok::<(), predicator_rs::Error>(())
115/// ```
116#[derive(Debug, Clone, PartialEq)]
117pub struct Stopped {
118 pub(crate) error: Box<Error>,
119 pub(crate) context: Context,
120 pub(crate) stored_roots: Vec<Key>,
121}
122
123impl Stopped {
124 /// The error the run stopped on.
125 ///
126 /// ```
127 /// use predicator_rs::{Context, Options, execute};
128 ///
129 /// let stopped = execute("x = 1 / 0", &Context::new(), &Options::new()).unwrap_err();
130 /// assert_eq!(stopped.error().reason(), "division_by_zero");
131 /// ```
132 pub fn error(&self) -> &Error {
133 &self.error
134 }
135
136 /// The error the run stopped on, taken.
137 ///
138 /// ```
139 /// use predicator_rs::{Context, Options, execute};
140 ///
141 /// let stopped = execute("x = 1 / 0", &Context::new(), &Options::new()).unwrap_err();
142 /// assert_eq!(stopped.into_error().type_name(), "EvaluationError");
143 /// ```
144 pub fn into_error(self) -> Error {
145 *self.error
146 }
147
148 /// The context as of the last statement the run completed.
149 ///
150 /// ```
151 /// use predicator_rs::{Context, Key, Options, Value, execute};
152 ///
153 /// let program = "x = 1; y = 1 / 0";
154 /// let stopped = execute(program, &Context::new(), &Options::new()).unwrap_err();
155 /// assert_eq!(stopped.context().get(&Key::from("x")), Some(&Value::from(1)));
156 /// assert_eq!(stopped.context().get(&Key::from("y")), None);
157 /// ```
158 pub fn context(&self) -> &Context {
159 &self.context
160 }
161
162 /// The context as of the last statement the run completed, taken.
163 ///
164 /// ```
165 /// use predicator_rs::{Context, Key, Options, Value, execute};
166 ///
167 /// let program = "x = 1; y = 1 / 0";
168 /// let stopped = execute(program, &Context::new(), &Options::new()).unwrap_err();
169 /// let context = stopped.into_context();
170 /// assert_eq!(context.get(&Key::from("x")), Some(&Value::from(1)));
171 /// ```
172 pub fn into_context(self) -> Context {
173 self.context
174 }
175
176 /// The roots the run stored into before it stopped, each once, in the
177 /// order of its first store.
178 ///
179 /// ```
180 /// use predicator_rs::{Context, Key, Options, execute};
181 ///
182 /// let program = "x = 1; y = 1 / 0";
183 /// let stopped = execute(program, &Context::new(), &Options::new()).unwrap_err();
184 /// assert_eq!(stopped.stored_roots(), [Key::from("x")]);
185 /// ```
186 pub fn stored_roots(&self) -> &[Key] {
187 &self.stored_roots
188 }
189}
190
191impl From<Stopped> for Error {
192 /// The error the run stopped on.
193 fn from(stopped: Stopped) -> Error {
194 *stopped.error
195 }
196}
197
198impl fmt::Display for Stopped {
199 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200 fmt::Display::fmt(&self.error, f)
201 }
202}
203
204impl std::error::Error for Stopped {}