opds error handling
This commit is contained in:
parent
faea154ff5
commit
93aeb80c56
12 changed files with 94 additions and 47 deletions
15
rusty-library/src/opds/error.rs
Normal file
15
rusty-library/src/opds/error.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use std::string::FromUtf8Error;
|
||||
|
||||
use quick_xml::DeError;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("opds error")]
|
||||
pub enum OpdsError {
|
||||
#[error("failed to serialize struct")]
|
||||
SerializingError(#[from] DeError),
|
||||
#[error("xml failure")]
|
||||
XmlError(#[from] quick_xml::Error),
|
||||
#[error("failed to decode as utf-8")]
|
||||
Utf8Error(#[from] FromUtf8Error),
|
||||
}
|
|
@ -8,7 +8,7 @@ use quick_xml::{
|
|||
use serde::Serialize;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use super::{author::Author, entry::Entry, link::Link};
|
||||
use super::{author::Author, entry::Entry, error::OpdsError, link::Link};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename = "feed")]
|
||||
|
@ -26,14 +26,14 @@ pub struct Feed {
|
|||
}
|
||||
|
||||
impl Feed {
|
||||
pub fn as_xml(&self) -> String {
|
||||
let xml = to_string(&self).unwrap();
|
||||
pub fn as_xml(&self) -> Result<String, OpdsError> {
|
||||
let xml = to_string(&self)?;
|
||||
let mut reader = Reader::from_str(&xml);
|
||||
reader.trim_text(true);
|
||||
|
||||
let declaration = BytesDecl::new("1.0", Some("UTF-8"), None);
|
||||
let mut writer = Writer::new(Cursor::new(Vec::new()));
|
||||
writer.write_event(Event::Decl(declaration)).unwrap();
|
||||
writer.write_event(Event::Decl(declaration))?;
|
||||
|
||||
let mut feed_start = BytesStart::new("feed");
|
||||
feed_start.push_attribute(("xmlns", "http://www.w3.org/2005/Atom"));
|
||||
|
@ -45,15 +45,15 @@ impl Feed {
|
|||
|
||||
loop {
|
||||
match reader.read_event() {
|
||||
Ok(Event::Start(e)) if e.name().as_ref() == b"feed" => writer
|
||||
.write_event(Event::Start(feed_start.clone()))
|
||||
.unwrap(),
|
||||
Ok(Event::Start(e)) if e.name().as_ref() == b"feed" => {
|
||||
writer.write_event(Event::Start(feed_start.clone()))?
|
||||
}
|
||||
Ok(Event::Eof) => break,
|
||||
Ok(e) => writer.write_event(e).unwrap(),
|
||||
Ok(e) => writer.write_event(e)?,
|
||||
Err(e) => (),
|
||||
}
|
||||
}
|
||||
let result = writer.into_inner().into_inner();
|
||||
String::from_utf8(result).unwrap()
|
||||
Ok(String::from_utf8(result)?)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
pub mod author;
|
||||
pub mod content;
|
||||
pub mod entry;
|
||||
pub mod error;
|
||||
pub mod feed;
|
||||
pub mod link;
|
||||
pub mod media_type;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue