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 ```dust
list = (true, 42, "Ok"); list = (true, 42, "Ok");
assert_eq(list.0, true); assert_equal(list.0, true);
``` ```
### Maps ### Maps
@ -132,7 +132,7 @@ reminder.message = "Buy milk";
reminder.tags = ("groceries", "home"); reminder.tags = ("groceries", "home");
json = to_json(reminder); json = to_json(reminder);
append_to_file(json, "info.txt"); append(json, "info.txt");
``` ```
### Tables ### Tables
@ -170,7 +170,7 @@ insert(
assert_eq(length(animals.all), 6); assert_eq(length(animals.all), 6);
by_name = sort_by(animals, "name"); sorted = sort(animals, "name");
``` ```
### The Yield Operator ### 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"); json = download("https://api.sampleapis.com/futurama/characters");
from_json(json) from_json(json)
-> select(input, "name"); -> select(input, "name");
-> get(input, 4) -> input.4
``` ```
### Functions ### Functions
@ -201,7 +201,7 @@ say_hi = 'output "hi"';
add_one = 'input + 1'; add_one = 'input + 1';
say_hi(); 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. 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.add = 'input.0 + input.1';
math.subtract = 'input.0 - input.1'; math.subtract = 'input.0 - input.1';
assert_eq(math.add(2, 2), 4); assert_equal(math.add(2, 2), 4);
assert_eq(math.subtract(100, 1), 99); assert_equal(math.subtract(100, 1), 99);
``` ```
### Time ### 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; pub struct Count;
impl Tool for Count { impl Tool for Count {

View File

@ -50,13 +50,14 @@ pub mod time;
/// Master list of all tools. /// Master list of all tools.
/// ///
/// This list is used to match identifiers with tools and to provide info to the shell. /// 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::Count,
&collections::CreateTable, &collections::CreateTable,
&collections::Insert, &collections::Insert,
&collections::Rows, &collections::Rows,
&collections::Select, &collections::Select,
&collections::String, &collections::String,
&collections::Replace,
&collections::Transform, &collections::Transform,
&collections::Where, &collections::Where,
&command::Bash, &command::Bash,