Rename builder to context builder and add context to eval scripts
parent
06f64abb18
commit
247550ac4e
@ -0,0 +1,68 @@
|
|||||||
|
mod bindings;
|
||||||
|
mod builder;
|
||||||
|
mod command_group_config;
|
||||||
|
pub use builder::*;
|
||||||
|
pub use command_group_config::CommandGroupConfig;
|
||||||
|
use nu_protocol::{
|
||||||
|
ast::Block,
|
||||||
|
engine::{EngineState, Stack},
|
||||||
|
PipelineData,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
error::{CrateError, CrateResult},
|
||||||
|
utils::parse_nu_script,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Represents the evaluation context of nu scripts and commands
|
||||||
|
/// This context is the state of the engine itself plus the stack
|
||||||
|
/// It stores variables on
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Context {
|
||||||
|
engine_state: EngineState,
|
||||||
|
stack: Stack,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Context {
|
||||||
|
pub fn builder() -> ContextBuilder {
|
||||||
|
ContextBuilder::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Evaluates the given block with the current engine context (stack plus engine state)
|
||||||
|
pub fn eval_block(&mut self, block: &Block, input: PipelineData) -> CrateResult<PipelineData> {
|
||||||
|
nu_engine::eval_block(
|
||||||
|
&self.engine_state,
|
||||||
|
&mut self.stack,
|
||||||
|
block,
|
||||||
|
input,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.map_err(CrateError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Evals nu script as string with the current engine context
|
||||||
|
pub fn eval_raw<S: ToString>(
|
||||||
|
&mut self,
|
||||||
|
contents: S,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> CrateResult<PipelineData> {
|
||||||
|
let block = parse_nu_script(&mut self.engine_state, contents.to_string())?;
|
||||||
|
|
||||||
|
self.eval_block(&block, input)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prints the data of the given pipeline to stdout
|
||||||
|
pub fn print_pipeline(&mut self, pipeline: PipelineData) -> CrateResult<()> {
|
||||||
|
pipeline.print(&self.engine_state, &mut self.stack, false, false)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prints the data of the given pipeline to stderr
|
||||||
|
pub fn print_pipeline_stderr(&mut self, pipeline: PipelineData) -> CrateResult<()> {
|
||||||
|
pipeline.print(&self.engine_state, &mut self.stack, false, true)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,10 @@
|
|||||||
|
pub mod context;
|
||||||
pub(crate) mod error;
|
pub(crate) mod error;
|
||||||
pub(crate) mod into_value;
|
pub(crate) mod into_value;
|
||||||
pub mod state_builder;
|
|
||||||
pub(crate) mod utils;
|
pub(crate) mod utils;
|
||||||
|
|
||||||
pub use into_value::*;
|
pub use into_value::*;
|
||||||
pub use rusty_value;
|
pub use rusty_value;
|
||||||
|
pub use utils::NewEmpty;
|
||||||
|
|
||||||
pub type Error = error::CrateError;
|
pub type Error = error::CrateError;
|
||||||
|
@ -1,11 +1,39 @@
|
|||||||
use nu_protocol::Span;
|
use nu_protocol::{
|
||||||
|
ast::Block,
|
||||||
|
engine::{EngineState, StateWorkingSet},
|
||||||
|
PipelineData, Span,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait SpanEmpty {
|
use crate::error::{CrateError, CrateResult};
|
||||||
|
|
||||||
|
pub trait NewEmpty {
|
||||||
fn empty() -> Self;
|
fn empty() -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpanEmpty for Span {
|
impl NewEmpty for Span {
|
||||||
|
#[inline]
|
||||||
fn empty() -> Self {
|
fn empty() -> Self {
|
||||||
Span::new(0, 0)
|
Span::new(0, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl NewEmpty for PipelineData {
|
||||||
|
#[inline]
|
||||||
|
fn empty() -> Self {
|
||||||
|
Self::new(Span::empty())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_nu_script(engine_state: &mut EngineState, contents: String) -> CrateResult<Block> {
|
||||||
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||||
|
let (block, err) = nu_parser::parse(&mut working_set, None, &contents.into_bytes(), false, &[]);
|
||||||
|
|
||||||
|
if let Some(err) = err {
|
||||||
|
Err(CrateError::from(err))
|
||||||
|
} else {
|
||||||
|
let delta = working_set.render();
|
||||||
|
engine_state.merge_delta(delta)?;
|
||||||
|
|
||||||
|
Ok(block)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
use embed_nu::{
|
||||||
|
context::{CommandGroupConfig, Context},
|
||||||
|
NewEmpty,
|
||||||
|
};
|
||||||
|
use nu_protocol::PipelineData;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_evals_strings() {
|
||||||
|
let mut ctx = get_context();
|
||||||
|
let pipeline = ctx
|
||||||
|
.eval_raw(r#"echo "Hello World""#, PipelineData::empty())
|
||||||
|
.unwrap();
|
||||||
|
ctx.print_pipeline(pipeline).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_context() -> Context {
|
||||||
|
Context::builder()
|
||||||
|
.with_command_groups(CommandGroupConfig::default().all_groups(true))
|
||||||
|
.add_parent_env_vars()
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
Loading…
Reference in New Issue