#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MediaTypeCapabilities {
pub media_type: &'static str,
pub extension: &'static str,
pub label: &'static str,
pub render: RenderMode,
pub anchoring: AnchoringModel,
pub text_source: TextSource,
pub authorable: bool,
pub uploadable: bool,
pub generatable: bool,
}
include!(concat!(env!("OUT_DIR"), "/media_types.rs"));
pub fn base_media_type(format: &str) -> String {
format
.split(';')
.next()
.unwrap_or_default()
.trim()
.to_lowercase()
}
pub fn capabilities_of(format: &str) -> Option<&'static MediaTypeCapabilities> {
let base = base_media_type(format);
MEDIA_TYPES.iter().find(|row| row.media_type == base)
}
pub fn text_source_of(format: &str) -> TextSource {
match capabilities_of(format) {
Some(row) => row.text_source,
None if base_media_type(format).starts_with("text/") => TextSource::Decode,
None => TextSource::None,
}
}
pub fn yields_geometry_of(format: &str) -> bool {
match text_source_of(format) {
TextSource::PdfTextLayer => true,
TextSource::Decode | TextSource::None => false,
}
}
pub fn derives_text_of(format: &str) -> bool {
match text_source_of(format) {
TextSource::PdfTextLayer => true,
TextSource::Decode | TextSource::None => false,
}
}
pub fn clone_format(source: Option<&str>) -> &'static MediaTypeCapabilities {
source
.and_then(capabilities_of)
.filter(|row| row.authorable)
.unwrap_or(&PLAIN_TEXT)
}
pub fn storage_file_name(name: &str, format: &MediaTypeCapabilities) -> String {
let mut slug = String::new();
for c in name.to_lowercase().chars() {
if c.is_ascii_lowercase() || c.is_ascii_digit() {
slug.push(c);
} else if !slug.ends_with('-') {
slug.push('-');
}
}
let slug = slug.strip_prefix('-').unwrap_or(&slug);
let slug = slug.strip_suffix('-').unwrap_or(slug);
format!("{slug}{}", format.extension)
}
pub fn derive_storage_uri(name: &str, format: &MediaTypeCapabilities) -> String {
format!("file://{}", storage_file_name(name, format))
}