vrl/compiler/
program.rs

1use crate::path::OwnedTargetPath;
2
3use super::state::{TypeInfo, TypeState};
4use super::{Context, Expression, Resolved, expression::Block};
5
6#[derive(Debug, Clone)]
7pub struct Program {
8    /// The initial state that the program was compiled with.
9    pub(crate) initial_state: TypeState,
10    pub(crate) expressions: Block,
11    pub(crate) info: ProgramInfo,
12}
13
14impl Program {
15    /// Retrieves the state of the type system before the program runs.
16    #[must_use]
17    pub fn initial_type_state(&self) -> TypeState {
18        self.initial_state.clone()
19    }
20
21    /// Retrieves the state of the type system after the program runs.
22    #[must_use]
23    pub fn final_type_info(&self) -> TypeInfo {
24        self.expressions.type_info(&self.initial_state)
25    }
26
27    /// Get detailed information about the program, as collected by the VRL
28    /// compiler.
29    #[must_use]
30    pub fn info(&self) -> &ProgramInfo {
31        &self.info
32    }
33
34    /// Resolve the program to its final [`Value`](`crate::value::Value`).
35    ///
36    /// # Errors
37    ///
38    /// Returns an error if the program resulted in a runtime error.
39    pub fn resolve(&self, ctx: &mut Context) -> Resolved {
40        self.expressions.resolve(ctx)
41    }
42}
43
44// This type is re-exposed so renaming it is a breaking change.
45#[allow(clippy::module_name_repetitions)]
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct ProgramInfo {
48    /// Returns whether the compiled program can fail at runtime.
49    ///
50    /// A program can only fail at runtime if the fallible-function-call
51    /// (`foo!()`) is used within the source.
52    pub fallible: bool,
53
54    /// Returns whether the compiled program can be aborted at runtime.
55    ///
56    /// A program can only abort at runtime if there's an explicit `abort`
57    /// statement in the source.
58    pub abortable: bool,
59
60    /// A list of possible queries made to the external [`target`](`OwnedTargetPath`) at runtime.
61    pub target_queries: Vec<OwnedTargetPath>,
62
63    /// A list of possible assignments made to the external [`target`](`OwnedTargetPath`) at
64    /// runtime.
65    pub target_assignments: Vec<OwnedTargetPath>,
66}