ref: Optimize fb2 parser

This commit is contained in:
2025-09-09 17:42:38 +03:00
parent bf81b5d8f4
commit 18fa38120b

View File

@@ -81,19 +81,13 @@ pub fn parse(path: &Path) -> Result<Vec<Book>, String> {
let mut reader = Reader::from_reader(BufReader::new(file));
let mut buf = Vec::new();
let mut title = String::new();
let mut lang = String::new();
let mut description = String::new();
let mut keywords = Vec::new();
let mut authors = Vec::new();
let mut published_at = String::new();
let mut publisher = String::new();
let mut in_title = false;
let mut in_lang = false;
let mut in_keywords = false;
let mut in_description = false;
let mut book = Book::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(e)) => match e.name().as_ref() {
@@ -102,7 +96,7 @@ pub fn parse(path: &Path) -> Result<Vec<Book>, String> {
b"keywords" => in_keywords = true,
b"annotation" => {
in_description = true;
description.clear();
book.description.clear();
}
b"author" => {
let mut buf_author = Vec::new();
@@ -180,39 +174,39 @@ pub fn parse(path: &Path) -> Result<Vec<Book>, String> {
author.last_name = last_name.clone();
author.middle_name = middle_name.clone();
}
authors.push(author);
book.author.push(author);
}
b"year" => {
if let Ok(Event::Text(t)) = reader.read_event_into(&mut buf) {
published_at = t.xml_content().map_err(|e| e.to_string())?.into_owned();
book.published_at = t.xml_content().map_err(|e| e.to_string())?.into_owned();
}
}
b"publisher" => {
if let Ok(Event::Text(t)) = reader.read_event_into(&mut buf) {
publisher = t.xml_content().map_err(|e| e.to_string())?.into_owned();
book.publisher = t.xml_content().map_err(|e| e.to_string())?.into_owned();
}
}
_ => {}
},
Ok(Event::Text(t)) if in_title => {
title = t.xml_content().map_err(|e| e.to_string())?.into_owned();
book.title = t.xml_content().map_err(|e| e.to_string())?.into_owned();
}
Ok(Event::Text(t)) if in_lang => {
lang = t.xml_content().map_err(|e| e.to_string())?.into_owned();
book.language = t.xml_content().map_err(|e| e.to_string())?.into_owned();
}
Ok(Event::Text(t)) if in_keywords => {
let raw = t.xml_content().map_err(|e| e.to_string())?;
for tag in raw.split(',').map(str::trim).filter(|s| !s.is_empty()) {
keywords.push(tag.to_string());
book.tags.push(tag.to_string());
}
}
Ok(Event::Text(t)) if in_description => {
let txt = t.xml_content().map_err(|e| e.to_string())?;
if !txt.trim().is_empty() {
if !description.is_empty() {
description.push(' ');
if !book.description.is_empty() {
book.description.push(' ');
}
description.push_str(&txt);
book.description.push_str(&txt);
}
}
Ok(Event::End(e)) => match e.name().as_ref() {
@@ -229,14 +223,5 @@ pub fn parse(path: &Path) -> Result<Vec<Book>, String> {
buf.clear();
}
let mut book = Book::new();
book.title = title;
book.author = authors;
book.language = lang;
book.description = description;
book.tags = keywords;
book.published_at = published_at;
book.publisher = publisher;
Ok(vec![book])
}