You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
3.7 KiB
Rust
145 lines
3.7 KiB
Rust
2 years ago
|
use crate::{
|
||
|
error::CrateResult,
|
||
|
into_value::IntoValue,
|
||
|
utils::{parse_nu_script, NewEmpty},
|
||
|
};
|
||
2 years ago
|
use std::env;
|
||
|
|
||
|
use nu_protocol::{
|
||
2 years ago
|
ast::Block,
|
||
2 years ago
|
engine::{EngineState, Stack, StateWorkingSet},
|
||
2 years ago
|
PipelineData, Span,
|
||
2 years ago
|
};
|
||
2 years ago
|
|
||
2 years ago
|
use super::{CommandGroupConfig, Context};
|
||
2 years ago
|
|
||
2 years ago
|
/// Builder to create a new nu engine state
|
||
2 years ago
|
pub struct ContextBuilder {
|
||
2 years ago
|
engine_state: EngineState,
|
||
|
stack: Stack,
|
||
2 years ago
|
blocks: Vec<Block>,
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
impl Default for ContextBuilder {
|
||
2 years ago
|
fn default() -> Self {
|
||
|
Self {
|
||
|
engine_state: EngineState::new(),
|
||
|
stack: Stack::new(),
|
||
2 years ago
|
blocks: Vec::new(),
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl ContextBuilder {
|
||
2 years ago
|
/// Enables certain command groups specified in the Config on the state
|
||
2 years ago
|
pub fn with_command_groups(mut self, group_config: CommandGroupConfig) -> Self {
|
||
2 years ago
|
macro_rules! toggle_command_groups {
|
||
|
($($group:ident),*) => {
|
||
|
paste::item!(
|
||
|
$(
|
||
|
if group_config.$group {
|
||
2 years ago
|
super::bindings::[<bind_ $group _commands>](&mut self.engine_state);
|
||
2 years ago
|
}
|
||
|
)*
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
toggle_command_groups!(
|
||
|
core,
|
||
|
filter,
|
||
|
chart,
|
||
|
misc,
|
||
|
path,
|
||
|
system,
|
||
|
string,
|
||
|
bit,
|
||
|
byte,
|
||
|
file_system,
|
||
|
platform,
|
||
|
date,
|
||
|
shell,
|
||
|
format,
|
||
|
viewer,
|
||
|
conversion,
|
||
|
environment,
|
||
|
math,
|
||
|
network,
|
||
|
random,
|
||
|
generator,
|
||
|
hash,
|
||
|
experimental
|
||
|
);
|
||
|
self
|
||
|
}
|
||
2 years ago
|
|
||
|
/// Adds a variable to the state
|
||
2 years ago
|
pub fn add_var<S: ToString, V: IntoValue>(mut self, name: S, value: V) -> CrateResult<Self> {
|
||
2 years ago
|
let mut working_set = StateWorkingSet::new(&self.engine_state);
|
||
|
|
||
|
let var_id = working_set.add_variable(
|
||
|
name.to_string().into_bytes(),
|
||
|
Span::empty(),
|
||
|
nu_protocol::Type::Any,
|
||
|
);
|
||
|
self.stack.add_var(var_id, value.into_value());
|
||
|
let delta = working_set.render();
|
||
|
self.engine_state.merge_delta(delta)?;
|
||
|
|
||
|
Ok(self)
|
||
|
}
|
||
|
|
||
|
/// Adds an environment variable to the state
|
||
2 years ago
|
pub fn add_env_var<S: ToString, V: IntoValue>(mut self, name: S, value: V) -> Self {
|
||
2 years ago
|
self.engine_state
|
||
|
.add_env_var(name.to_string(), value.into_value());
|
||
|
|
||
|
self
|
||
|
}
|
||
|
|
||
|
/// Adds the environment variables of the parent process to the
|
||
|
/// states env variables
|
||
2 years ago
|
pub fn add_parent_env_vars(self) -> Self {
|
||
|
let mut builder = self;
|
||
|
|
||
2 years ago
|
for (name, val) in env::vars() {
|
||
2 years ago
|
builder = builder.add_env_var(name, val);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
builder
|
||
|
}
|
||
|
|
||
|
/// Adds a block to the builder
|
||
|
/// This block is evaluated when building to put
|
||
|
/// the blocks contents in scope.
|
||
|
/// Note: Code not contained in declarations is being executed when building
|
||
|
/// the context
|
||
|
pub fn add_block(mut self, block: Block) -> Self {
|
||
|
self.blocks.push(block);
|
||
|
|
||
2 years ago
|
self
|
||
|
}
|
||
2 years ago
|
|
||
|
/// Adds a script to the context.
|
||
|
/// This script is being parsed so this operation can fail
|
||
|
pub fn add_script(mut self, contents: String) -> CrateResult<Self> {
|
||
|
let block = parse_nu_script(&mut self.engine_state, contents)?;
|
||
|
self.blocks.push(block);
|
||
|
|
||
|
Ok(self)
|
||
|
}
|
||
|
|
||
|
/// builds the context
|
||
|
pub fn build(self) -> CrateResult<Context> {
|
||
|
let mut ctx = Context {
|
||
|
engine_state: self.engine_state,
|
||
|
stack: self.stack,
|
||
|
};
|
||
|
for block in self.blocks {
|
||
|
ctx.eval_block(&block, PipelineData::empty())?;
|
||
|
}
|
||
|
|
||
|
Ok(ctx)
|
||
|
}
|
||
2 years ago
|
}
|