get_epub_metadata() simplified.
This commit is contained in:
parent
da77983228
commit
b02ede5182
1 changed files with 21 additions and 23 deletions
44
src/epub.rs
44
src/epub.rs
|
@ -1,5 +1,4 @@
|
|||
use std::{
|
||||
borrow::Borrow,
|
||||
collections::HashMap,
|
||||
fs::{self, File},
|
||||
io::Read,
|
||||
|
@ -132,20 +131,20 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
loop {
|
||||
match reader.read_event_into(&mut buf) {
|
||||
// See if we have EPUB3 or EPUB2
|
||||
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"package" => {
|
||||
Ok(Event::Start(ref e)) if e.name().as_ref() == b"package" => {
|
||||
if e.attributes().any(|attr| {
|
||||
attr.as_ref().unwrap().key.into_inner() == b"version"
|
||||
attr.as_ref().unwrap().key.as_ref() == b"version"
|
||||
&& attr.as_ref().unwrap().value.starts_with(b"3")
|
||||
}) {
|
||||
is_epub3 = true;
|
||||
}
|
||||
}
|
||||
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"creator" => {
|
||||
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"creator" => {
|
||||
creator_found = true;
|
||||
if is_epub3 {
|
||||
if let Some(idval) = e
|
||||
.attributes()
|
||||
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"id")
|
||||
.filter(|attr| attr.as_ref().unwrap().key.as_ref() == b"id")
|
||||
.next()
|
||||
{
|
||||
curr_id = "#".to_string()
|
||||
|
@ -165,7 +164,7 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
attr.as_ref()
|
||||
.unwrap()
|
||||
.key
|
||||
.into_inner()
|
||||
.as_ref()
|
||||
.ends_with(b"file-as")
|
||||
})
|
||||
.next()
|
||||
|
@ -174,13 +173,13 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
let entry = xml_authors.entry(curr_id.clone()).or_insert(XmlAut::new());
|
||||
entry.sort = file_as_val
|
||||
.unwrap()
|
||||
.decode_and_unescape_value(*reader.decoder().borrow())
|
||||
.unescape_value()
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
entry.role = "aut".to_string();
|
||||
} else if let Some(_role_val) = e
|
||||
.attributes()
|
||||
.filter(|attr| attr.as_ref().unwrap().key.into_inner().ends_with(b"role"))
|
||||
.filter(|attr| attr.as_ref().unwrap().key.as_ref().ends_with(b"role"))
|
||||
.next()
|
||||
{
|
||||
curr_id = "none".to_string() + xml_authors.len().to_string().as_str();
|
||||
|
@ -199,33 +198,33 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
|
||||
creator_found = false;
|
||||
}
|
||||
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"meta" && is_epub3 => {
|
||||
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"meta" && is_epub3 => {
|
||||
if let Some(refines) = e
|
||||
.attributes()
|
||||
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"refines")
|
||||
.filter(|attr| attr.as_ref().unwrap().key.as_ref() == b"refines")
|
||||
.next()
|
||||
{
|
||||
if e.attributes().any(|attr| {
|
||||
attr.as_ref().unwrap().key.into_inner() == b"property"
|
||||
attr.as_ref().unwrap().key.as_ref() == b"property"
|
||||
&& attr.as_ref().unwrap().value.ends_with(b"file-as")
|
||||
}) {
|
||||
curr_id = String::from_utf8(refines.unwrap().value.to_vec()).unwrap();
|
||||
file_as_found = true;
|
||||
} else if e.attributes().any(|attr| {
|
||||
attr.as_ref().unwrap().key.into_inner() == b"property"
|
||||
attr.as_ref().unwrap().key.as_ref() == b"property"
|
||||
&& attr.as_ref().unwrap().value.ends_with(b"role")
|
||||
}) {
|
||||
curr_id = String::from_utf8(refines.unwrap().value.to_vec()).unwrap();
|
||||
role_found = true;
|
||||
} else if e.attributes().any(|attr| {
|
||||
attr.as_ref().unwrap().key.into_inner() == b"property"
|
||||
attr.as_ref().unwrap().key.as_ref() == b"property"
|
||||
&& attr.as_ref().unwrap().value.ends_with(b"group-position")
|
||||
}) {
|
||||
series_index_found = true;
|
||||
}
|
||||
}
|
||||
if e.attributes().any(|attr| {
|
||||
attr.as_ref().unwrap().key.into_inner() == b"property"
|
||||
attr.as_ref().unwrap().key.as_ref() == b"property"
|
||||
&& attr
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
|
@ -235,9 +234,9 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
series_found = true;
|
||||
}
|
||||
}
|
||||
Ok(Event::Empty(ref e)) if e.local_name().into_inner() == b"meta" && !is_epub3 => {
|
||||
Ok(Event::Empty(ref e)) if e.local_name().as_ref() == b"meta" && !is_epub3 => {
|
||||
if e.attributes().any(|attr| {
|
||||
attr.as_ref().unwrap().key.into_inner() == b"name"
|
||||
attr.as_ref().unwrap().key.as_ref() == b"name"
|
||||
&& attr
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
|
@ -247,15 +246,15 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
}) {
|
||||
epub_meta.series.name = e
|
||||
.attributes()
|
||||
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"content")
|
||||
.filter(|attr| attr.as_ref().unwrap().key.as_ref() == b"content")
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.decode_and_unescape_value(*reader.decoder().borrow())
|
||||
.unescape_value()
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
} else if e.attributes().any(|attr| {
|
||||
attr.as_ref().unwrap().key.into_inner() == b"name"
|
||||
attr.as_ref().unwrap().key.as_ref() == b"name"
|
||||
&& attr
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
|
@ -265,11 +264,11 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
}) {
|
||||
let index_float = e
|
||||
.attributes()
|
||||
.filter(|attr| attr.as_ref().unwrap().key.into_inner() == b"content")
|
||||
.filter(|attr| attr.as_ref().unwrap().key.as_ref() == b"content")
|
||||
.next()
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.decode_and_unescape_value(*reader.decoder().borrow())
|
||||
.unescape_value()
|
||||
.unwrap_or_default()
|
||||
.parse::<f32>()
|
||||
.unwrap_or_default();
|
||||
|
@ -301,11 +300,10 @@ pub fn get_epub_metadata(filename: &str) -> Option<EpubMetadata> {
|
|||
|
||||
series_index_found = false;
|
||||
}
|
||||
Ok(Event::Start(ref e)) if e.local_name().into_inner() == b"subject" => {
|
||||
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"subject" => {
|
||||
genre_found = true;
|
||||
}
|
||||
Ok(Event::Text(ref e)) if genre_found => {
|
||||
//epub_meta.genre = e.unescape_and_decode(&reader).unwrap();
|
||||
epub_meta.genre = e.unescape().unwrap().to_string();
|
||||
genre_found = false;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue