Skip to content

Commit

Permalink
chore: crate cleanup (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuchsnj committed Apr 13, 2023
1 parent 6505478 commit 566456d
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 104 deletions.
2 changes: 1 addition & 1 deletion benches/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn benchmark_vrl_runtimes(c: &mut Criterion) {
} = vrl::compile(source.program, &functions).unwrap();

group.bench_with_input(BenchmarkId::new(source.name, "ast"), &(), |b, _| {
let state = state::Runtime::default();
let state = state::RuntimeState::default();
let mut runtime = Runtime::new(state);
let target: Value = serde_json::from_str(source.target).expect("valid json");

Expand Down
23 changes: 3 additions & 20 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ regex = { version = "1", default-features = false, optional = true, features = [
rustyline = { version = "11", default-features = false, optional = true }
serde_json = "1"
thiserror = "1"
vrl = { path = "../..", default-features = false }
vrl-compiler = { path = "../compiler" }
vrl-diagnostic = { path = "../diagnostic" }
core = { package = "vrl-core", path = "../core", default-features = false }
value = { path = "../value", default-features = false, features = [] }
webbrowser = { version = "0.8", default-features = false, optional = true }
Expand All @@ -31,23 +32,5 @@ package = "vrl-stdlib"
path = "../stdlib"

[features]
default = ["repl", "expressions"]
default = ["repl"]
repl = ["dep:once_cell", "dep:prettytable-rs", "dep:regex", "dep:rustyline", "dep:webbrowser"]
expressions = [
"expr-abort",
"expr-assignment",
"expr-function_call",
"expr-if_statement",
"expr-literal",
"expr-op",
"expr-query",
"expr-unary",
]
expr-abort = ["vrl/expr-abort"]
expr-assignment = ["vrl/expr-assignment"]
expr-function_call = ["vrl/expr-function_call"]
expr-if_statement = ["vrl/expr-if_statement"]
expr-literal = ["vrl/expr-literal"]
expr-op = ["vrl/expr-op"]
expr-query = ["vrl/expr-query"]
expr-unary = ["vrl/expr-unary"]
19 changes: 11 additions & 8 deletions lib/cli/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ use clap::Parser;
use core::TimeZone;
use lookup::{owned_value_path, OwnedTargetPath};
use value::Secrets;
use vrl::state::TypeState;
use vrl::{diagnostic::Formatter, state, Function, Program, Runtime, Target, VrlRuntime};
use vrl::{CompilationResult, CompileConfig};
use vrl_compiler::runtime::Runtime;
use vrl_compiler::state::RuntimeState;
use vrl_compiler::{
compile_with_state, CompilationResult, CompileConfig, Function, Program, Target, TypeState,
VrlRuntime,
};
use vrl_diagnostic::Formatter;

#[cfg(feature = "repl")]
use super::repl;
Expand Down Expand Up @@ -135,10 +139,9 @@ fn run(opts: &Opts, stdlib_functions: Vec<Box<dyn Function>>) -> Result<(), Erro
program,
warnings,
config: _,
} = vrl::compile_with_state(&source, &stdlib::all(), &state, CompileConfig::default())
.map_err(|diagnostics| {
Error::Parse(Formatter::new(&source, diagnostics).colored().to_string())
})?;
} = compile_with_state(&source, &stdlib::all(), &state, CompileConfig::default()).map_err(
|diagnostics| Error::Parse(Formatter::new(&source, diagnostics).colored().to_string()),
)?;

#[allow(clippy::print_stderr)]
if opts.print_warnings {
Expand All @@ -154,7 +157,7 @@ fn run(opts: &Opts, stdlib_functions: Vec<Box<dyn Function>>) -> Result<(), Erro
metadata: &mut metadata,
secrets: &mut secrets,
};
let state = state::Runtime::default();
let state = RuntimeState::default();
let runtime = Runtime::new(state);

let result = execute(&mut target, &program, tz, runtime, opts.runtime).map(|v| {
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod cmd;
mod repl;

pub use cmd::{cmd, Opts};
use vrl_compiler::runtime::Terminate;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -28,7 +29,7 @@ pub enum Error {
Parse(String),

#[error(transparent)]
Runtime(#[from] vrl::Terminate),
Runtime(#[from] Terminate),

#[error("input error: {}", .0)]
Json(#[from] serde_json::Error),
Expand Down
18 changes: 9 additions & 9 deletions lib/cli/src/repl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::TargetValue;
use std::borrow::Cow::{self, Borrowed, Owned};
use std::collections::BTreeMap;
use std::rc::Rc;

use ::value::Value;
Expand All @@ -19,11 +20,10 @@ use rustyline::{
Context, Editor, Helper,
};
use value::Secrets;
use vrl::state::TypeState;
use vrl::{
diagnostic::Formatter, prelude::BTreeMap, state, CompileConfig, Function, Runtime, Target,
VrlRuntime,
};
use vrl_compiler::runtime::Runtime;
use vrl_compiler::state::{RuntimeState, TypeState};
use vrl_compiler::{compile_with_state, CompileConfig, Function, Program, Target, VrlRuntime};
use vrl_diagnostic::Formatter;

// Create a list of all possible error values for potential docs lookup
static ERRORS: Lazy<Vec<String>> = Lazy::new(|| {
Expand Down Expand Up @@ -64,7 +64,7 @@ pub(crate) fn run(

let mut state = TypeState::default();

let mut rt = Runtime::new(state::Runtime::default());
let mut rt = Runtime::new(RuntimeState::default());
let mut rl = Editor::<Repl, MemHistory>::new()?;
rl.set_helper(Some(Repl::new(stdlib_functions.clone())));

Expand Down Expand Up @@ -169,7 +169,7 @@ fn resolve(
// The CLI should be moved out of the "vrl" module, and then it can use the `vector-core::compile_vrl` function which includes this automatically
config.set_read_only_path(OwnedTargetPath::metadata(owned_value_path!("vector")), true);

let program = match vrl::compile_with_state(program, stdlib_functions, state, config) {
let program = match compile_with_state(program, stdlib_functions, state, config) {
Ok(result) => result.program,
Err(diagnostics) => {
return Err(Formatter::new(program, diagnostics).colored().to_string());
Expand All @@ -182,7 +182,7 @@ fn resolve(

fn execute(
runtime: &mut Runtime,
program: &vrl::Program,
program: &Program,
object: &mut dyn Target,
timezone: TimeZone,
vrl_runtime: VrlRuntime,
Expand Down Expand Up @@ -294,7 +294,7 @@ impl Validator for Repl {
) -> rustyline::Result<ValidationResult> {
let timezone = TimeZone::default();
let mut state = TypeState::default();
let mut rt = Runtime::new(state::Runtime::default());
let mut rt = Runtime::new(RuntimeState::default());
let mut target = TargetValue {
value: Value::Null,
metadata: Value::Object(BTreeMap::new()),
Expand Down
14 changes: 9 additions & 5 deletions lib/compiler/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use core::TimeZone;

use crate::{state::Runtime, Target};
use crate::{state::RuntimeState, Target};

pub struct Context<'a> {
target: &'a mut dyn Target,
state: &'a mut Runtime,
state: &'a mut RuntimeState,
timezone: &'a TimeZone,
}

impl<'a> Context<'a> {
/// Create a new [`Context`].
pub fn new(target: &'a mut dyn Target, state: &'a mut Runtime, timezone: &'a TimeZone) -> Self {
pub fn new(
target: &'a mut dyn Target,
state: &'a mut RuntimeState,
timezone: &'a TimeZone,
) -> Self {
Self {
target,
state,
Expand All @@ -31,12 +35,12 @@ impl<'a> Context<'a> {

/// Get a reference to the [`runtime state`](Runtime).
#[must_use]
pub fn state(&self) -> &Runtime {
pub fn state(&self) -> &RuntimeState {
self.state
}

/// Get a mutable reference to the [`runtime state`](Runtime).
pub fn state_mut(&mut self) -> &mut Runtime {
pub fn state_mut(&mut self) -> &mut RuntimeState {
self.state
}

Expand Down
6 changes: 3 additions & 3 deletions lib/compiler/src/function/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use value::{

use super::Example;
use crate::{
state::Runtime,
state::RuntimeState,
value::{Kind, VrlValueConvert},
Context,
};
Expand Down Expand Up @@ -263,11 +263,11 @@ where
}
}

fn insert(state: &mut Runtime, ident: Option<&Ident>, data: Value) -> Option<Value> {
fn insert(state: &mut RuntimeState, ident: Option<&Ident>, data: Value) -> Option<Value> {
ident.and_then(|ident| state.swap_variable(ident.clone(), data))
}

fn cleanup(state: &mut Runtime, ident: Option<&Ident>, data: Option<Value>) {
fn cleanup(state: &mut RuntimeState, ident: Option<&Ident>, data: Option<Value>) {
match (ident, data) {
(Some(ident), Some(value)) => {
state.insert_variable(ident.clone(), value);
Expand Down
36 changes: 36 additions & 0 deletions lib/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ mod test_util;

pub mod expression;
pub mod function;
pub mod runtime;
pub mod state;
pub mod type_def;
pub mod value;

pub use self::compile_config::CompileConfig;
pub use self::deprecation_warning::DeprecationWarning;
use ::parser::parse;
pub use compiler::{CompilationResult, Compiler};
pub use core::{
value, ExpressionError, Resolved, SecretTarget, Target, TargetValue, TargetValueRef,
Expand All @@ -64,6 +66,40 @@ pub use type_def::TypeDef;

pub type Result<T = CompilationResult> = std::result::Result<T, DiagnosticList>;

/// Compile a given source into the final [`Program`].
pub fn compile(source: &str, fns: &[Box<dyn Function>]) -> Result {
let external = state::ExternalEnv::default();
let config = CompileConfig::default();

compile_with_external(source, fns, &external, config)
}

pub fn compile_with_external(
source: &str,
fns: &[Box<dyn Function>],
external: &state::ExternalEnv,
config: CompileConfig,
) -> Result {
let state = TypeState {
local: state::LocalEnv::default(),
external: external.clone(),
};

compile_with_state(source, fns, &state, config)
}

pub fn compile_with_state(
source: &str,
fns: &[Box<dyn Function>],
state: &TypeState,
config: CompileConfig,
) -> Result {
let ast = parse(source)
.map_err(|err| diagnostic::DiagnosticList::from(vec![Box::new(err) as Box<_>]))?;

Compiler::compile(fns, ast, state, config)
}

/// Available VRL runtimes.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
Expand Down
14 changes: 9 additions & 5 deletions src/runtime.rs → lib/compiler/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use core::TimeZone;
use std::{error::Error, fmt};

use compiler::ExpressionError;
use super::ExpressionError;
use lookup::OwnedTargetPath;
use value::Value;

use crate::{state, Context, Program, Target, TimeZone};
use crate::{state, Context, Program, Target};

pub type RuntimeResult = Result<Value, Terminate>;

#[derive(Debug, Default)]
pub struct Runtime {
state: state::Runtime,
state: state::RuntimeState,
}

/// The error raised if the runtime is terminated.
Expand All @@ -28,6 +29,7 @@ pub enum Terminate {
}

impl Terminate {
#[must_use]
pub fn get_expression_error(self) -> ExpressionError {
match self {
Terminate::Abort(error) => error,
Expand All @@ -52,10 +54,12 @@ impl Error for Terminate {
}

impl Runtime {
pub fn new(state: state::Runtime) -> Self {
#[must_use]
pub fn new(state: state::RuntimeState) -> Self {
Self { state }
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.state.is_empty()
}
Expand All @@ -82,7 +86,7 @@ impl Runtime {
}
Err(err) => {
return Err(Terminate::Error(
format!("error querying target object: {}", err).into(),
format!("error querying target object: {err}").into(),
))
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ impl ExternalEnv {

/// The state used at runtime to track changes as they happen.
#[derive(Debug, Default)]
pub struct Runtime {
pub struct RuntimeState {
/// The [`Value`] stored in each variable.
variables: HashMap<Ident, Value>,
}

impl Runtime {
impl RuntimeState {
#[must_use]
pub fn is_empty(&self) -> bool {
self.variables.is_empty()
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ macro_rules! bench_function {

let (expression, want) = $crate::__prep_bench_or_test!($func, &state, $args, $(Ok(::value::Value::from($ok)))? $(Err($err.to_owned()))?);
let expression = expression.unwrap();
let mut runtime_state = $crate::state::Runtime::default();
let mut runtime_state = $crate::state::RuntimeState::default();
let mut target: ::value::Value = ::std::collections::BTreeMap::default().into();
let tz = vrl_core::TimeZone::Named(chrono_tz::Tz::UTC);
let mut ctx = $crate::Context::new(&mut target, &mut runtime_state, &tz);
Expand Down Expand Up @@ -93,7 +93,7 @@ macro_rules! test_function {
let (expression, want) = $crate::__prep_bench_or_test!($func, &state, $args, $(Ok(::value::Value::from($ok)))? $(Err($err.to_owned()))?);
match expression {
Ok(expression) => {
let mut runtime_state = $crate::state::Runtime::default();
let mut runtime_state = $crate::state::RuntimeState::default();
let mut target: ::value::Value = ::std::collections::BTreeMap::default().into();
let tz = $tz;
let mut ctx = $crate::Context::new(&mut target, &mut runtime_state, &tz);
Expand Down
2 changes: 1 addition & 1 deletion lib/stdlib/src/del.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ mod tests {
let tz = TimeZone::default();
for (object, exp, func) in cases {
let mut object: Value = object.into();
let mut runtime_state = vrl::state::Runtime::default();
let mut runtime_state = vrl::state::RuntimeState::default();
let mut ctx = Context::new(&mut object, &mut runtime_state, &tz);
let got = func
.resolve(&mut ctx)
Expand Down
4 changes: 2 additions & 2 deletions lib/stdlib/src/to_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod tests {
#[test]
fn out_of_range_integer() {
let mut object: Value = BTreeMap::new().into();
let mut runtime_state = vrl::state::Runtime::default();
let mut runtime_state = vrl::state::RuntimeState::default();
let tz = TimeZone::default();
let mut ctx = Context::new(&mut object, &mut runtime_state, &tz);
let f = ToTimestampFn {
Expand All @@ -293,7 +293,7 @@ mod tests {
#[test]
fn out_of_range_float() {
let mut object: Value = BTreeMap::new().into();
let mut runtime_state = vrl::state::Runtime::default();
let mut runtime_state = vrl::state::RuntimeState::default();
let tz = TimeZone::default();
let mut ctx = Context::new(&mut object, &mut runtime_state, &tz);
let f = ToTimestampFn {
Expand Down
2 changes: 1 addition & 1 deletion lib/stdlib/src/unnest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ mod tests {
let tz = TimeZone::default();
for (object, expected, func, expected_typedef) in cases {
let mut object = object.clone();
let mut runtime_state = vrl::state::Runtime::default();
let mut runtime_state = vrl::state::RuntimeState::default();
let mut ctx = Context::new(&mut object, &mut runtime_state, &tz);

let got_typedef = func.type_def(&state);
Expand Down
Loading

0 comments on commit 566456d

Please sign in to comment.