nushell/src/commands/args.rs

95 lines
1.8 KiB
Rust
Raw Normal View History

2019-05-12 00:59:57 +02:00
use crate::object::Value;
use crate::ShellError;
2019-05-12 00:59:57 +02:00
use derive_new::new;
2019-05-12 05:14:16 +02:00
use std::cell::Cell;
2019-05-12 00:59:57 +02:00
use std::collections::VecDeque;
2019-05-11 10:08:21 +02:00
#[derive(Debug)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
Fatal,
}
#[derive(Debug)]
pub struct LogItem {
level: LogLevel,
value: Value,
}
2019-05-12 00:59:57 +02:00
#[derive(Debug, Default)]
pub struct ObjectStream<T> {
queue: VecDeque<T>,
2019-05-12 00:59:57 +02:00
}
impl<T> ObjectStream<T> {
crate fn empty() -> ObjectStream<T> {
ObjectStream {
queue: VecDeque::new(),
}
}
crate fn iter(&self) -> impl Iterator<Item = &T> {
self.queue.iter()
}
crate fn take(&mut self) -> Option<T> {
self.queue.pop_front()
}
crate fn add(&mut self, value: T) {
self.queue.push_back(value);
}
}
#[derive(new)]
2019-05-12 00:59:57 +02:00
pub struct Streams {
#[new(value = "ObjectStream::empty()")]
success: ObjectStream<Value>,
#[new(value = "ObjectStream::empty()")]
errors: ObjectStream<ShellError>,
#[new(value = "ObjectStream::empty()")]
log: ObjectStream<LogItem>,
2019-05-12 05:14:16 +02:00
}
impl std::fmt::Debug for Streams {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Streams")
}
}
impl Streams {
crate fn read(&mut self) -> Option<Value> {
self.success.take()
2019-05-12 05:14:16 +02:00
}
crate fn add(&mut self, value: Value) {
self.success.add(value);
2019-05-12 05:14:16 +02:00
}
// fn take_stream(&mut self, stream: &mut ObjectStream) -> ObjectStream {
// let mut new_stream = Cell::new(ObjectStream::default());
// new_stream.swap()
// std::mem::swap(stream, &mut new_stream);
// new_stream
// }
2019-05-12 00:59:57 +02:00
}
#[derive(Debug, new)]
2019-05-11 10:08:21 +02:00
pub struct Args {
2019-05-12 00:59:57 +02:00
argv: Vec<Value>,
2019-05-12 05:14:16 +02:00
#[new(value = "Streams::new()")]
2019-05-12 00:59:57 +02:00
streams: Streams,
}
impl Args {
crate fn first(&self) -> Option<&Value> {
self.argv.first()
}
2019-05-11 10:08:21 +02:00
}