nushell/crates/nu-protocol/src/ast/block.rs

72 lines
1.4 KiB
Rust
Raw Normal View History

2021-09-02 10:25:22 +02:00
use std::ops::{Index, IndexMut};
use crate::{Signature, Span, VarId};
2021-09-06 01:16:27 +02:00
2021-09-02 20:21:37 +02:00
use super::Statement;
2021-09-02 10:25:22 +02:00
#[derive(Debug, Clone)]
pub struct Block {
2021-09-06 01:16:27 +02:00
pub signature: Box<Signature>,
2021-09-02 10:25:22 +02:00
pub stmts: Vec<Statement>,
2021-10-25 22:04:23 +02:00
pub captures: Vec<VarId>,
pub redirect_env: bool,
pub span: Option<Span>, // None option encodes no span to avoid using test_span()
2021-09-02 10:25:22 +02:00
}
impl Block {
pub fn len(&self) -> usize {
self.stmts.len()
}
pub fn is_empty(&self) -> bool {
self.stmts.is_empty()
}
}
impl Index<usize> for Block {
type Output = Statement;
fn index(&self, index: usize) -> &Self::Output {
&self.stmts[index]
}
}
impl IndexMut<usize> for Block {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.stmts[index]
}
}
impl Default for Block {
fn default() -> Self {
Self::new()
}
}
impl Block {
pub fn new() -> Self {
2021-09-06 01:16:27 +02:00
Self {
signature: Box::new(Signature::new("")),
stmts: vec![],
2021-10-25 22:04:23 +02:00
captures: vec![],
redirect_env: false,
span: None,
}
}
2021-09-02 10:25:22 +02:00
}
2021-09-10 09:28:43 +02:00
impl<T> From<T> for Block
where
T: Iterator<Item = Statement>,
{
fn from(stmts: T) -> Self {
Self {
signature: Box::new(Signature::new("")),
stmts: stmts.collect(),
2021-10-25 22:04:23 +02:00
captures: vec![],
redirect_env: false,
span: None,
2021-09-10 09:28:43 +02:00
}
}
}