From 0a8464bab3fc960ed4bd315fc81f67f031156e6f Mon Sep 17 00:00:00 2001 From: "Andrew Phillips (aider)" Date: Sat, 10 May 2025 13:45:51 -0300 Subject: [PATCH] fix: add error handling for unknown column types --- src/modes/common.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/modes/common.rs b/src/modes/common.rs index 0da264f..6af5bfb 100644 --- a/src/modes/common.rs +++ b/src/modes/common.rs @@ -73,6 +73,29 @@ impl ColumnType { ColumnType::Meta => "meta", } } + + /// Returns a Result with error message if the string is not a valid ColumnType + pub fn from_str(s: &str) -> anyhow::Result { + Self::try_from(s) + } +} + +impl TryFrom<&str> for ColumnType { + type Error = anyhow::Error; + + fn try_from(s: &str) -> Result { + match s { + "id" => Ok(ColumnType::Id), + "time" => Ok(ColumnType::Time), + "size" => Ok(ColumnType::Size), + "compression" => Ok(ColumnType::Compression), + "filesize" => Ok(ColumnType::FileSize), + "filepath" => Ok(ColumnType::FilePath), + "tags" => Ok(ColumnType::Tags), + "meta" => Ok(ColumnType::Meta), + _ => Err(anyhow!("Unknown column type: {}", s)), + } + } } pub fn get_format_box_chars_no_border_line_separator() -> TableFormat {