Add more helper functions

This commit is contained in:
JT
2021-10-20 18:58:25 +13:00
parent 11070ffbbe
commit b322a12f58
12 changed files with 98 additions and 121 deletions

View File

@ -55,26 +55,17 @@ impl Command for SubCommand {
Example {
description: "Convert string to integer",
example: "'2' | into int",
result: Some(Value::Int {
val: 2,
span: Span::unknown(),
}),
result: Some(Value::test_int(2)),
},
Example {
description: "Convert decimal to integer",
example: "5.9 | into int",
result: Some(Value::Int {
val: 5,
span: Span::unknown(),
}),
result: Some(Value::test_int(5)),
},
Example {
description: "Convert decimal string to integer",
example: "'5.9' | into int",
result: Some(Value::Int {
val: 5,
span: Span::unknown(),
}),
result: Some(Value::test_int(5)),
},
Example {
description: "Convert file size to integer",
@ -88,18 +79,9 @@ impl Command for SubCommand {
description: "Convert bool to integer",
example: "[$false, $true] | into int",
result: Some(Value::Stream {
stream: vec![
Value::Int {
val: 0,
span: Span::unknown(),
},
Value::Int {
val: 1,
span: Span::unknown(),
},
]
.into_iter()
.into_value_stream(),
stream: vec![Value::test_int(0), Value::test_int(1)]
.into_iter()
.into_value_stream(),
span: Span::unknown(),
}),
},

View File

@ -34,7 +34,10 @@ impl Command for Git {
Ok(val) => {
let result = val.stdout;
Ok(Value::string(&String::from_utf8_lossy(&result), call.head))
Ok(Value::String {
val: String::from_utf8_lossy(&result).to_string(),
span: call.head,
})
}
Err(_err) => {
// FIXME: Move this to an external signature and add better error handling

View File

@ -49,7 +49,10 @@ impl Command for GitCheckout {
Ok(val) => {
let result = val.stdout;
Ok(Value::string(&String::from_utf8_lossy(&result), call.head))
Ok(Value::String {
val: String::from_utf8_lossy(&result).to_string(),
span: call.head,
})
}
Err(_err) => {
// FIXME: Move this to an external signature and add better error handling

View File

@ -42,7 +42,7 @@ impl Command for Lines {
let iter = lines.into_iter().filter_map(move |s| {
if !s.is_empty() {
Some(Value::String { val: s, span })
Some(Value::string(s, span))
} else {
None
}

View File

@ -25,26 +25,11 @@ impl Command for SubCommand {
example: "'hello' | split chars",
result: Some(Value::List {
vals: vec![
Value::String {
val: "h".into(),
span: Span::unknown(),
},
Value::String {
val: "e".into(),
span: Span::unknown(),
},
Value::String {
val: "l".into(),
span: Span::unknown(),
},
Value::String {
val: "l".into(),
span: Span::unknown(),
},
Value::String {
val: "o".into(),
span: Span::unknown(),
},
Value::test_string("h"),
Value::test_string("e"),
Value::test_string("l"),
Value::test_string("l"),
Value::test_string("o"),
],
span: Span::unknown(),
}),
@ -74,10 +59,7 @@ fn split_chars_helper(v: &Value, name: Span) -> Vec<Value> {
s.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.map(move |x| Value::string(x, v_span))
.collect()
} else {
vec![Value::Error {

View File

@ -86,18 +86,12 @@ fn split_column_helper(
for (&k, v) in split_result.iter().zip(&gen_columns) {
cols.push(v.to_string());
vals.push(Value::String {
val: k.into(),
span: head,
});
vals.push(Value::string(k, head));
}
} else {
for (&k, v) in split_result.iter().zip(&positional) {
cols.push(v.into());
vals.push(Value::String {
val: k.into(),
span: head,
})
vals.push(Value::string(k, head));
}
}
Value::List {

View File

@ -55,10 +55,7 @@ fn split_row_helper(v: &Value, separator: &Spanned<String>, name: Span) -> Vec<V
s.split(&splitter)
.filter_map(|s| {
if s.trim() != "" {
Some(Value::String {
val: s.into(),
span: v_span,
})
Some(Value::string(s, v_span))
} else {
None
}