Skip to content

Commit

Permalink
Move compiler::Resolved to core
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Sichert <mail@pablosichert.com>
  • Loading branch information
pablosichert committed Feb 21, 2022
1 parent 07d6827 commit 894dfa0
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 82 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 1 addition & 80 deletions lib/vrl/compiler/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use array::Array;
pub use assignment::Assignment;
pub use block::Block;
pub use container::{Container, Variant};
pub use core::{ExpressionError, Resolved};
pub use function_argument::FunctionArgument;
pub use function_call::FunctionCall;
pub use group::Group;
Expand All @@ -46,8 +47,6 @@ pub use query::{Query, Target};
pub use unary::Unary;
pub use variable::Variable;

pub type Resolved = Result<Value, ExpressionError>;

pub trait Expression: Send + Sync + fmt::Debug + DynClone {
/// Resolve an expression to a concrete [`Value`].
///
Expand Down Expand Up @@ -363,81 +362,3 @@ impl DiagnosticError for Error {
}
}
}

// -----------------------------------------------------------------------------

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExpressionError {
Abort {
span: Span,
message: Option<String>,
},
Error {
message: String,
labels: Vec<Label>,
notes: Vec<Note>,
},
}

impl std::fmt::Display for ExpressionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.message().fmt(f)
}
}

impl std::error::Error for ExpressionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}

impl DiagnosticError for ExpressionError {
fn code(&self) -> usize {
0
}

fn message(&self) -> String {
use ExpressionError::*;

match self {
Abort { message, .. } => message.clone().unwrap_or_else(|| "aborted".to_owned()),
Error { message, .. } => message.clone(),
}
}

fn labels(&self) -> Vec<Label> {
use ExpressionError::*;

match self {
Abort { span, .. } => {
vec![Label::primary("aborted", span)]
}
Error { labels, .. } => labels.clone(),
}
}

fn notes(&self) -> Vec<Note> {
use ExpressionError::*;

match self {
Abort { .. } => vec![],
Error { notes, .. } => notes.clone(),
}
}
}

impl From<String> for ExpressionError {
fn from(message: String) -> Self {
ExpressionError::Error {
message,
labels: vec![],
notes: vec![],
}
}
}

impl From<&str> for ExpressionError {
fn from(message: &str) -> Self {
message.to_owned().into()
}
}
4 changes: 2 additions & 2 deletions lib/vrl/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub mod vm;

pub use crate::value::Value;
pub use context::Context;
pub use core::{value, Target};
pub use core::{value, ExpressionError, Resolved, Target};
pub(crate) use diagnostic::Span;
pub use expression::{Expression, ExpressionError, Resolved};
pub use expression::Expression;
pub use function::{Function, Parameter};
pub use paste::paste;
pub use program::Program;
Expand Down
1 change: 1 addition & 0 deletions lib/vrl/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ publish = false
[dependencies]
lookup = { path = "../../lookup" }
value = { path = "../../value" }
diagnostic = { package = "vrl-diagnostic", path = "../diagnostic" }
80 changes: 80 additions & 0 deletions lib/vrl/core/src/expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use diagnostic::{DiagnosticError, Label, Note, Span};
use value::Value;

pub type Resolved = Result<Value, ExpressionError>;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExpressionError {
Abort {
span: Span,
message: Option<String>,
},
Error {
message: String,
labels: Vec<Label>,
notes: Vec<Note>,
},
}

impl std::fmt::Display for ExpressionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.message().fmt(f)
}
}

impl std::error::Error for ExpressionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}

impl DiagnosticError for ExpressionError {
fn code(&self) -> usize {
0
}

fn message(&self) -> String {
use ExpressionError::*;

match self {
Abort { message, .. } => message.clone().unwrap_or_else(|| "aborted".to_owned()),
Error { message, .. } => message.clone(),
}
}

fn labels(&self) -> Vec<Label> {
use ExpressionError::*;

match self {
Abort { span, .. } => {
vec![Label::primary("aborted", span)]
}
Error { labels, .. } => labels.clone(),
}
}

fn notes(&self) -> Vec<Note> {
use ExpressionError::*;

match self {
Abort { .. } => vec![],
Error { notes, .. } => notes.clone(),
}
}
}

impl From<String> for ExpressionError {
fn from(message: String) -> Self {
ExpressionError::Error {
message,
labels: vec![],
notes: vec![],
}
}
}

impl From<&str> for ExpressionError {
fn from(message: &str) -> Self {
message.to_owned().into()
}
}
2 changes: 2 additions & 0 deletions lib/vrl/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod expression;
mod r#macro;
mod target;

pub use expression::{ExpressionError, Resolved};
pub use target::Target;
pub use value::Value;

0 comments on commit 894dfa0

Please sign in to comment.