#[derive(Debug, Clone, Hash, Default, PartialEq, Eq)]
pub struct NZB {
pub meta: Meta,
pub files: Vec<File>,
}
impl NZB {
pub fn new(meta: Meta, files: Vec<File>) -> Self {
Self { meta, files }
}
pub fn parse(xml: impl AsRef<str>) -> Result<Self, InvalidNZBError> {
let xml = xml.as_ref();
let xml = sanitize_xml(xml);
let nzb = match Document::parse(xml) {
Ok(doc) => doc,
Err(err) => return Err(InvalidNZBError::new(err.to_string())),
};
let meta = parse_metadata(&nzb);
let files = match parse_files(&nzb) {
Ok(files) => files,
Err(err) => return Err(InvalidNZBError::new(err.to_string())),
};
Ok(NZB::new(meta, files))
}
}
I'm not really sure what's the idiomatic rust approach to writing an API like this. In this case fn new
is essentially useless, since a user would always supply the xml string itself. My options seem to be:
- Rename
fn parse
tofn new
, removefn parse
. - Keep it as is.
- Provide a top level
pub fn parse
outside of theimpl NZB
? e.g.use mylib::parse; let nzb = parse(xml);