nushell/src/context.rs

174 lines
4.3 KiB
Rust
Raw Normal View History

2019-08-06 18:26:33 +02:00
use crate::commands::{Command, UnevaluatedCallInfo};
2019-08-09 06:51:21 +02:00
use crate::parser::hir;
2019-05-15 18:12:38 +02:00
use crate::prelude::*;
2019-05-16 02:21:46 +02:00
2019-07-24 00:22:11 +02:00
use derive_new::new;
2019-05-26 08:54:41 +02:00
use indexmap::IndexMap;
2019-07-24 00:22:11 +02:00
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
2019-05-15 18:12:38 +02:00
use std::error::Error;
2019-05-22 09:12:03 +02:00
use std::sync::Arc;
2019-07-24 00:22:11 +02:00
use uuid::Uuid;
2019-05-15 18:12:38 +02:00
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SpanSource {
Url(String),
File(String),
2019-07-24 00:22:11 +02:00
Source(Text),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SourceMap(HashMap<Uuid, SpanSource>);
impl SourceMap {
pub fn insert(&mut self, uuid: Uuid, span_source: SpanSource) {
self.0.insert(uuid, span_source);
}
pub fn get(&self, uuid: &Uuid) -> Option<&SpanSource> {
self.0.get(uuid)
}
pub fn new() -> SourceMap {
SourceMap(HashMap::new())
}
}
2019-07-24 00:22:11 +02:00
#[derive(Clone, new)]
pub struct CommandRegistry {
#[new(value = "Arc::new(Mutex::new(IndexMap::default()))")]
2019-08-02 21:15:07 +02:00
registry: Arc<Mutex<IndexMap<String, Arc<Command>>>>,
2019-07-24 00:22:11 +02:00
}
impl CommandRegistry {
crate fn empty() -> CommandRegistry {
CommandRegistry {
registry: Arc::new(Mutex::new(IndexMap::default())),
}
}
2019-08-03 04:17:28 +02:00
crate fn get_command(&self, name: &str) -> Option<Arc<Command>> {
2019-07-24 00:22:11 +02:00
let registry = self.registry.lock().unwrap();
registry.get(name).map(|c| c.clone())
}
fn has(&self, name: &str) -> bool {
let registry = self.registry.lock().unwrap();
registry.contains_key(name)
}
2019-08-02 21:15:07 +02:00
fn insert(&mut self, name: impl Into<String>, command: Arc<Command>) {
2019-07-24 00:22:11 +02:00
let mut registry = self.registry.lock().unwrap();
registry.insert(name.into(), command);
}
crate fn names(&self) -> Vec<String> {
2019-07-24 06:10:48 +02:00
let registry = self.registry.lock().unwrap();
2019-07-24 00:22:11 +02:00
registry.keys().cloned().collect()
}
}
2019-06-07 08:34:42 +02:00
#[derive(Clone)]
2019-05-15 18:12:38 +02:00
pub struct Context {
2019-07-24 00:22:11 +02:00
registry: CommandRegistry,
crate source_map: SourceMap,
2019-05-24 06:34:43 +02:00
crate host: Arc<Mutex<dyn Host + Send>>,
2019-08-07 19:49:11 +02:00
crate shell_manager: ShellManager,
2019-05-15 18:12:38 +02:00
}
impl Context {
2019-07-24 00:22:11 +02:00
crate fn registry(&self) -> &CommandRegistry {
&self.registry
}
crate fn basic() -> Result<Context, Box<dyn Error>> {
2019-05-15 18:12:38 +02:00
Ok(Context {
2019-07-24 00:22:11 +02:00
registry: CommandRegistry::new(),
source_map: SourceMap::new(),
host: Arc::new(Mutex::new(crate::env::host::BasicHost)),
2019-08-07 19:49:11 +02:00
shell_manager: ShellManager::basic()?,
2019-05-15 18:12:38 +02:00
})
}
2019-08-02 21:15:07 +02:00
pub fn add_commands(&mut self, commands: Vec<Arc<Command>>) {
2019-05-28 08:45:18 +02:00
for command in commands {
2019-07-24 00:22:11 +02:00
self.registry.insert(command.name().to_string(), command);
2019-05-15 18:12:38 +02:00
}
}
pub fn add_span_source(&mut self, uuid: Uuid, span_source: SpanSource) {
self.source_map.insert(uuid, span_source);
}
2019-07-24 00:22:11 +02:00
pub fn clone_commands(&self) -> CommandRegistry {
self.registry.clone()
2019-05-26 08:54:41 +02:00
}
2019-05-22 09:12:03 +02:00
crate fn has_command(&self, name: &str) -> bool {
2019-07-24 00:22:11 +02:00
self.registry.has(name)
2019-05-15 18:12:38 +02:00
}
2019-08-02 21:15:07 +02:00
crate fn get_command(&self, name: &str) -> Arc<Command> {
2019-07-24 00:22:11 +02:00
self.registry.get_command(name).unwrap()
2019-05-22 09:12:03 +02:00
}
2019-08-02 21:15:07 +02:00
crate async fn run_command(
2019-05-22 09:12:03 +02:00
&mut self,
2019-08-02 21:15:07 +02:00
command: Arc<Command>,
name_span: Span,
source_map: SourceMap,
2019-07-24 00:22:11 +02:00
args: hir::Call,
source: Text,
input: InputStream,
) -> Result<OutputStream, ShellError> {
2019-07-24 00:22:11 +02:00
let command_args = self.command_args(args, input, source, source_map, name_span);
2019-08-09 06:51:21 +02:00
/*
2019-05-16 02:21:46 +02:00
let command_args = CommandArgs {
host: self.host.clone(),
2019-08-07 19:49:11 +02:00
shell_manager: self.shell_manager.clone(),
call_info: CallInfo {
name_span,
source_map,
args,
},
2019-05-16 02:21:46 +02:00
input,
};
2019-08-09 06:51:21 +02:00
*/
2019-05-16 02:21:46 +02:00
2019-08-02 21:15:07 +02:00
command.run(command_args, self.registry()).await
2019-05-15 18:12:38 +02:00
}
2019-06-22 05:43:37 +02:00
2019-07-24 00:22:11 +02:00
fn call_info(
&self,
args: hir::Call,
source: Text,
source_map: SourceMap,
2019-08-09 06:51:21 +02:00
name_span: Span,
2019-07-24 00:22:11 +02:00
) -> UnevaluatedCallInfo {
UnevaluatedCallInfo {
args,
source,
source_map,
name_span,
}
}
fn command_args(
&self,
args: hir::Call,
input: InputStream,
source: Text,
source_map: SourceMap,
2019-08-09 06:51:21 +02:00
name_span: Span,
2019-07-24 00:22:11 +02:00
) -> CommandArgs {
CommandArgs {
host: self.host.clone(),
2019-08-09 06:51:21 +02:00
shell_manager: self.shell_manager.clone(),
2019-07-24 00:22:11 +02:00
call_info: self.call_info(args, source, source_map, name_span),
input,
}
2019-06-22 05:43:37 +02:00
}
}