use std::path::Path;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum InputFormat {
#[cfg(feature = "html")]
Html,
#[cfg(feature = "markdown")]
Markdown,
#[cfg(feature = "docx")]
Docx,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OutputFormat {
#[cfg(feature = "blocknote-writer")]
Blocknote,
#[cfg(feature = "html-writer")]
Html,
#[cfg(feature = "oxa-writer")]
Oxa,
#[cfg(feature = "pandoc-native-writer")]
PandocNative,
}
#[inline]
#[must_use]
pub fn detect_input_format(path: &Path) -> Option<InputFormat> {
let ext = path.extension()?.to_str()?.to_ascii_lowercase();
match ext.as_str() {
#[cfg(feature = "docx")]
"docx" => Some(InputFormat::Docx),
#[cfg(feature = "html")]
"html" | "htm" => Some(InputFormat::Html),
#[cfg(feature = "markdown")]
"md" | "markdown" => Some(InputFormat::Markdown),
_ => None,
}
}
#[inline]
#[must_use]
pub fn detect_output_format(path: &Path) -> Option<OutputFormat> {
let ext = path.extension()?.to_str()?.to_ascii_lowercase();
match ext.as_str() {
#[cfg(feature = "html-writer")]
"html" | "htm" => Some(OutputFormat::Html),
#[cfg(feature = "blocknote-writer")]
"json" => Some(OutputFormat::Blocknote),
#[cfg(feature = "pandoc-native-writer")]
"native" => Some(OutputFormat::PandocNative),
_ => None,
}
}
#[inline]
#[must_use]
pub fn strip_bom(input: &str) -> &str {
input.strip_prefix('\u{FEFF}').unwrap_or(input)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strip_bom_empty_string() {
assert_eq!(strip_bom(""), "");
}
#[test]
fn strip_bom_no_bom() {
assert_eq!(strip_bom("hello"), "hello");
}
#[test]
fn strip_bom_with_bom() {
assert_eq!(strip_bom("\u{FEFF}hello"), "hello");
}
#[test]
fn strip_bom_bom_only() {
assert_eq!(strip_bom("\u{FEFF}"), "");
}
#[cfg(feature = "docx")]
#[test]
fn detect_input_format_recognizes_docx() {
use std::path::Path;
assert_eq!(
detect_input_format(Path::new("a.docx")),
Some(InputFormat::Docx)
);
assert_eq!(
detect_input_format(Path::new("a.DOCX")),
Some(InputFormat::Docx)
);
assert_eq!(
detect_input_format(Path::new("a.DocX")),
Some(InputFormat::Docx)
);
}
}