Skip to content

Commit

Permalink
More ununwraps (#1152)
Browse files Browse the repository at this point in the history
* More ununwraps

* More ununwraps

* Update completer.rs

* Update completer.rs
  • Loading branch information
Jonathan Turner committed Jan 2, 2020
1 parent 3e3cb15 commit 339a2de
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 103 deletions.
16 changes: 8 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ fn load_plugin(path: &std::path::Path, context: &mut Context) -> Result<(), Shel
let name = params.name.clone();
let fname = fname.to_string();

if context.get_command(&name).is_some() {
if context.get_command(&name)?.is_some() {
trace!("plugin {:?} already loaded.", &name);
} else if params.is_filter {
context.add_commands(vec![whole_stream_command(PluginCommand::new(
name, fname, params,
))]);
context.add_commands(vec![whole_stream_command(
PluginCommand::new(name, fname, params),
)])?;
} else {
context.add_commands(vec![whole_stream_command(PluginSink::new(
name, fname, params,
))]);
))])?;
}
Ok(())
}
Expand Down Expand Up @@ -328,7 +328,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
whole_stream_command(FromXML),
whole_stream_command(FromYAML),
whole_stream_command(FromYML),
]);
])?;

cfg_if::cfg_if! {
if #[cfg(data_processing_primitives)] {
Expand All @@ -337,15 +337,15 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
whole_stream_command(EvaluateBy),
whole_stream_command(TSortBy),
whole_stream_command(MapMaxBy),
]);
])?;
}
}

