Remove dead code

This commit is contained in:
Yehuda Katz 2019-05-15 15:58:44 -07:00
parent a5a34b88a8
commit af1963d148
26 changed files with 77 additions and 231 deletions

View File

@ -8,5 +8,4 @@ crate mod select;
crate mod take;
crate mod to_array;
crate use command::Command;
crate use to_array::to_array;

View File

@ -1,9 +1,6 @@
use crate::object::Value;
use crate::ShellError;
use derive_new::new;
use std::cell::Cell;
use std::collections::VecDeque;
#[allow(unused)]
#[derive(Debug)]
pub enum LogLevel {
Trace,
@ -14,81 +11,9 @@ pub enum LogLevel {
Fatal,
}
#[allow(unused)]
#[derive(Debug)]
pub struct LogItem {
level: LogLevel,
value: Value,
}
#[derive(Debug, Default)]
pub struct ObjectStream<T> {
queue: VecDeque<T>,
}
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)]
pub struct Streams {
#[new(value = "ObjectStream::empty()")]
success: ObjectStream<Value>,
#[new(value = "ObjectStream::empty()")]
errors: ObjectStream<ShellError>,
#[new(value = "ObjectStream::empty()")]
log: ObjectStream<LogItem>,
}
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()
}
crate fn add(&mut self, value: Value) {
self.success.add(value);
}
// 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
// }
}
#[derive(Debug, new)]
pub struct Args {
argv: Vec<Value>,
#[new(value = "Streams::new()")]
streams: Streams,
}
impl Args {
crate fn first(&self) -> Option<&Value> {
self.argv.first()
}
}

View File

