mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Remove unwraps (#1153)
* Remove a batch of unwraps * finish another batch
This commit is contained in:
@ -254,7 +254,7 @@ pub struct ExpandTracer {
|
||||
|
||||
impl ExpandTracer {
|
||||
pub fn print(&self, source: Text) -> PrintTracer {
|
||||
let root = self.frame_stack.get(0).unwrap().to_tree_frame(&source);
|
||||
let root = self.frame_stack[0].to_tree_frame(&source);
|
||||
|
||||
PrintTracer { root, source }
|
||||
}
|
||||
|
@ -42,7 +42,11 @@ impl CompareOperator {
|
||||
|
||||
impl From<&str> for CompareOperator {
|
||||
fn from(input: &str) -> CompareOperator {
|
||||
CompareOperator::from_str(input).unwrap()
|
||||
if let Ok(output) = CompareOperator::from_str(input) {
|
||||
output
|
||||
} else {
|
||||
unreachable!("Internal error: CompareOperator from failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,7 +94,11 @@ impl EvaluationOperator {
|
||||
|
||||
impl From<&str> for EvaluationOperator {
|
||||
fn from(input: &str) -> EvaluationOperator {
|
||||
EvaluationOperator::from_str(input).unwrap()
|
||||
if let Ok(output) = EvaluationOperator::from_str(input) {
|
||||
output
|
||||
} else {
|
||||
unreachable!("Internal error: EvaluationOperator 'from' failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,13 +148,21 @@ macro_rules! primitive_decimal {
|
||||
$(
|
||||
impl From<$ty> for Number {
|
||||
fn from(decimal: $ty) -> Number {
|
||||
Number::Decimal(BigDecimal::$from(decimal).unwrap())
|
||||
if let Some(num) = BigDecimal::$from(decimal) {
|
||||
Number::Decimal(num)
|
||||
} else {
|
||||
unreachable!("Internal error: BigDecimal 'from' failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&$ty> for Number {
|
||||
fn from(decimal: &$ty) -> Number {
|
||||
Number::Decimal(BigDecimal::$from(*decimal).unwrap())
|
||||
if let Some(num) = BigDecimal::$from(*decimal) {
|
||||
Number::Decimal(num)
|
||||
} else {
|
||||
unreachable!("Internal error: BigDecimal 'from' failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
)*
|
||||
@ -909,11 +917,13 @@ pub fn module(input: NomSpan) -> IResult<NomSpan, TokenNode> {
|
||||
}
|
||||
|
||||
fn parse_int<T>(frag: &str, neg: Option<T>) -> i64 {
|
||||
let int = FromStr::from_str(frag).unwrap();
|
||||
|
||||
match neg {
|
||||
None => int,
|
||||
Some(_) => -int,
|
||||
if let Ok(int) = FromStr::from_str(frag) {
|
||||
match neg {
|
||||
None => int,
|
||||
Some(_) => -int,
|
||||
}
|
||||
} else {
|
||||
unreachable!("Internal error: parse_int failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -323,10 +323,13 @@ impl TokenTreeBuilder {
|
||||
|
||||
let mut input = input.into_iter();
|
||||
|
||||
let head = input.next().unwrap();
|
||||
let tail = input.collect();
|
||||
if let Some(head) = input.next() {
|
||||
let tail = input.collect();
|
||||
|
||||
CallNode::new(Box::new(head), tail).spanned(span.into())
|
||||
CallNode::new(Box::new(head), tail).spanned(span.into())
|
||||
} else {
|
||||
unreachable!("Internal error: spanned_call failed")
|
||||
}
|
||||
}
|
||||
|
||||
fn consume_delimiter(
|
||||
|
@ -88,9 +88,19 @@ impl RawNumber {
|
||||
|
||||
pub(crate) fn to_number(self, source: &Text) -> Number {
|
||||
match self {
|
||||
RawNumber::Int(tag) => Number::Int(BigInt::from_str(tag.slice(source)).unwrap()),
|
||||
RawNumber::Int(tag) => {
|
||||
if let Ok(int) = BigInt::from_str(tag.slice(source)) {
|
||||
Number::Int(int)
|
||||
} else {
|
||||
unreachable!("Internal error: to_number failed")
|
||||
}
|
||||
}
|
||||
RawNumber::Decimal(tag) => {
|
||||
Number::Decimal(BigDecimal::from_str(tag.slice(source)).unwrap())
|
||||
if let Ok(decimal) = BigDecimal::from_str(tag.slice(source)) {
|
||||
Number::Decimal(decimal)
|
||||
} else {
|
||||
unreachable!("Internal error: to_number failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -124,14 +124,14 @@ impl CallStub {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_parameter(&mut self, name: &str) -> &mut Self {
|
||||
pub fn with_parameter(&mut self, name: &str) -> Result<&mut Self, ShellError> {
|
||||
let fields: Vec<Value> = name
|
||||
.split('.')
|
||||
.map(|s| UntaggedValue::string(s.to_string()).into_value(Tag::unknown()))
|
||||
.collect();
|
||||
|
||||
self.positionals.push(value::column_path(&fields));
|
||||
self
|
||||
self.positionals.push(value::column_path(&fields)?);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn create(&self) -> CallInfo {
|
||||
@ -156,7 +156,11 @@ pub fn expect_return_value_at(
|
||||
};
|
||||
|
||||
if idx == at {
|
||||
return item.raw_value().unwrap();
|
||||
if let Some(value) = item.raw_value() {
|
||||
return value;
|
||||
} else {
|
||||
panic!("Internal error: could not get raw value in expect_return_value_at")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -168,6 +172,7 @@ pub fn expect_return_value_at(
|
||||
}
|
||||
|
||||
pub mod value {
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, TaggedDictBuilder, UntaggedValue, Value};
|
||||
use nu_source::Tag;
|
||||
use nu_value_ext::ValueExt;
|
||||
@ -199,10 +204,10 @@ pub mod value {
|
||||
UntaggedValue::table(list).into_untagged_value()
|
||||
}
|
||||
|
||||
pub fn column_path(paths: &[Value]) -> Value {
|
||||
UntaggedValue::Primitive(Primitive::ColumnPath(
|
||||
table(&paths.to_vec()).as_column_path().unwrap().item,
|
||||
pub fn column_path(paths: &[Value]) -> Result<Value, ShellError> {
|
||||
Ok(UntaggedValue::Primitive(Primitive::ColumnPath(
|
||||
table(&paths.to_vec()).as_column_path()?.item,
|
||||
))
|
||||
.into_untagged_value()
|
||||
.into_untagged_value())
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,11 @@ impl AbsoluteFile {
|
||||
}
|
||||
|
||||
pub fn dir(&self) -> AbsolutePath {
|
||||
AbsolutePath::new(self.inner.parent().unwrap())
|
||||
AbsolutePath::new(if let Some(parent) = self.inner.parent() {
|
||||
parent
|
||||
} else {
|
||||
unreachable!("Internal error: could not get parent in dir")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
mod integration {
|
||||
use crate::inc::{Action, SemVerAction};
|
||||
use crate::Inc;
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::test_helpers::value::{column_path, string};
|
||||
use nu_plugin::test_helpers::{plugin, CallStub};
|
||||
|
||||
@ -52,16 +53,21 @@ mod integration {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn picks_up_argument_for_field() {
|
||||
fn picks_up_argument_for_field() -> Result<(), ShellError> {
|
||||
plugin(&mut Inc::new())
|
||||
.args(CallStub::new().with_parameter("package.version").create())
|
||||
.args(CallStub::new().with_parameter("package.version")?.create())
|
||||
.setup(|plugin, _| {
|
||||
plugin.expect_field(column_path(&[string("package"), string("version")]))
|
||||
//FIXME: this will need to be updated
|
||||
if let Ok(column_path) = column_path(&[string("package"), string("version")]) {
|
||||
plugin.expect_field(column_path)
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
mod sem_ver {
|
||||
use crate::Inc;
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::test_helpers::value::{get_data, string, structured_sample_record};
|
||||
use nu_plugin::test_helpers::{expect_return_value_at, plugin, CallStub};
|
||||
|
||||
@ -70,12 +76,12 @@ mod integration {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn major_input_using_the_field_passed_as_parameter() {
|
||||
fn major_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
|
||||
let run = plugin(&mut Inc::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_long_flag("major")
|
||||
.with_parameter("version")
|
||||
.with_parameter("version")?
|
||||
.create(),
|
||||
)
|
||||
.input(cargo_sample_record("0.1.3"))
|
||||
@ -85,15 +91,16 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "version"), string("1.0.0"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minor_input_using_the_field_passed_as_parameter() {
|
||||
fn minor_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
|
||||
let run = plugin(&mut Inc::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_long_flag("minor")
|
||||
.with_parameter("version")
|
||||
.with_parameter("version")?
|
||||
.create(),
|
||||
)
|
||||
.input(cargo_sample_record("0.1.3"))
|
||||
@ -103,15 +110,16 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "version"), string("0.2.0"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn patch_input_using_the_field_passed_as_parameter() {
|
||||
fn patch_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
|
||||
let run = plugin(&mut Inc::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_long_flag("patch")
|
||||
.with_parameter("version")
|
||||
.with_parameter("version")?
|
||||
.create(),
|
||||
)
|
||||
.input(cargo_sample_record("0.1.3"))
|
||||
@ -121,6 +129,7 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "version"), string("0.1.4"));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
mod integration {
|
||||
use crate::strutils::{Action, ReplaceAction};
|
||||
use crate::Str;
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::test_helpers::value::{
|
||||
column_path, get_data, int, string, structured_sample_record, table,
|
||||
unstructured_sample_record,
|
||||
@ -83,16 +84,21 @@ mod integration {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn picks_up_argument_for_field() {
|
||||
fn picks_up_argument_for_field() -> Result<(), ShellError> {
|
||||
plugin(&mut Str::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_parameter("package.description")
|
||||
.with_parameter("package.description")?
|
||||
.create(),
|
||||
)
|
||||
.setup(|plugin, _| {
|
||||
plugin.expect_field(column_path(&[string("package"), string("description")]))
|
||||
//FIXME: this is possibly not correct
|
||||
if let Ok(column_path) = column_path(&[string("package"), string("description")]) {
|
||||
plugin.expect_field(column_path)
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -115,12 +121,12 @@ mod integration {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upcases_the_input_using_the_field_passed_as_parameter() {
|
||||
fn upcases_the_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
|
||||
let run = plugin(&mut Str::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_long_flag("upcase")
|
||||
.with_parameter("name")
|
||||
.with_parameter("name")?
|
||||
.create(),
|
||||
)
|
||||
.input(structured_sample_record("name", "jotandrehuda"))
|
||||
@ -130,15 +136,16 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "name"), string("JOTANDREHUDA"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn downcases_the_input_using_the_field_passed_as_parameter() {
|
||||
fn downcases_the_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
|
||||
let run = plugin(&mut Str::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_long_flag("downcase")
|
||||
.with_parameter("name")
|
||||
.with_parameter("name")?
|
||||
.create(),
|
||||
)
|
||||
.input(structured_sample_record("name", "JOTANDREHUDA"))
|
||||
@ -148,15 +155,17 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "name"), string("jotandrehuda"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_the_input_to_integer_using_the_field_passed_as_parameter() {
|
||||
fn converts_the_input_to_integer_using_the_field_passed_as_parameter() -> Result<(), ShellError>
|
||||
{
|
||||
let run = plugin(&mut Str::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_long_flag("to-int")
|
||||
.with_parameter("Nu_birthday")
|
||||
.with_parameter("Nu_birthday")?
|
||||
.create(),
|
||||
)
|
||||
.input(structured_sample_record("Nu_birthday", "10"))
|
||||
@ -166,14 +175,15 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "Nu_birthday"), int(10));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replaces_the_input_using_the_field_passed_as_parameter() {
|
||||
fn replaces_the_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
|
||||
let run = plugin(&mut Str::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_parameter("rustconf")
|
||||
.with_parameter("rustconf")?
|
||||
.with_named_parameter("replace", string("22nd August 2019"))
|
||||
.create(),
|
||||
)
|
||||
@ -184,14 +194,15 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "rustconf"), string("22nd August 2019"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_and_replaces_the_input_using_the_field_passed_as_parameter() {
|
||||
fn find_and_replaces_the_input_using_the_field_passed_as_parameter() -> Result<(), ShellError> {
|
||||
let run = plugin(&mut Str::new())
|
||||
.args(
|
||||
CallStub::new()
|
||||
.with_parameter("staff")
|
||||
.with_parameter("staff")?
|
||||
.with_named_parameter(
|
||||
"find-replace",
|
||||
table(&[string("kittens"), string("jotandrehuda")]),
|
||||
@ -205,6 +216,7 @@ mod integration {
|
||||
let actual = expect_return_value_at(run, 0);
|
||||
|
||||
assert_eq!(get_data(actual, "staff"), string("wyjotandrehuda"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Reference in New Issue
Block a user