use anyhow::{Context, Result};
use xberg::{SupportedFormat, list_supported_formats};
pub fn compiled_in_formats() -> Result<Vec<SupportedFormat>> {
xberg::extractors::ensure_initialized()
.context("Failed to initialize the extractor registry while listing supported formats")?;
let registry = xberg::plugins::registry::get_document_extractor_registry();
let registered = registry.read();
Ok(list_supported_formats()
.into_iter()
.filter(|format| registered.get(&format.mime_type).is_ok())
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
fn extensions() -> Vec<String> {
compiled_in_formats()
.expect("listing compiled-in formats must succeed")
.into_iter()
.map(|format| format.extension)
.collect()
}
fn contains(extensions: &[String], extension: &str) -> bool {
extensions.iter().any(|candidate| candidate == extension)
}
#[test]
fn compiled_in_formats_always_reports_ungated_formats() {
let extensions = extensions();
for extension in ["txt", "md", "csv", "json"] {
assert!(
contains(&extensions, extension),
"'{extension}' has an ungated extractor and must always be reported; got {extensions:?}"
);
}
}
#[test]
fn compiled_in_formats_tracks_the_compiled_feature_set() {
let extensions = extensions();
if cfg!(feature = "pdf-surface") {
assert!(
contains(&extensions, "pdf"),
"the `pdf-surface` feature is enabled, so 'pdf' must be reported; got {extensions:?}"
);
} else {
assert!(
!contains(&extensions, "pdf"),
"the `pdf-surface` feature is disabled, so 'pdf' must not be advertised; got {extensions:?}"
);
}
for extension in ["docx", "xlsx"] {
if cfg!(feature = "formats-no-heic") {
assert!(
contains(&extensions, extension),
"the `formats-no-heic` feature is enabled, so '{extension}' must be reported; got {extensions:?}"
);
} else {
assert!(
!contains(&extensions, extension),
"the `formats-no-heic` feature is disabled, so '{extension}' must not be advertised; \
this is the GH#1387 mismatch; got {extensions:?}"
);
}
}
}
#[test]
fn compiled_in_formats_is_a_subset_of_the_static_catalogue() {
let compiled_in = compiled_in_formats().expect("listing compiled-in formats must succeed");
let catalogue = list_supported_formats();
assert!(
compiled_in.len() <= catalogue.len(),
"compiled-in formats ({}) cannot exceed the static catalogue ({})",
compiled_in.len(),
catalogue.len()
);
if !cfg!(feature = "formats-no-heic") {
assert!(
compiled_in.len() < catalogue.len(),
"without the format features the catalogue ({}) must over-report relative to the \
compiled-in list ({}) — equal lengths mean the filter did nothing",
catalogue.len(),
compiled_in.len()
);
}
}
}