Fixing captures (#723)

* WIP fixing captures

* small fix

* WIP

* Rewrite to proof-of-concept better parse_def

* Add missing file

* Finish capture refactor

* Fix tests

* Add more tests
This commit is contained in:
JT
2022-01-12 15:06:56 +11:00
committed by GitHub
parent 47495715a6
commit 186da4d725
30 changed files with 424 additions and 164 deletions

View File

@ -0,0 +1,9 @@
use std::collections::HashMap;
use crate::{BlockId, Value, VarId};
#[derive(Clone, Debug)]
pub struct CaptureBlock {
pub block_id: BlockId,
pub captures: HashMap<VarId, Value>,
}

View File

@ -154,6 +154,7 @@ pub const SCOPE_VARIABLE_ID: usize = 1;
pub const IN_VARIABLE_ID: usize = 2;
pub const CONFIG_VARIABLE_ID: usize = 3;
pub const ENV_VARIABLE_ID: usize = 4;
// NOTE: If you add more to this list, make sure to update the > checks based on the last in the list
impl EngineState {
pub fn new() -> Self {

View File

@ -1,9 +1,11 @@
mod call_info;
mod capture_block;
mod command;
mod engine_state;
mod stack;
pub use call_info::*;
pub use capture_block::*;
pub use command::*;
pub use engine_state::*;
pub use stack::*;

View File

@ -62,7 +62,10 @@ impl Stack {
return Ok(v.clone());
}
Err(ShellError::NushellFailed("variable not found".into()))
Err(ShellError::NushellFailed(format!(
"variable (var_id: {}) not found",
var_id
)))
}
pub fn add_var(&mut self, var_id: VarId, value: Value) {
@ -80,7 +83,24 @@ impl Stack {
}
}
pub fn collect_captures(&self, captures: &[VarId]) -> Stack {
pub fn captures_to_stack(&self, captures: &HashMap<VarId, Value>) -> Stack {
let mut output = Stack::new();
output.vars = captures.clone();
// FIXME: this is probably slow
output.env_vars = self.env_vars.clone();
output.env_vars.push(HashMap::new());
let config = self
.get_var(CONFIG_VARIABLE_ID)
.expect("internal error: config is missing");
output.vars.insert(CONFIG_VARIABLE_ID, config);
output
}
pub fn gather_captures(&self, captures: &[VarId]) -> Stack {
let mut output = Stack::new();
for capture in captures {