2019-07-20 04:27:10 +02:00
|
|
|
use crate::commands::command::{CallInfo, Sink, SinkCommandArgs};
|
2019-08-01 03:58:42 +02:00
|
|
|
use crate::parser::registry::{Args, CommandConfig, CommandRegistry};
|
2019-05-15 18:12:38 +02:00
|
|
|
use crate::prelude::*;
|
2019-07-19 21:48:14 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use uuid::Uuid;
|
2019-05-16 02:21:46 +02:00
|
|
|
|
2019-05-26 08:54:41 +02:00
|
|
|
use indexmap::IndexMap;
|
2019-07-19 21:48:14 +02:00
|
|
|
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-05-15 18:12:38 +02:00
|
|
|
|
2019-07-19 21:48:14 +02:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum SpanSource {
|
|
|
|
Url(String),
|
|
|
|
File(String),
|
|
|
|
}
|
2019-07-20 03:12:04 +02:00
|
|
|
|
|
|
|
#[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-06-07 08:34:42 +02:00
|
|
|
#[derive(Clone)]
|
2019-05-15 18:12:38 +02:00
|
|
|
pub struct Context {
|
2019-05-26 08:54:41 +02:00
|
|
|
commands: IndexMap<String, Arc<dyn Command>>,
|
2019-06-07 08:34:42 +02:00
|
|
|
sinks: IndexMap<String, Arc<dyn Sink>>,
|
2019-07-20 03:12:04 +02:00
|
|
|
crate source_map: SourceMap,
|
2019-05-24 06:34:43 +02:00
|
|
|
crate host: Arc<Mutex<dyn Host + Send>>,
|
2019-07-16 21:10:25 +02:00
|
|
|
crate env: Arc<Mutex<Environment>>,
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
2019-06-03 05:48:58 +02:00
|
|
|
crate fn basic() -> Result<Context, Box<dyn Error>> {
|
2019-05-15 18:12:38 +02:00
|
|
|
Ok(Context {
|
2019-05-16 00:58:44 +02:00
|
|
|
commands: indexmap::IndexMap::new(),
|
2019-06-07 08:34:42 +02:00
|
|
|
sinks: indexmap::IndexMap::new(),
|
2019-07-20 03:12:04 +02:00
|
|
|
source_map: SourceMap::new(),
|
2019-05-23 09:23:06 +02:00
|
|
|
host: Arc::new(Mutex::new(crate::env::host::BasicHost)),
|
2019-07-16 21:10:25 +02:00
|
|
|
env: Arc::new(Mutex::new(Environment::basic()?)),
|
2019-05-15 18:12:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-28 08:45:18 +02:00
|
|
|
pub fn add_commands(&mut self, commands: Vec<Arc<dyn Command>>) {
|
|
|
|
for command in commands {
|
|
|
|
self.commands.insert(command.name().to_string(), command);
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:34:42 +02:00
|
|
|
pub fn add_sinks(&mut self, sinks: Vec<Arc<dyn Sink>>) {
|
|
|
|
for sink in sinks {
|
|
|
|
self.sinks.insert(sink.name().to_string(), sink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:48:14 +02:00
|
|
|
pub fn add_span_source(&mut self, uuid: Uuid, span_source: SpanSource) {
|
2019-07-20 03:12:04 +02:00
|
|
|
self.source_map.insert(uuid, span_source);
|
2019-07-19 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 08:34:42 +02:00
|
|
|
crate fn has_sink(&self, name: &str) -> bool {
|
|
|
|
self.sinks.contains_key(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn get_sink(&self, name: &str) -> Arc<dyn Sink> {
|
|
|
|
self.sinks.get(name).unwrap().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn run_sink(
|
|
|
|
&mut self,
|
|
|
|
command: Arc<dyn Sink>,
|
2019-08-05 10:54:29 +02:00
|
|
|
name_span: Span,
|
2019-06-07 08:34:42 +02:00
|
|
|
args: Args,
|
2019-08-01 03:58:42 +02:00
|
|
|
input: Vec<Tagged<Value>>,
|
2019-06-07 08:34:42 +02:00
|
|
|
) -> Result<(), ShellError> {
|
|
|
|
let command_args = SinkCommandArgs {
|
|
|
|
ctx: self.clone(),
|
2019-07-20 04:27:10 +02:00
|
|
|
call_info: CallInfo {
|
|
|
|
name_span,
|
|
|
|
source_map: self.source_map.clone(),
|
|
|
|
args,
|
|
|
|
},
|
2019-06-07 08:34:42 +02:00
|
|
|
input,
|
|
|
|
};
|
|
|
|
|
|
|
|
command.run(command_args)
|
|
|
|
}
|
|
|
|
|
2019-05-26 08:54:41 +02:00
|
|
|
pub fn clone_commands(&self) -> indexmap::IndexMap<String, Arc<dyn Command>> {
|
|
|
|
self.commands.clone()
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
crate fn has_command(&self, name: &str) -> bool {
|
2019-05-15 18:12:38 +02:00
|
|
|
self.commands.contains_key(name)
|
|
|
|
}
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
crate fn get_command(&self, name: &str) -> Arc<dyn Command> {
|
|
|
|
self.commands.get(name).unwrap().clone()
|
|
|
|
}
|
|
|
|
|
2019-05-16 02:21:46 +02:00
|
|
|
crate fn run_command(
|
2019-05-22 09:12:03 +02:00
|
|
|
&mut self,
|
|
|
|
command: Arc<dyn Command>,
|
2019-08-05 10:54:29 +02:00
|
|
|
name_span: Span,
|
2019-07-20 03:12:04 +02:00
|
|
|
source_map: SourceMap,
|
2019-06-01 07:50:16 +02:00
|
|
|
args: Args,
|
2019-05-23 09:23:06 +02:00
|
|
|
input: InputStream,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-05-16 02:21:46 +02:00
|
|
|
let command_args = CommandArgs {
|
2019-05-23 09:23:06 +02:00
|
|
|
host: self.host.clone(),
|
|
|
|
env: self.env.clone(),
|
2019-07-20 04:27:10 +02:00
|
|
|
call_info: CallInfo {
|
|
|
|
name_span,
|
|
|
|
source_map,
|
|
|
|
args,
|
|
|
|
},
|
2019-05-16 02:21:46 +02:00
|
|
|
input,
|
|
|
|
};
|
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
command.run(command_args)
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 05:43:37 +02:00
|
|
|
|
|
|
|
impl CommandRegistry for Context {
|
|
|
|
fn get(&self, name: &str) -> Option<CommandConfig> {
|
|
|
|
self.commands.get(name).map(|c| c.config())
|
|
|
|
}
|
|
|
|
}
|