#[cfg(feature = "clipboard")]
{
context.add_commands(vec![whole_stream_command(
crate::commands::clip::clipboard::Clip,
)]);
)])?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/commands/autoview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn autoview(
}
}
};
if let Some(table) = table {
if let Some(table) = table? {
let mut new_output_stream: OutputStream = stream.to_output_stream();
let mut finished = false;
let mut current_idx = 0;
Expand Down Expand Up @@ -136,7 +136,7 @@ pub fn autoview(
value: UntaggedValue::Primitive(Primitive::String(ref s)),
tag: Tag { anchor, span },
} if anchor.is_some() => {
if let Some(text) = text {
if let Some(text) = text? {
let mut stream = VecDeque::new();
stream.push_back(UntaggedValue::string(s).into_value(Tag { anchor, span }));
let result = text.run(raw.with_input(stream.into()), &context.commands);
Expand All @@ -155,7 +155,7 @@ pub fn autoview(
value: UntaggedValue::Primitive(Primitive::Line(ref s)),
tag: Tag { anchor, span },
} if anchor.is_some() => {
if let Some(text) = text {
if let Some(text) = text? {
let mut stream = VecDeque::new();
stream.push_back(UntaggedValue::string(s).into_value(Tag { anchor, span }));
let result = text.run(raw.with_input(stream.into()), &context.commands);
Expand Down Expand Up @@ -190,7 +190,7 @@ pub fn autoview(
}

Value { value: UntaggedValue::Primitive(Primitive::Binary(ref b)), .. } => {
if let Some(binary) = binary {
if let Some(binary) = binary? {
let mut stream = VecDeque::new();
stream.push_back(x);
let result = binary.run(raw.with_input(stream.into()), &context.commands);
Expand All @@ -205,7 +205,7 @@ pub fn autoview(
yield Err(e);
}
Value { value: ref item, .. } => {
if let Some(table) = table {
if let Some(table) = table? {
let mut stream = VecDeque::new();
stream.push_back(x);
let result = table.run(raw.with_input(stream.into()), &context.commands);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/classified/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) async fn run_internal_command(
let contents_tag = tagged_contents.tag.clone();
let command_name = format!("from-{}", extension);
let command = command.clone();
if let Some(converter) = context.registry.get_command(&command_name) {
if let Some(converter) = context.registry.get_command(&command_name)? {
let new_args = RawCommandArgs {
host: context.host.clone(),
ctrl_c: context.ctrl_c.clone(),
Expand Down Expand Up @@ -103,7 +103,7 @@ pub(crate) async fn run_internal_command(
HelpShell::for_command(
UntaggedValue::string(cmd).into_value(tag),
&context.registry(),
).unwrap(),
)?,
));
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub struct RunnableContext {
}

impl RunnableContext {
pub fn get_command(&self, name: &str) -> Option<Arc<Command>> {
pub fn get_command(&self, name: &str) -> Result<Option<Arc<Command>>, ShellError> {
self.commands.get_command(name)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/enter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl PerItemCommand for Enter {

let (_, command) = (spec[0], spec[1]);

if registry.has(command) {
if registry.has(command)? {
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::EnterHelpShell(
UntaggedValue::string(command).into_value(Tag::unknown()),
)))]
Expand Down Expand Up @@ -88,7 +88,7 @@ impl PerItemCommand for Enter {
if let Some(extension) = file_extension {
let command_name = format!("from-{}", extension);
if let Some(converter) =
registry.get_command(&command_name)
registry.get_command(&command_name)?
{
let new_args = RawCommandArgs {
host: raw_args.host,
Expand Down
48 changes: 31 additions & 17 deletions src/commands/from_ssv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,30 +391,34 @@ mod tests {
}

#[test]
fn it_trims_remaining_separator_space() {
fn it_trims_remaining_separator_space() -> Result<(), ShellError> {
let input = r#"
colA colB colC
val1 val2 val3
"#;

let trimmed = |s: &str| s.trim() == s;

let result = string_to_table(input, false, true, 2).unwrap();
let result = string_to_table(input, false, true, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
assert!(result
.iter()
.all(|row| row.iter().all(|(a, b)| trimmed(a) && trimmed(b))))
.all(|row| row.iter().all(|(a, b)| trimmed(a) && trimmed(b))));

Ok(())
}

#[test]
fn it_keeps_empty_columns() {
fn it_keeps_empty_columns() -> Result<(), ShellError> {
let input = r#"
colA col B col C
val2 val3
val4 val 5 val 6
val7 val8
"#;

let result = string_to_table(input, false, true, 2).unwrap();
let result = string_to_table(input, false, true, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
assert_eq!(
result,
vec![
Expand All @@ -434,35 +438,39 @@ mod tests {
owned("col C", "val8")
],
]
)
);
Ok(())
}

#[test]
fn it_uses_the_full_final_column() {
fn it_uses_the_full_final_column() -> Result<(), ShellError> {
let input = r#"
colA col B
val1 val2 trailing value that should be included
"#;

let result = string_to_table(input, false, true, 2).unwrap();
let result = string_to_table(input, false, true, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
assert_eq!(
result,
vec![vec![
owned("colA", "val1"),
owned("col B", "val2 trailing value that should be included"),
],]
)
);
Ok(())
}

#[test]
fn it_handles_empty_values_when_headerless_and_aligned_columns() {
fn it_handles_empty_values_when_headerless_and_aligned_columns() -> Result<(), ShellError> {
let input = r#"
a multi-word value b d
1 3-3 4
last
"#;

let result = string_to_table(input, true, true, 2).unwrap();
let result = string_to_table(input, true, true, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
assert_eq!(
result,
vec![
Expand All @@ -488,22 +496,28 @@ mod tests {
owned("Column5", "last")
],
]
)
);
Ok(())
}

#[test]
fn input_is_parsed_correctly_if_either_option_works() {
fn input_is_parsed_correctly_if_either_option_works() -> Result<(), ShellError> {
let input = r#"
docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP
kubernetes component=apiserver,provider=kubernetes <none> 172.30.0.2 443/TCP
kubernetes-ro component=apiserver,provider=kubernetes <none> 172.30.0.1 80/TCP
"#;

let aligned_columns_headerless = string_to_table(input, true, true, 2).unwrap();
let separator_headerless = string_to_table(input, true, false, 2).unwrap();
let aligned_columns_with_headers = string_to_table(input, false, true, 2).unwrap();
let separator_with_headers = string_to_table(input, false, false, 2).unwrap();
let aligned_columns_headerless = string_to_table(input, true, true, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
let separator_headerless = string_to_table(input, true, false, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
let aligned_columns_with_headers = string_to_table(input, false, true, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
let separator_with_headers = string_to_table(input, false, false, 2)
.ok_or_else(|| ShellError::unexpected("table couldn't be parsed"))?;
assert_eq!(aligned_columns_headerless, separator_headerless);
assert_eq!(aligned_columns_with_headers, separator_with_headers);
Ok(())
}
}
6 changes: 3 additions & 3 deletions src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ impl PerItemCommand for Help {
}) => {
let mut help = VecDeque::new();
if document == "commands" {
let mut sorted_names = registry.names();
let mut sorted_names = registry.names()?;
sorted_names.sort();
for cmd in sorted_names {
let mut short_desc = TaggedDictBuilder::new(tag.clone());
let value = command_dict(
registry.get_command(&cmd).ok_or_else(|| {
registry.get_command(&cmd)?.ok_or_else(|| {
ShellError::labeled_error(
format!("Could not load {}", cmd),
"could not load command",
Expand All @@ -71,7 +71,7 @@ impl PerItemCommand for Help {

help.push_back(ReturnSuccess::value(short_desc.into_value()));
}
} else if let Some(command) = registry.get_command(document) {
} else if let Some(command) = registry.get_command(document)? {
let mut long_desc = String::new();

long_desc.push_str(&command.usage());
Expand Down

0 comments on commit 339a2de

Please sign in to comment.