use logoxide::{
Result,
catalog::{Catalog, CatalogSource},
};
pub fn parse(location: &str, bytes: &[u8]) -> Result<Catalog> {
let path = location.split(['?', '#']).next().unwrap_or(location);
match path.to_ascii_lowercase().ends_with(".tar.gz") {
true => crate::sword::catalog(location, bytes),
false => parse_text(location, &String::from_utf8_lossy(bytes)),
}
}
pub fn parse_text(location: &str, text: &str) -> Result<Catalog> {
let path = location
.split(['?', '#'])
.next()
.unwrap_or(location)
.to_ascii_lowercase();
if crate::door43::is_catalog(location) {
crate::door43::catalog(text)
} else if let Some(genre) = crate::ccel::genre_of(location) {
crate::ccel::genre_catalog(text, genre)
} else if path.contains("ccel.org/ccel/") {
crate::ccel::catalog(text)
} else if path.ends_with(".csv") {
crate::ebible::catalog(text)
} else if path.ends_with(".json") {
crate::fetch::catalog(text)
} else {
Catalog::parse(location, text)
}
}
pub fn is_publishers(location: &str) -> bool {
let path = location
.split(['?', '#'])
.next()
.unwrap_or(location)
.to_ascii_lowercase();
path.contains("ccel.org/ccel/")
|| path.contains("ccel.org/index/genre/")
|| crate::door43::is_catalog(&path)
|| path.ends_with(".csv")
|| path.ends_with(".json")
|| path.ends_with(".tar.gz")
}
pub fn publisher_name(source: &CatalogSource) -> Option<&'static str> {
Some(match source.prefix()? {
"ebible" => "eBible.org",
"fetch" => "fetch.bible",
"ccel" => "CCEL",
"sword" => "SWORD",
"door43" => "Door43",
_ => return None,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lists_are_told_apart_by_location() {
let toml = "[[package]]\nid = \"kjv\"\nname = \"KJV\"\nabbreviation = \"KJV\"\nlanguage = \"en\"\ngit = \"https://example.org/kjv\"\n";
let catalog = parse("https://example.org/catalog.toml", toml.as_bytes()).unwrap();
assert_eq!(catalog.entries[0].id, "kjv");
assert_eq!(publisher_name(&catalog.entries[0].source), None);
assert!(!is_publishers("https://example.org/catalog.toml"));
for location in logoxide::catalog::DEFAULT_CATALOGS {
assert!(is_publishers(location), "{location}");
}
assert!(is_publishers(
"https://ebible.org/Scriptures/translations.csv?x=1"
));
assert!(parse("translations.csv", b"").is_err());
assert!(parse("https://ccel.org/ccel/bible/", b"<html></html>").is_err());
assert!(parse("mods.d.tar.gz", b"not gzip").is_err());
assert!(is_publishers("https://ccel.org/index/genre/gen-COM"));
assert!(parse("https://ccel.org/index/genre/gen-COM", b"<html></html>").is_err());
}
}