From ca4fd34bc1a9c33b4373a5d452d080ded670da40 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 14 Oct 2023 13:52:16 -0400 Subject: [PATCH] Implement from_json --- src/abstract_tree/tool.rs | 8 +++++++- src/error.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/abstract_tree/tool.rs b/src/abstract_tree/tool.rs index e78e73b..cdf6dfb 100644 --- a/src/abstract_tree/tool.rs +++ b/src/abstract_tree/tool.rs @@ -184,7 +184,13 @@ impl Tool { Tool::Zsh => todo!(), Tool::FromCsv => todo!(), Tool::ToCsv => todo!(), - Tool::FromJson => todo!(), + Tool::FromJson => { + Error::expect_tool_argument_amount("from_json", 1, values.len())?; + + let json_string = values[0].as_string()?; + + serde_json::from_str(json_string)? + } Tool::ToJson => todo!(), }; diff --git a/src/error.rs b/src/error.rs index f699253..055b7b6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -122,6 +122,24 @@ pub enum Error { CustomMessage(String), } +impl Error { + pub fn expect_tool_argument_amount( + tool_name: &'static str, + expected: usize, + actual: usize, + ) -> Result<()> { + if expected == actual { + Ok(()) + } else { + Err(Error::ExpectedToolArgumentAmount { + tool_name, + expected, + actual, + }) + } + } +} + impl From for Error { fn from(value: csv::Error) -> Self { Error::ToolFailure(value.to_string())