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 {