Mercurial > lbo > hg > scrapeprice
changeset 8:6027d11cb86d
Move Extractor/Extracted traits to driver mod
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 22 Mar 2020 23:10:06 +0100 |
parents | 8dee877af779 |
children | e13f77dac798 |
files | src/driver.rs src/extract.rs src/implem.rs |
diffstat | 3 files changed, 17 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/src/driver.rs Sun Mar 22 23:08:08 2020 +0100 +++ b/src/driver.rs Sun Mar 22 23:10:06 2020 +0100 @@ -26,12 +26,26 @@ fn next(&mut self, doc: &extract::Document) -> Vec<Uri>; } +/// Extracted information can be presented as sequence of key/value pairs. +pub trait Extracted { + fn all(&mut self) -> Box<dyn iter::Iterator<Item = (String, String)> + Send> { + Box::new(iter::empty()) + } +} + +/// An Extractor retrieves information from a Document. +pub trait Extractor { + fn extract(&mut self, doc: &extract::Document) -> Option<Box<dyn Extracted>> { + None + } +} + /// DriverLogic holds the driven implementation. The members tell the driver what to fetch, and /// what and how to store it. pub struct DriverLogic { pub explore: Box<dyn Explorer>, pub store: Box<dyn Storage>, - pub extract: Box<dyn extract::Extractor>, + pub extract: Box<dyn Extractor>, } pub struct Driver {
--- a/src/extract.rs Sun Mar 22 23:08:08 2020 +0100 +++ b/src/extract.rs Sun Mar 22 23:10:06 2020 +0100 @@ -60,18 +60,6 @@ .map_err(|_| HTTPError::LogicError(format!("failed to parse selector {}", sel))) } -pub trait Extracted { - fn all(&mut self) -> Box<dyn iter::Iterator<Item = (String, String)> + Send> { - Box::new(iter::empty()) - } -} - -pub trait Extractor { - fn extract(&mut self, doc: &Document) -> Option<Box<dyn Extracted>> { - None - } -} - #[cfg(test)] mod tests { use super::Document;
--- a/src/implem.rs Sun Mar 22 23:08:08 2020 +0100 +++ b/src/implem.rs Sun Mar 22 23:10:06 2020 +0100 @@ -19,8 +19,8 @@ String::from_iter(s.chars().skip(start).take(len)) } -impl extract::Extractor for AudiophilItemPriceExtractor { - fn extract(&mut self, doc: &extract::Document) -> Option<Box<dyn extract::Extracted>> { +impl driver::Extractor for AudiophilItemPriceExtractor { + fn extract(&mut self, doc: &extract::Document) -> Option<Box<dyn driver::Extracted>> { let mut data = doc.get_contents(&[".bez.neu", ".preis strong"]).unwrap(); let prices = data.pop().unwrap(); let descs = data.pop().unwrap();