Implement from_json

This commit is contained in:
Jeff 2023-10-14 13:52:16 -04:00
parent d898696d6c
commit ca4fd34bc1
2 changed files with 25 additions and 1 deletions

View File

@ -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!(),
};

View File

@ -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<csv::Error> for Error {
fn from(value: csv::Error) -> Self {
Error::ToolFailure(value.to_string())