use std::path::Path;
use serde::Serialize;
use super::error::AiError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Format {
Docx,
Xlsx,
Pptx,
}
impl Format {
pub fn from_path(path: &Path) -> Result<Self, AiError> {
let ext = path
.extension()
.and_then(|s| s.to_str())
.unwrap_or("")
.to_ascii_lowercase();
match ext.as_str() {
"docx" => Ok(Self::Docx),
"xlsx" => Ok(Self::Xlsx),
"pptx" => Ok(Self::Pptx),
"pdf" | "md" | "markdown" | "html" | "htm" | "txt" => Err(AiError::usage(format!(
"bl ai reads native Office files (.docx .xlsx .pptx). \
It does not convert .{ext}. Run blackline on the OOXML package."
))),
"" => Err(AiError::usage(
"file has no extension; expected .docx, .xlsx, or .pptx",
)),
other => Err(AiError::usage(format!(
"unsupported .{other}; expected .docx, .xlsx, or .pptx"
))),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::Docx => "docx",
Self::Xlsx => "xlsx",
Self::Pptx => "pptx",
}
}
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}