indexer: More crate updates + warning fixes + 2018 edition

This commit is contained in:
Yorhel 2021-12-11 14:52:04 +01:00
parent c48feedc85
commit c9e81a8922
14 changed files with 193 additions and 499 deletions

View file

@ -2,41 +2,25 @@ use std::collections::HashSet;
use std::io::BufReader;
use std::str::FromStr;
use std::error::Error;
use std::fmt;
use chrono::NaiveDateTime;
use postgres;
use quick_xml as xml;
use quick_xml::events::Event;
use archive;
use open;
use pkg;
use man;
use crate::archive;
use crate::open;
use crate::pkg;
use crate::man;
// Ugh, quick-xml's Error type does not implement Error.
#[derive(Debug)]
struct XmlError(String);
impl fmt::Display for XmlError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) }
}
impl Error for XmlError {
fn description(&self) -> &str { self.0.as_ref() }
}
fn to_err(e: xml::Error) -> XmlError {
XmlError(format!("{}", e))
}
fn xml_getattr(e: &xml::events::BytesStart, attr: &str) -> Result<String,Box<Error>> {
fn xml_getattr(e: &xml::events::BytesStart, attr: &str) -> Result<String,Box<dyn Error>> {
for kv in e.attributes().with_checks(false) {
let kv = kv.map_err(to_err)?;
let kv = kv?;
if kv.key == attr.as_bytes() {
return Ok(String::from_utf8(kv.value.into_owned())?);
}
}
Err(Box::new(XmlError(format!("Attribute '{}' not found", attr))))
Err(Box::new(xml::Error::UnexpectedToken(format!("Attribute '{}' not found", attr))))
}
@ -53,7 +37,7 @@ struct PkgInfo {
// Shared function to read primary.xml.gz and filelists.xml.gz. Runs the callback for each package
// with the info that was found.
fn readpkgs<F>(url: String, mut cb: F) -> Result<(),Box<Error>>
fn readpkgs<F>(url: String, mut cb: F) -> Result<(),Box<dyn Error>>
where F: FnMut(PkgInfo)
{
debug!("Reading {}", url);
@ -74,8 +58,7 @@ fn readpkgs<F>(url: String, mut cb: F) -> Result<(),Box<Error>>
loop {
buf.clear();
let event = xml.read_event(&mut buf);
let event = event.map_err(to_err)?;
let event = xml.read_event(&mut buf)?;
match event {
@ -97,7 +80,7 @@ fn readpkgs<F>(url: String, mut cb: F) -> Result<(),Box<Error>>
Event::Text(e) =>
if savestr {
saved = Some(e.unescape_and_decode(&xml).map_err(to_err)?);
saved = Some(e.unescape_and_decode(&xml)?);
savestr = false
},
@ -126,7 +109,7 @@ fn readpkgs<F>(url: String, mut cb: F) -> Result<(),Box<Error>>
// Reads repomd.xml and returns the path to the primary.xml.gz and filelists.xml.gz
fn repomd(url: String) -> Result<(String,String),Box<Error>> {
fn repomd(url: String) -> Result<(String,String),Box<dyn Error>> {
debug!("Reading {}", url);
let mut fd = open::Path{path: &url, cache: true, canbelocal: false}.open()?;
let mut xml = xml::Reader::from_reader(
@ -143,7 +126,7 @@ fn repomd(url: String) -> Result<(String,String),Box<Error>> {
loop {
buf.clear();
let event = xml.read_event(&mut buf).map_err(to_err)?;
let event = xml.read_event(&mut buf)?;
match event {
Event::Start(ref e) |
Event::Empty(ref e) => {
@ -173,7 +156,7 @@ fn repomd(url: String) -> Result<(String,String),Box<Error>> {
}
pub fn sync<T: postgres::GenericClient>(pg: &mut T, sys: i32, cat: &str, mirror: &str) -> Result<(),Box<Error>> {
pub fn sync<T: postgres::GenericClient>(pg: &mut T, sys: i32, cat: &str, mirror: &str) -> Result<(),Box<dyn Error>> {
let(primary, filelists) = repomd(format!("{}repodata/repomd.xml", mirror))?;
let mut pkgswithman = HashSet::new();