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

74 lines
1.9 KiB
Rust
Raw Normal View History

2022-03-07 21:08:56 +01:00
use serde::{Deserialize, Serialize};
use crate::{span, OverlayId, Span};
use std::collections::HashSet;
2021-09-26 20:39:19 +02:00
2022-03-07 21:08:56 +01:00
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2021-09-26 20:39:19 +02:00
pub enum ImportPatternMember {
Glob { span: Span },
Name { name: Vec<u8>, span: Span },
2021-09-27 02:23:22 +02:00
List { names: Vec<(Vec<u8>, Span)> },
2021-09-26 20:39:19 +02:00
}
2022-03-07 21:08:56 +01:00
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImportPatternHead {
pub name: Vec<u8>,
pub id: Option<OverlayId>,
pub span: Span,
}
2022-03-07 21:08:56 +01:00
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2021-09-26 20:39:19 +02:00
pub struct ImportPattern {
pub head: ImportPatternHead,
2021-09-26 20:39:19 +02:00
pub members: Vec<ImportPatternMember>,
// communicate to eval which decls/aliases were hidden during `parse_hide()` so it does not
// interpret these as env var names:
pub hidden: HashSet<Vec<u8>>,
2021-09-26 20:39:19 +02:00
}
impl ImportPattern {
pub fn new() -> Self {
ImportPattern {
head: ImportPatternHead {
name: vec![],
id: None,
2021-12-19 08:46:13 +01:00
span: Span { start: 0, end: 0 },
},
members: vec![],
hidden: HashSet::new(),
}
}
pub fn span(&self) -> Span {
let mut spans = vec![self.head.span];
for member in &self.members {
match member {
ImportPatternMember::Glob { span } => spans.push(*span),
ImportPatternMember::Name { name: _, span } => spans.push(*span),
ImportPatternMember::List { names } => {
for (_, span) in names {
spans.push(*span);
}
}
}
}
span(&spans)
}
pub fn with_hidden(self, hidden: HashSet<Vec<u8>>) -> Self {
ImportPattern {
head: self.head,
members: self.members,
hidden,
}
}
}
impl Default for ImportPattern {
fn default() -> Self {
Self::new()
}
}