Clean up lint errors

This commit is contained in:
Yehuda Katz
2019-07-23 21:10:48 -07:00
parent 5a8e041a48
commit 73deeb69db
24 changed files with 253 additions and 124 deletions

View File

@ -42,7 +42,7 @@ crate mod trim;
crate mod vtable;
crate mod where_;
crate use command::{command, filter, EvaluatedFilterCommandArgs, EvaluatedStaticCommandArgs};
crate use command::{command, EvaluatedStaticCommandArgs};
crate use config::Config;
crate use open::Open;
crate use rm::Remove;

View File

@ -27,18 +27,6 @@ impl ToDebug for UnevaluatedCallInfo {
}
impl UnevaluatedCallInfo {
fn name(&self) -> Result<&str, ShellError> {
let head = &self.args.head();
match head.item() {
hir::RawExpression::Literal(hir::Literal::Bare) => Ok(head.span.slice(&self.source)),
hir::RawExpression::Literal(hir::Literal::String(span)) => Ok(span.slice(&self.source)),
other => Err(ShellError::type_error(
"Command name",
head.type_name().spanned(head.span),
)),
}
}
fn evaluate(
self,
registry: &registry::CommandRegistry,
@ -94,20 +82,6 @@ impl CommandArgs {
}
}
pub enum EvaluatedInput {
Static(InputStream),
Filter(Spanned<Value>),
}
impl EvaluatedInput {
pub fn stream(self) -> InputStream {
match self {
EvaluatedInput::Static(stream) => stream,
EvaluatedInput::Filter(value) => vec![value].into(),
}
}
}
pub struct EvaluatedStaticCommandArgs {
pub args: EvaluatedCommandArgs,
pub input: InputStream,
@ -152,6 +126,7 @@ impl EvaluatedStaticCommandArgs {
#[get = "pub"]
pub struct EvaluatedFilterCommandArgs {
args: EvaluatedCommandArgs,
#[allow(unused)]
input: Spanned<Value>,
}
@ -189,8 +164,6 @@ pub struct EvaluatedCommandArgs {
}
impl EvaluatedCommandArgs {
pub fn parts(self) -> () {}
pub fn call_args(&self) -> &registry::EvaluatedArgs {
&self.call_info.args
}
@ -199,10 +172,6 @@ impl EvaluatedCommandArgs {
self.call_info.args.nth(pos)
}
pub fn positional_iter(&self) -> impl Iterator<Item = &Spanned<Value>> {
self.call_info.args.positional_iter()
}
pub fn expect_nth(&self, pos: usize) -> Result<&Spanned<Value>, ShellError> {
self.call_info.args.expect_nth(pos)
}
@ -308,6 +277,7 @@ pub trait Sink {
}
}
#[allow(unused)]
pub struct FnFilterCommand {
name: String,
func: fn(EvaluatedFilterCommandArgs) -> Result<OutputStream, ShellError>,
@ -397,6 +367,7 @@ pub fn command(
})
}
#[allow(unused)]
pub fn filter(
name: &str,
func: fn(EvaluatedFilterCommandArgs) -> Result<OutputStream, ShellError>,

View File

@ -2,6 +2,6 @@ use crate::commands::command::CommandAction;
use crate::errors::ShellError;
use crate::prelude::*;
pub fn exit(_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
pub fn exit(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::Exit))].into())
}

View File

