From 491dd649cdecc550b840e14b04a6e14d1cb00c81 Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 25 Aug 2023 10:58:32 -0400 Subject: [PATCH] Reimplement sort --- src/tools/collections.rs | 29 +++++++++++++++++++++++++++++ src/tools/mod.rs | 29 ----------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/tools/collections.rs b/src/tools/collections.rs index 677d260..92f2d16 100644 --- a/src/tools/collections.rs +++ b/src/tools/collections.rs @@ -4,6 +4,35 @@ use crate::{Error, Result, Table, Tool, ToolInfo, Value, ValueType, VariableMap}; +pub struct Sort; + +impl Tool for Sort { + fn info(&self) -> ToolInfo<'static> { + ToolInfo { + identifier: "sort", + description: "Apply default ordering to a list or table.", + group: "collections", + inputs: vec![ValueType::List, ValueType::Table], + } + } + + fn run(&self, argument: &Value) -> Result { + if let Ok(mut list) = argument.as_list().cloned() { + list.sort(); + + Ok(Value::List(list)) + } else if let Ok(mut table) = argument.as_table().cloned() { + table.sort(); + + Ok(Value::Table(table)) + } else { + Err(crate::Error::ExpectedList { + actual: argument.clone(), + }) + } + } +} + pub struct Transform; impl Tool for Transform { diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 8f79013..19a16ef 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -168,35 +168,6 @@ pub struct ToolInfo<'a> { // } // } -// pub struct Sort; - -// impl Macro for Sort { -// fn info(&self) -> MacroInfo<'static> { -// MacroInfo { -// identifier: "sort", -// description: "Apply default ordering.", -// } -// } - -// fn run(&self, argument: &Value) -> Result { -// if let Ok(mut list) = argument.as_list().cloned() { -// list.sort(); - -// Ok(Value::List(list)) -// } else if let Ok(map) = argument.as_map().cloned() { -// Ok(Value::Map(map)) -// } else if let Ok(mut table) = argument.as_table().cloned() { -// table.sort(); - -// Ok(Value::Table(table)) -// } else { -// Err(crate::Error::ExpectedList { -// actual: argument.clone(), -// }) -// } -// } -// } - // pub struct Map; // impl Macro for Map {