1
0

Compare commits

..

2 Commits

Author SHA1 Message Date
46e2057b1c Fix examples 2023-08-25 10:49:05 -04:00
3d7b24299a Implement replace 2023-08-25 10:42:19 -04:00
3 changed files with 36 additions and 8 deletions

View File

@ -120,7 +120,7 @@ Lists are sequential collections. They can be built by grouping values with pare
```dust
list = (true, 42, "Ok");
assert_eq(list.0, true);
assert_equal(list.0, true);
```
### Maps
@ -132,7 +132,7 @@ reminder.message = "Buy milk";
reminder.tags = ("groceries", "home");
json = to_json(reminder);
append_to_file(json, "info.txt");
append(json, "info.txt");
```
### Tables
@ -170,7 +170,7 @@ insert(
assert_eq(length(animals.all), 6);
by_name = sort_by(animals, "name");
sorted = sort(animals, "name");
```
### The Yield Operator
@ -187,7 +187,7 @@ This can be useful when working on the command line but to make a script easier
json = download("https://api.sampleapis.com/futurama/characters");
from_json(json)
-> select(input, "name");
-> get(input, 4)
-> input.4
```
### Functions
@ -201,7 +201,7 @@ say_hi = 'output "hi"';
add_one = 'input + 1';
say_hi();
assert_eq(add_one(3), 4);
assert_equal(add_one(3), 4);
```
This function simply passes the input to the shell's standard output.
@ -217,8 +217,8 @@ organize them.
math.add = 'input.0 + input.1';
math.subtract = 'input.0 - input.1';
assert_eq(math.add(2, 2), 4);
assert_eq(math.subtract(100, 1), 99);
assert_equal(math.add(2, 2), 4);
assert_equal(math.subtract(100, 1), 99);
```
### Time

View File

@ -73,6 +73,33 @@ impl Tool for String {
}
}
pub struct Replace;
impl Tool for Replace {
fn info(&self) -> ToolInfo<'static> {
ToolInfo {
identifier: "replace",
description: "Replace all occurences of a substring in a string.",
group: "collections",
inputs: vec![ValueType::ListExact(vec![
ValueType::String,
ValueType::String,
ValueType::String,
])],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let argument = self.check_type(argument)?.as_list()?;
let target = argument[0].as_string()?;
let to_remove = argument[1].as_string()?;
let replacement = argument[2].as_string()?;
let result = target.replace(to_remove, replacement);
Ok(Value::String(result))
}
}
pub struct Count;
impl Tool for Count {

View File

@ -50,13 +50,14 @@ pub mod time;
/// Master list of all tools.
///
/// This list is used to match identifiers with tools and to provide info to the shell.
pub const TOOL_LIST: [&'static dyn Tool; 49] = [
pub const TOOL_LIST: [&'static dyn Tool; 50] = [
&collections::Count,
&collections::CreateTable,
&collections::Insert,
&collections::Rows,
&collections::Select,
&collections::String,
&collections::Replace,
&collections::Transform,
&collections::Where,
&command::Bash,