fix: add error handling for unknown column types

This commit is contained in:
Andrew Phillips (aider)
2025-05-10 13:45:51 -03:00
parent 33fa31bbb6
commit 0a8464bab3

View File

@@ -73,6 +73,29 @@ impl ColumnType {
ColumnType::Meta => "meta", 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> {
Self::try_from(s)
}
}
impl TryFrom<&str> for ColumnType {
type Error = anyhow::Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
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 { pub fn get_format_box_chars_no_border_line_separator() -> TableFormat {