@ -50,7 +50,7 @@ pub fn from_yaml_string_to_value(
pub fn from_yaml(
args: CommandArgs,
registry: &CommandRegistry,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let span = args.name_span();
let out = args.input;

View File

@ -1,11 +1,10 @@
use crate::context::SpanSource;
use crate::errors::ShellError;
use crate::object::{Primitive, Switch, Value};
use crate::object::{Primitive, Value};
use crate::parser::hir::SyntaxType;
use crate::parser::parse::span::Span;
use crate::parser::registry::{self, CommandConfig, NamedType, PositionalType};
use crate::parser::registry::{self, CommandConfig, NamedType};
use crate::prelude::*;
use indexmap::IndexMap;
use mime::Mime;
use std::path::{Path, PathBuf};
use std::str::FromStr;
@ -77,17 +76,10 @@ impl Command for Open {
}
fn config(&self) -> CommandConfig {
let mut named = IndexMap::default();
named.insert("raw".to_string(), NamedType::Switch);
CommandConfig {
name: self.name().to_string(),
positional: vec![PositionalType::mandatory("path", SyntaxType::Block)],
rest_positional: false,
named,
is_sink: true,
is_filter: false,
}
CommandConfig::new(self.name())
.required("path", SyntaxType::Block)
.named("raw", NamedType::Switch)
.sink()
}
}

View File

@ -3,7 +3,7 @@ use crate::object::process::process_dict;
use crate::prelude::*;
use sysinfo::{RefreshKind, SystemExt};
pub fn ps(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
pub fn ps(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let mut system = sysinfo::System::new_with_specifics(RefreshKind::new().with_processes());
system.refresh_processes();
let list = system.get_process_list();

View File

@ -2,7 +2,7 @@ use crate::errors::ShellError;
use crate::object::{SpannedDictBuilder, Value};
use crate::prelude::*;
pub fn size(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
pub fn size(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let input = args.input;
Ok(input
.values

View File

@ -6,7 +6,7 @@ use crate::prelude::*;
use sys_info::*;
use sysinfo::{ComponentExt, DiskExt, NetworkExt, RefreshKind, SystemExt};
pub fn sysinfo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
pub fn sysinfo(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let name_span = args.name_span();
let mut idx = SpannedDictBuilder::new(name_span);

View File

@ -1,7 +1,10 @@
use crate::object::Value;
use crate::prelude::*;
pub fn to_array(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
pub fn to_array(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let out = args.input.values.collect();
Ok(out

View File

@ -2,7 +2,7 @@ use crate::errors::ShellError;
use crate::object::Value;
use crate::prelude::*;
pub fn trim(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
pub fn trim(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let input = args.input;
Ok(input

View File

@ -1,6 +1,5 @@
use crate::errors::ShellError;
use crate::object::base as value;
use crate::object::{types, Value};
use crate::parser::hir::SyntaxType;
use crate::parser::registry::{self, CommandConfig, PositionalType};
use crate::prelude::*;

View File

@ -1,9 +1,5 @@
use crate::commands::command::{CallInfo, Sink, SinkCommandArgs, UnevaluatedCallInfo};
use crate::parser::{
hir,
registry::{self, CommandConfig},
Span,
};
use crate::parser::{hir, registry, Span};
use crate::prelude::*;
use derive_new::new;
@ -51,12 +47,6 @@ impl CommandRegistry {
}
}
fn get_config(&self, name: &str) -> Option<CommandConfig> {
let registry = self.registry.lock().unwrap();
registry.get(name).map(|c| c.config())
}
fn get_command(&self, name: &str) -> Option<Arc<dyn Command>> {
let registry = self.registry.lock().unwrap();
@ -75,7 +65,7 @@ impl CommandRegistry {
}
crate fn names(&self) -> Vec<String> {
let mut registry = self.registry.lock().unwrap();
let registry = self.registry.lock().unwrap();
registry.keys().cloned().collect()
}
}

View File

@ -24,6 +24,7 @@ mod plugin;
mod shell;
mod stream;
mod traits;
mod utils;
pub use crate::commands::command::{CallInfo, ReturnSuccess, ReturnValue};
pub use crate::context::SpanSource;
@ -31,6 +32,7 @@ pub use crate::env::host::BasicHost;
pub use crate::parser::parse::span::SpannedItem;
pub use crate::parser::Spanned;
pub use crate::plugin::{serve_plugin, Plugin};
pub use crate::utils::{AbsolutePath, RelativePath};
pub use cli::cli;
pub use errors::ShellError;
pub use object::base::{Primitive, Value};

View File

@ -6,6 +6,6 @@ crate mod into;
crate mod process;
crate mod types;
crate use base::{Block, Primitive, Switch, Value};
crate use base::{Primitive, Value};
crate use dict::{Dictionary, SpannedDictBuilder};
crate use files::dir_entry_dict;

View File

@ -266,6 +266,7 @@ pub enum Switch {
}
impl Switch {
#[allow(unused)]
pub fn is_present(&self) -> bool {
match self {
Switch::Present => true,

View File

@ -77,7 +77,7 @@ fn parse_command_tail(
trace_remaining("nodes", tail.clone(), source);
for (name, kind) in config.named() {
for (name, kind) in &config.named {
trace!(target: "nu::parse", "looking for {} : {:?}", name, kind);
match kind {
@ -115,7 +115,7 @@ fn parse_command_tail(
if tail.at_end() {
return Err(ShellError::argument_error(
config.name().clone(),
config.name.clone(),
ArgumentError::MissingValueForName(name.to_string()),
flag.span,
));
@ -139,14 +139,14 @@ fn parse_command_tail(
let mut positional = vec![];
for arg in config.positional() {
for arg in &config.positional {
trace!("Processing positional {:?}", arg);
match arg {
PositionalType::Mandatory(..) => {
if tail.len() == 0 {
return Err(ShellError::argument_error(
config.name().clone(),
config.name.clone(),
ArgumentError::MissingMandatoryPositional(arg.name().to_string()),
command_span,
));
@ -207,7 +207,7 @@ fn extract_mandatory(
match flag {
None => Err(ShellError::argument_error(
config.name().clone(),
config.name.clone(),
ArgumentError::MissingMandatoryFlag(name.to_string()),
span,
)),

View File

@ -4,7 +4,6 @@ use crate::evaluate::{evaluate_baseline_expr, Scope};
use crate::parser::{hir, hir::SyntaxType, parse_command, CallNode, Spanned};
use crate::prelude::*;
use derive_new::new;
use getset::Getters;
use indexmap::IndexMap;
use log::trace;
use serde::{Deserialize, Serialize};
@ -70,8 +69,7 @@ impl PositionalType {
}
}
#[derive(Debug, Getters, Serialize, Deserialize, Clone)]
#[get = "crate"]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CommandConfig {
pub name: String,
pub positional: Vec<PositionalType>,
@ -81,6 +79,44 @@ pub struct CommandConfig {
pub is_sink: bool,
}
impl CommandConfig {
pub fn new(name: impl Into<String>) -> CommandConfig {
CommandConfig {
name: name.into(),
positional: vec![],
rest_positional: false,
named: IndexMap::default(),
is_filter: false,
is_sink: false,
}
}
pub fn required(mut self, name: impl Into<String>, ty: impl Into<SyntaxType>) -> CommandConfig {
self.positional
.push(PositionalType::Mandatory(name.into(), ty.into()));
self
}
pub fn optional(mut self, name: impl Into<String>, ty: impl Into<SyntaxType>) -> CommandConfig {
self.positional
.push(PositionalType::Optional(name.into(), ty.into()));
self
}
pub fn named(mut self, name: impl Into<String>, ty: impl Into<NamedType>) -> CommandConfig {
self.named.insert(name.into(), ty.into());
self
}
pub fn sink(mut self) -> CommandConfig {
self.is_sink = true;
self
}
}
#[derive(Debug, Default, new, Serialize, Deserialize)]
pub struct EvaluatedArgs {
pub positional: Option<Vec<Spanned<Value>>>,

View File

@ -34,8 +34,7 @@ macro_rules! trace_stream {
crate use crate::cli::MaybeOwned;
crate use crate::commands::command::{
Command, CommandAction, CommandArgs, EvaluatedCommandArgs, ReturnSuccess, ReturnValue, Sink,
SinkCommandArgs,
Command, CommandAction, CommandArgs, ReturnSuccess, ReturnValue, Sink, SinkCommandArgs,
};
crate use crate::context::{CommandRegistry, Context};
crate use crate::env::host::handle_unexpected;

View File

@ -1,5 +1,4 @@
use crate::context::CommandRegistry;
use crate::prelude::*;
use derive_new::new;
use rustyline::completion::Completer;

62
src/utils.rs Normal file
View File

@ -0,0 +1,62 @@
use std::ops::Div;
use std::path::{Path, PathBuf};
pub struct AbsolutePath {
inner: PathBuf,
}
impl AbsolutePath {
pub fn new(path: impl AsRef<Path>) -> AbsolutePath {
let path = path.as_ref();
if path.is_absolute() {
AbsolutePath {
inner: path.to_path_buf(),
}
} else {
panic!("AbsolutePath::new must take an absolute path")
}
}
}
impl Div<&str> for AbsolutePath {
type Output = AbsolutePath;
fn div(self, rhs: &str) -> Self::Output {
AbsolutePath {
inner: self.inner.join(rhs),
}
}
}
impl AsRef<Path> for AbsolutePath {
fn as_ref(&self) -> &Path {
self.inner.as_path()
}
}
pub struct RelativePath {
inner: PathBuf,
}
impl RelativePath {
pub fn new(path: impl Into<PathBuf>) -> RelativePath {
let path = path.into();
if path.is_relative() {
RelativePath { inner: path }
} else {
panic!("RelativePath::new must take a relative path")
}
}
}
impl<T: AsRef<str>> Div<T> for RelativePath {
type Output = RelativePath;
fn div(self, rhs: T) -> Self::Output {
RelativePath {
inner: self.inner.join(rhs.as_ref()),
}
}
}