From 5e3185107075136e1ce73db318d66719e9fb8d10 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 2 Jan 2020 18:24:41 +1300 Subject: [PATCH] A couple more (#1149) --- crates/nu-protocol/src/value/column_path.rs | 4 +-- crates/nu-source/src/pretty.rs | 6 ++-- crates/nu-value-ext/src/lib.rs | 35 ++++++++++++--------- src/commands/classified/internal.rs | 4 +-- src/commands/config.rs | 2 +- src/commands/edit.rs | 2 +- src/commands/format.rs | 12 +++++-- src/commands/save.rs | 2 +- src/commands/table.rs | 7 ++++- 9 files changed, 45 insertions(+), 29 deletions(-) diff --git a/crates/nu-protocol/src/value/column_path.rs b/crates/nu-protocol/src/value/column_path.rs index 46e7b8c107..990327218e 100644 --- a/crates/nu-protocol/src/value/column_path.rs +++ b/crates/nu-protocol/src/value/column_path.rs @@ -48,8 +48,8 @@ impl ColumnPath { self.members.iter() } - pub fn split_last(&self) -> (&PathMember, &[PathMember]) { - self.members.split_last().unwrap() + pub fn split_last(&self) -> Option<(&PathMember, &[PathMember])> { + self.members.split_last() } } diff --git a/crates/nu-source/src/pretty.rs b/crates/nu-source/src/pretty.rs index cf52199f0b..d7ea02e0cb 100644 --- a/crates/nu-source/src/pretty.rs +++ b/crates/nu-source/src/pretty.rs @@ -414,8 +414,7 @@ pub trait PrettyDebug { let doc = self.pretty_doc(); let mut buffer = termcolor::Buffer::no_color(); - doc.render_raw(width, &mut TermColored::new(&mut buffer)) - .unwrap(); + let _ = doc.render_raw(width, &mut TermColored::new(&mut buffer)); String::from_utf8_lossy(buffer.as_slice()).to_string() } @@ -424,8 +423,7 @@ pub trait PrettyDebug { let doc = self.pretty_doc(); let mut buffer = termcolor::Buffer::ansi(); - doc.render_raw(width, &mut TermColored::new(&mut buffer)) - .unwrap(); + let _ = doc.render_raw(width, &mut TermColored::new(&mut buffer)); String::from_utf8_lossy(buffer.as_slice()).to_string() } diff --git a/crates/nu-value-ext/src/lib.rs b/crates/nu-value-ext/src/lib.rs index a15bb4620c..c4f2f09d61 100644 --- a/crates/nu-value-ext/src/lib.rs +++ b/crates/nu-value-ext/src/lib.rs @@ -291,25 +291,30 @@ pub fn insert_data_at_column_path( split_path: &ColumnPath, new_value: Value, ) -> Result { - let (last, front) = split_path.split_last(); - let mut original = value.clone(); + if let Some((last, front)) = split_path.split_last() { + let mut original = value.clone(); - let mut current: &mut Value = &mut original; + let mut current: &mut Value = &mut original; - for member in front { - let type_name = current.spanned_type_name(); + for member in front { + let type_name = current.spanned_type_name(); - current = get_mut_data_by_member(current, &member).ok_or_else(|| { - ShellError::missing_property( - member.plain_string(std::usize::MAX).spanned(member.span), - type_name, - ) - })? + current = get_mut_data_by_member(current, &member).ok_or_else(|| { + ShellError::missing_property( + member.plain_string(std::usize::MAX).spanned(member.span), + type_name, + ) + })? + } + + insert_data_at_member(current, &last, new_value)?; + + Ok(original) + } else { + Err(ShellError::untagged_runtime_error( + "Internal error: could not split column-path correctly", + )) } - - insert_data_at_member(current, &last, new_value)?; - - Ok(original) } pub fn replace_data_at_column_path( diff --git a/src/commands/classified/internal.rs b/src/commands/classified/internal.rs index 2042707c41..b11ddb58f7 100644 --- a/src/commands/classified/internal.rs +++ b/src/commands/classified/internal.rs @@ -148,10 +148,10 @@ pub(crate) async fn run_internal_command( let doc = PrettyDebug::pretty_doc(&v); let mut buffer = termcolor::Buffer::ansi(); - doc.render_raw( + let _ = doc.render_raw( context.with_host(|host| host.width() - 5), &mut nu_source::TermColored::new(&mut buffer), - ).unwrap(); + ); let value = String::from_utf8_lossy(buffer.as_slice()); diff --git a/src/commands/config.rs b/src/commands/config.rs index eaa7dbab0b..dd9244b41a 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -155,7 +155,7 @@ pub fn config( if result.contains_key(&key) { result.swap_remove(&key); - config::write(&result, &configuration).unwrap(); + config::write(&result, &configuration)? } else { yield Err(ShellError::labeled_error( "Key does not exist in config", diff --git a/src/commands/edit.rs b/src/commands/edit.rs index 48d9f0669d..7d4afd0562 100644 --- a/src/commands/edit.rs +++ b/src/commands/edit.rs @@ -38,7 +38,7 @@ impl PerItemCommand for Edit { value: Value, ) -> Result { let value_tag = value.tag(); - let field = call_info.args.expect_nth(0)?.as_column_path().unwrap(); + let field = call_info.args.expect_nth(0)?.as_column_path()?; let replacement = call_info.args.expect_nth(1)?.tagged_unknown(); let stream = match value { diff --git a/src/commands/format.rs b/src/commands/format.rs index b121a59384..99b49d10cd 100644 --- a/src/commands/format.rs +++ b/src/commands/format.rs @@ -37,9 +37,17 @@ impl PerItemCommand for Format { value: Value, ) -> Result { //let value_tag = value.tag(); - let pattern = call_info.args.expect_nth(0)?.as_string().unwrap(); + let pattern = call_info.args.expect_nth(0)?; + let pattern_tag = pattern.tag.clone(); + let pattern = pattern.as_string()?; - let format_pattern = format(&pattern).unwrap(); + let format_pattern = format(&pattern).map_err(|_| { + ShellError::labeled_error( + "Could not create format pattern", + "could not create format pattern", + pattern_tag, + ) + })?; let commands = format_pattern.1; let output = if let Value { diff --git a/src/commands/save.rs b/src/commands/save.rs index 2285b433a7..1c1d06cae1 100644 --- a/src/commands/save.rs +++ b/src/commands/save.rs @@ -178,7 +178,7 @@ fn save( let content : Result, ShellError> = 'scope: loop { break if !save_raw { if let Some(extension) = full_path.extension() { - let command_name = format!("to-{}", extension.to_str().unwrap()); + let command_name = format!("to-{}", extension.to_string_lossy()); if let Some(converter) = registry.get_command(&command_name) { let new_args = RawCommandArgs { host, diff --git a/src/commands/table.rs b/src/commands/table.rs index 22f9082529..854967e245 100644 --- a/src/commands/table.rs +++ b/src/commands/table.rs @@ -39,7 +39,12 @@ fn table(args: CommandArgs, registry: &CommandRegistry) -> Result { - i.to_usize().unwrap() + if let Some(num) = i.to_usize() { + num + } else { + yield Err(ShellError::labeled_error("Expected a row number", "expected a row number", &args.args.call_info.name_tag)); + 0 + } } _ => { 0