Rename module; Implement collection commands

This commit is contained in:
Jeff 2023-08-23 01:21:02 -04:00
parent 26a71585f0
commit 350fbb701d
16 changed files with 17 additions and 20 deletions

View File

@ -59,7 +59,7 @@ impl Macro for String {
identifier: "string",
description: "Stringify a value.",
group: "collections",
inputs: vec![],
inputs: vec![ValueType::Any],
}
}
@ -89,13 +89,13 @@ impl Macro for Count {
identifier: "count",
description: "Return the number of items in a value.",
group: "collections",
inputs: vec![],
inputs: vec![ValueType::Any],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let len = match argument {
Value::String(string) => string.len(),
Value::String(string) => string.chars().count(),
Value::List(list) => list.len(),
Value::Map(map) => map.len(),
Value::Table(table) => table.len(),
@ -119,7 +119,7 @@ impl Macro for CreateTable {
identifier: "create_table",
description: "Define a new table with a list of column names and list of rows.",
group: "collections",
inputs: vec![],
inputs: vec![ValueType::ListOf(vec![ValueType::List, ValueType::List])],
}
}
@ -155,7 +155,7 @@ impl Macro for Rows {
identifier: "rows",
description: "Extract a table's rows as a list.",
group: "collections",
inputs: vec![],
inputs: vec![ValueType::Table],
}
}
@ -213,13 +213,13 @@ impl Macro for Insert {
identifier: "insert",
description: "Add new rows to a table.",
group: "collections",
inputs: vec![],
inputs: vec![ValueType::Table, ValueType::List],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let argument = argument.as_list()?;
let new_rows = &argument[1..];
let new_rows = argument[1].as_list()?;
let mut table = argument[0].as_table()?.clone();
table.reserve(new_rows.len());
@ -242,7 +242,7 @@ impl Macro for Select {
identifier: "select",
description: "Extract one or more values based on their key.",
group: "collections",
inputs: vec![],
inputs: vec![ValueType::Table, ValueType::String],
}
}

View File

@ -1,13 +1,10 @@
//! This module contains whale's built-in macro system. Every macro is listed
//! alphabetically. Use [call_macro] to check an identifier against every macro.
//! This module contains dust's built-in commands.
//!
//! ## Writing macros
//!
//! - snake case identifier, this is enforced by a test
//! - the type name should be the identifier in upper camel case
//! - always verify user input, this creates helpful errors
//! - the description should be brief, it will display in the shell
//! - maintain alphabetical order
//!
//!
//! ## Usage
//!

View File

@ -2,9 +2,9 @@
#![forbid(unsafe_code)]
pub use crate::{
commands::*,
error::*,
interface::*,
macros::*,
operator::Operator,
token::PartialToken,
tree::Node,
@ -14,9 +14,9 @@ pub use crate::{
},
};
mod commands;
mod error;
mod interface;
mod macros;
mod operator;
mod token;
mod tree;

View File

@ -1,4 +1,4 @@
table = create_table (
table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
@ -7,7 +7,7 @@ table = create_table (
)
);
test_table = create_table (
test_table = create_table(
("text", "bool"),
(
("a", true),
@ -16,9 +16,9 @@ test_table = create_table (
)
);
assert_equal(table:select("text", "bool"), test_table);
assert_equal(select(table, "text", "bool"), test_table);
test_table = create_table (
test_table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
@ -26,4 +26,4 @@ test_table = create_table (
)
);
assert_equal(table:where('text == "a"'), test_table);
assert_equal(where(table, 'text == "a"'), test_table);