@ -1,11 +1,8 @@
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{dir_entry_dict, Value};
use crate::object::Value;
use crate::prelude::*;
use crate::Args;
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
use std::path::PathBuf;
#[derive(new)]
pub struct CdBlueprint;
@ -14,7 +11,7 @@ impl crate::CommandBlueprint for CdBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn Host,
_host: &dyn Host,
env: &mut Environment,
) -> Result<Box<dyn Command>, ShellError> {
let target = match args.first() {
@ -37,7 +34,7 @@ pub struct Cd {
}
impl crate::Command for Cd {
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
fn run(&mut self, _stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
let mut stream = VecDeque::new();
let path = dunce::canonicalize(self.cwd.join(&self.target).as_path())?;
stream.push_back(ReturnValue::change_cwd(path));

View File

@ -1,12 +1,9 @@
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{dir_entry_dict, Value};
use crate::prelude::*;
use crate::Args;
use crate::Command;
use derive_new::new;
use std::path::PathBuf;
use sysinfo::SystemExt;
#[derive(new)]
pub struct LsBlueprint;
@ -14,8 +11,8 @@ pub struct LsBlueprint;
impl crate::CommandBlueprint for LsBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn crate::Host,
_args: Vec<Value>,
_host: &dyn crate::Host,
env: &mut crate::Environment,
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(Ls {
@ -30,9 +27,9 @@ pub struct Ls {
}
impl crate::Command for Ls {
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
fn run(&mut self, _stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
let entries =
std::fs::read_dir(&self.cwd).map_err((|e| ShellError::string(format!("{:?}", e))))?;
std::fs::read_dir(&self.cwd).map_err(|e| ShellError::string(format!("{:?}", e)))?;
let mut shell_entries = VecDeque::new();

View File

@ -4,8 +4,6 @@ use crate::object::Value;
use crate::prelude::*;
use crate::Command;
use derive_new::new;
use std::cell::RefCell;
use std::rc::Rc;
use sysinfo::SystemExt;
#[derive(new)]
@ -14,9 +12,9 @@ pub struct PsBlueprint;
impl crate::CommandBlueprint for PsBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn crate::Host,
env: &mut crate::Environment,
_args: Vec<Value>,
_host: &dyn crate::Host,
_env: &mut crate::Environment,
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(Ps::new()))
}
@ -26,7 +24,7 @@ impl crate::CommandBlueprint for PsBlueprint {
pub struct Ps;
impl crate::Command for Ps {
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
fn run(&mut self, _stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
let mut system = sysinfo::System::new();
system.refresh_all();

View File

@ -1,12 +1,8 @@
use crate::errors::ShellError;
use crate::object::base::reject;
use crate::object::process::Process;
use crate::object::{dir_entry_dict, Value};
use crate::object::Value;
use crate::prelude::*;
use crate::Args;
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
#[derive(new)]
pub struct RejectBlueprint;
@ -15,8 +11,8 @@ impl crate::CommandBlueprint for RejectBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn Host,
env: &mut Environment,
_host: &dyn Host,
_env: &mut Environment,
) -> Result<Box<dyn Command>, ShellError> {
if args.is_empty() {
return Err(ShellError::string("take requires an integer"));

View File

@ -1,12 +1,8 @@
use crate::errors::ShellError;
use crate::object::base::select;
use crate::object::process::Process;
use crate::object::{dir_entry_dict, Value};
use crate::object::Value;
use crate::prelude::*;
use crate::Args;
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
#[derive(new)]
pub struct SelectBlueprint;
@ -15,8 +11,8 @@ impl crate::CommandBlueprint for SelectBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn Host,
env: &mut Environment,
_host: &dyn Host,
_env: &mut Environment,
) -> Result<Box<dyn Command>, ShellError> {
if args.is_empty() {
return Err(ShellError::string("take requires an integer"));

View File

@ -1,11 +1,7 @@
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{dir_entry_dict, Value};
use crate::object::Value;
use crate::prelude::*;
use crate::Args;
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
#[derive(new)]
pub struct TakeBlueprint;
@ -14,8 +10,8 @@ impl crate::CommandBlueprint for TakeBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn Host,
env: &mut Environment,
_host: &dyn Host,
_env: &mut Environment,
) -> Result<Box<dyn Command>, ShellError> {
if args.is_empty() {
return Err(ShellError::string("take requires an integer"));

View File

@ -1,11 +1,7 @@
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{dir_entry_dict, Value};
use crate::object::Value;
use crate::prelude::*;
use crate::Args;
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
#[derive(new)]
pub struct ToArrayBlueprint;
@ -13,9 +9,9 @@ pub struct ToArrayBlueprint;
impl crate::CommandBlueprint for ToArrayBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn Host,
env: &mut Environment,
_args: Vec<Value>,
_host: &dyn Host,
_env: &mut Environment,
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(ToArray))
}

View File

@ -1,11 +1,8 @@
use crate::prelude::*;
use std::collections::BTreeMap;
use std::error::Error;
pub type Commands = BTreeMap<String, Box<dyn crate::CommandBlueprint>>;
pub struct Context {
commands: BTreeMap<String, Box<dyn crate::CommandBlueprint>>,
commands: indexmap::IndexMap<String, Box<dyn crate::CommandBlueprint>>,
crate host: Box<dyn crate::Host>,
crate env: Environment,
}
@ -13,7 +10,7 @@ pub struct Context {
impl Context {
crate fn basic() -> Result<Context, Box<Error>> {
Ok(Context {
commands: BTreeMap::new(),
commands: indexmap::IndexMap::new(),
host: Box::new(crate::env::host::BasicHost),
env: crate::Environment::basic()?,
})
@ -25,10 +22,6 @@ impl Context {
}
}
crate fn get_command(&mut self, name: &str) -> Option<&dyn crate::CommandBlueprint> {
self.commands.get(name).map(|c| &**c)
}
crate fn has_command(&mut self, name: &str) -> bool {
self.commands.contains_key(name)
}

View File

@ -1,4 +1,6 @@
#[allow(unused)]
use crate::prelude::*;
use crate::Value;
use derive_new::new;

View File

@ -3,20 +3,26 @@ crate mod generic;
crate mod list;
crate mod table;
use crate::object::Value;
use crate::prelude::*;
use crate::Context;
use std::sync::{Arc, Mutex};
crate use entries::{EntriesListView, EntriesView};
crate use generic::GenericView;
crate use list::ListView;
crate use table::TableView;
crate trait RenderView {
fn render_view(&self, host: &dyn Host) -> Vec<String>;
}
crate fn print_rendered(lines: &[String], host: &mut dyn Host) {
fn print_rendered(lines: &[String], host: &mut dyn Host) {
for line in lines {
host.stdout(line);
}
}
crate fn print_view(view: &impl RenderView, context: Arc<Mutex<Context>>) {
let mut ctx = context.lock().unwrap();
crate::format::print_rendered(&view.render_view(&ctx.host), &mut ctx.host);
}

View File

@ -32,7 +32,7 @@ impl EntriesView {
}
impl RenderView for EntriesView {
fn render_view(&self, host: &dyn Host) -> Vec<String> {
fn render_view(&self, _host: &dyn Host) -> Vec<String> {
if self.entries.len() == 0 {
return vec![];
}

View File

@ -3,6 +3,7 @@ use crate::Host;
use derive_new::new;
// A list is printed one line at a time with an optional separator between groups
#[allow(unused)]
#[derive(new)]
pub struct ListView {
list: Vec<Vec<String>>,
@ -10,7 +11,7 @@ pub struct ListView {
}
impl RenderView for ListView {
fn render_view(&self, host: &dyn Host) -> Vec<String> {
fn render_view(&self, _host: &dyn Host) -> Vec<String> {
let mut out = vec![];
for output in &self.list {

View File

@ -43,7 +43,7 @@ impl TableView {
}
impl RenderView for TableView {
fn render_view(&self, host: &dyn Host) -> Vec<String> {
fn render_view(&self, _host: &dyn Host) -> Vec<String> {
if self.entries.len() == 0 {
return vec![];
}

View File

@ -1,6 +1,5 @@
#![feature(crate_visibility_modifier)]
#![feature(in_band_lifetimes)]
#![allow(unused)]
mod commands;
mod context;
@ -11,28 +10,21 @@ mod object;
mod parser;
mod prelude;
crate use crate::commands::args::{Args, Streams};
use crate::commands::command::ReturnValue;
crate use crate::commands::command::{Command, CommandAction, CommandBlueprint};
use crate::context::Context;
crate use crate::env::{Environment, Host};
crate use crate::errors::ShellError;
crate use crate::format::{EntriesListView, GenericView, RenderView};
crate use crate::format::{EntriesListView, GenericView};
use crate::object::Value;
use ansi_term::Color;
use conch_parser::lexer::Lexer;
use conch_parser::parse::DefaultParser;
use rustyline::error::ReadlineError;
use rustyline::Editor;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::collections::VecDeque;
use std::error::Error;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use subprocess::Exec;
use sysinfo::{self, SystemExt};
#[derive(Debug)]
pub enum MaybeOwned<'a, T> {
@ -55,7 +47,7 @@ fn main() -> Result<(), Box<Error>> {
println!("No previous history.");
}
let mut context = Arc::new(Mutex::new(Context::basic()?));
let context = Arc::new(Mutex::new(Context::basic()?));
{
use crate::commands::*;
@ -88,8 +80,6 @@ fn main() -> Result<(), Box<Error>> {
let mut input = VecDeque::new();
let last = parsed.len() - 1;
for item in parsed {
input = process_command(
crate::parser::print_items(&item),
@ -135,17 +125,14 @@ fn process_command(
let command = &parsed[0].name();
let arg_list = parsed[1..].iter().map(|i| i.as_value()).collect();
let streams = Streams::new();
if command == &"format" {
format(input, context);
Ok(VecDeque::new())
} else if command == &"format-list" {
let view = EntriesListView::from_stream(input);
let mut ctx = context.lock().unwrap();
crate::format::print_rendered(&view.render_view(&ctx.host), &mut ctx.host);
crate::format::print_view(&view, context.clone());
Ok(VecDeque::new())
} else {
@ -155,7 +142,7 @@ fn process_command(
true => {
let mut instance = ctx.create_command(command, arg_list)?;
let mut result = instance.run(input)?;
let result = instance.run(input)?;
let mut next = VecDeque::new();
for v in result {
@ -180,12 +167,10 @@ fn process_command(
}
fn format(input: VecDeque<Value>, context: Arc<Mutex<Context>>) {
let mut ctx = context.lock().unwrap();
let last = input.len() - 1;
for (i, item) in input.iter().enumerate() {
let view = GenericView::new(item);
crate::format::print_rendered(&view.render_view(&ctx.host), &mut ctx.host);
crate::format::print_view(&view, context.clone());
if last != i {
println!("");

View File

@ -6,6 +6,5 @@ crate mod process;
crate mod types;
crate use base::{Primitive, Value};
crate use desc::{DataDescriptor, DataDescriptorInstance};
crate use dict::Dictionary;
crate use files::dir_entry_dict;

View File

@ -1,16 +1,15 @@
use crate::errors::ShellError;
use crate::format::{EntriesView, GenericView};
use crate::object::desc::DataDescriptor;
use ansi_term::Color;
use chrono::{DateTime, Utc};
use chrono_humanize::Humanize;
use std::fmt::Debug;
use std::time::SystemTime;
#[derive(Debug, Clone)]
pub enum Primitive {
Nothing,
Int(i64),
#[allow(unused)]
Float(f64),
Bytes(u128),
String(String),
@ -43,7 +42,7 @@ impl Primitive {
(true, None) => format!("Yes"),
(false, None) => format!("No"),
(true, Some(s)) => format!("{}", s),
(false, Some(s)) => format!(""),
(false, Some(_)) => format!(""),
},
Primitive::Date(d) => format!("{}", d.humanize()),
}
@ -59,30 +58,21 @@ pub enum Value {
}
impl Value {
crate fn to_shell_string(&self) -> String {
match self {
Value::Primitive(p) => p.format(None),
Value::Object(o) => format!("[object Object]"),
Value::List(l) => format!("[list List]"),
Value::Error(e) => format!("{}", e),
}
}
crate fn data_descriptors(&self) -> Vec<DataDescriptor> {
match self {
Value::Primitive(p) => vec![],
Value::Primitive(_) => vec![],
Value::Object(o) => o.data_descriptors(),
Value::List(l) => vec![],
Value::Error(e) => vec![],
Value::List(_) => vec![],
Value::Error(_) => vec![],
}
}
crate fn get_data(&'a self, desc: &DataDescriptor) -> crate::MaybeOwned<'a, Value> {
match self {
Value::Primitive(p) => crate::MaybeOwned::Owned(Value::nothing()),
Value::Primitive(_) => crate::MaybeOwned::Owned(Value::nothing()),
Value::Object(o) => o.get_data(desc),
Value::List(l) => crate::MaybeOwned::Owned(Value::nothing()),
Value::Error(e) => crate::MaybeOwned::Owned(Value::nothing()),
Value::List(_) => crate::MaybeOwned::Owned(Value::nothing()),
Value::Error(_) => crate::MaybeOwned::Owned(Value::nothing()),
}
}
@ -101,8 +91,8 @@ impl Value {
crate fn format_leaf(&self, field_name: Option<&str>) -> String {
match self {
Value::Primitive(p) => p.format(field_name),
Value::Object(o) => format!("[object Object]"),
Value::List(l) => format!("[list List]"),
Value::Object(_) => format!("[object Object]"),
Value::List(_) => format!("[list List]"),
Value::Error(e) => format!("{}", e),
}
}
@ -142,6 +132,7 @@ impl Value {
Value::Primitive(Primitive::Int(s.into()))
}
#[allow(unused)]
crate fn system_date(s: SystemTime) -> Value {
Value::Primitive(Primitive::Date(s.into()))
}
@ -161,6 +152,7 @@ impl Value {
Value::Primitive(Primitive::Nothing)
}
#[allow(unused)]
crate fn list(values: impl Into<Vec<Value>>) -> Value {
Value::List(values.into())
}

View File

@ -1,4 +1,4 @@
use crate::object::types::{AnyShell, Type};
use crate::object::types::Type;
use derive_new::new;
#[derive(new)]
@ -14,18 +14,4 @@ impl PartialEq for DataDescriptor {
}
}
impl DataDescriptor {
crate fn any(name: impl Into<String>) -> DataDescriptor {
DataDescriptor {
name: name.into(),
readonly: true,
ty: Box::new(AnyShell),
}
}
}
#[derive(new)]
pub struct DataDescriptorInstance<'desc> {
desc: &'desc DataDescriptor,
value: crate::object::base::Value,
}
impl DataDescriptor {}

View File

@ -1,11 +1,10 @@
#[allow(unused)]
use crate::prelude::*;
use crate::object::desc::DataDescriptor;
use crate::object::{Primitive, Value};
use crate::prelude::*;
use crate::MaybeOwned;
use derive_new::new;
use indexmap::IndexMap;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Default)]
pub struct Dictionary {
@ -30,7 +29,7 @@ impl Dictionary {
crate fn data_descriptors(&self) -> Vec<DataDescriptor> {
self.entries
.iter()
.map(|(name, value)| {
.map(|(name, _)| {
DataDescriptor::new(name.clone(), true, Box::new(crate::object::types::AnyShell))
})
.collect()

View File

@ -1,11 +1,5 @@
use crate::errors::ShellError;
use crate::object::{DataDescriptor, Dictionary, Value};
use crate::MaybeOwned;
#[derive(Debug)]
pub struct DirEntry {
dict: Dictionary,
}
use crate::object::{Dictionary, Value};
#[derive(Debug)]
pub enum FileType {

View File

@ -1,16 +1,8 @@
use crate::object::base::{Primitive, Value};
use crate::object::desc::DataDescriptor;
use crate::object::base::Value;
use crate::object::dict::Dictionary;
use crate::MaybeOwned;
use derive_new::new;
use itertools::join;
use sysinfo::ProcessExt;
#[derive(Debug)]
pub struct Process {
dict: Dictionary,
}
crate fn process_dict(proc: &sysinfo::Process) -> Dictionary {
let mut dict = Dictionary::default();
dict.add("name", Value::string(proc.name()));

View File

@ -1,4 +1,4 @@
use std::any::{Any, TypeId};
use std::any::Any;
pub trait Type {
fn as_any(&self) -> &dyn Any;

View File

@ -1,6 +1,7 @@
use rustyline::{completion, Context};
use std::collections::BTreeMap;
#[allow(unused)]
crate struct Completer {
commands: BTreeMap<String, Box<dyn crate::CommandBlueprint>>,
}
@ -10,9 +11,9 @@ impl completion::Completer for Completer {
fn complete(
&self,
line: &str,
pos: usize,
ctx: &Context<'_>,
_line: &str,
_pos: usize,
_ctx: &Context<'_>,
) -> rustyline::Result<(usize, Vec<completion::Pair>)> {
let pairs = self
.commands

View File

@ -5,7 +5,7 @@ use nom::character::complete::one_of;
use nom::multi::separated_list;
use nom::sequence::{preceded, terminated};
use nom::IResult;
use nom::{complete, named, separated_list, ws};
use nom::{complete, named, ws};
use std::str::FromStr;
#[derive(Debug, Clone)]
@ -26,8 +26,6 @@ impl Item {
}
crate fn print_items(items: &[Item]) -> String {
let mut out = String::new();
let formatted = items.iter().map(|item| match item {
Item::Bare(s) => format!("{}", s),
Item::Quoted(s) => format!("{:?}", s),
@ -42,7 +40,7 @@ impl Item {
match self {
Item::Quoted(s) => s,
Item::Bare(s) => s,
Item::Int(i) => unimplemented!(),
Item::Int(_) => unimplemented!(),
}
}
}

View File

@ -1,7 +1,5 @@
crate use crate::commands::args::{Args, Streams};
crate use crate::commands::command::{Command, CommandAction, CommandBlueprint, ReturnValue};
crate use crate::commands::command::{Command, ReturnValue};
crate use crate::env::{Environment, Host};
crate use crate::errors::ShellError;
crate use crate::format::RenderView;
crate use crate::object::{Primitive, Value};
crate use std::collections::VecDeque;