diff --git a/scraper-rs/src/supermercado.rs b/scraper-rs/src/supermercado.rs index c2a9054..6b46693 100644 --- a/scraper-rs/src/supermercado.rs +++ b/scraper-rs/src/supermercado.rs @@ -1,7 +1,15 @@ use clap::ValueEnum; use reqwest::Url; -#[derive(ValueEnum, Clone, Debug, Copy)] +const SUPERMERCADOS_HOSTS: [(Supermercado, &'static str); 5] = [ + (Supermercado::Dia, "diaonline.supermercadosdia.com.ar"), + (Supermercado::Carrefour, "www.carrefour.com.ar"), + (Supermercado::Coto, "www.cotodigital3.com.ar"), + (Supermercado::Jumbo, "www.jumbo.com.ar"), + (Supermercado::Farmacity, "www.farmacity.com"), +]; + +#[derive(ValueEnum, Clone, Debug, Copy, PartialEq)] pub enum Supermercado { Dia, Jumbo, @@ -11,22 +19,32 @@ pub enum Supermercado { } impl Supermercado { pub fn host(&self) -> &'static str { - match self { - Self::Dia => "diaonline.supermercadosdia.com.ar", - Self::Carrefour => "www.carrefour.com.ar", - Self::Coto => "www.cotodigital3.com.ar", - Self::Jumbo => "www.jumbo.com.ar", - Self::Farmacity => "www.farmacity.com", - } + SUPERMERCADOS_HOSTS + .into_iter() + .find(|(supermercado, _host)| self == supermercado) + .map(|(_, host)| host) + .unwrap() } pub fn from_url(url: &Url) -> Option { - match url.host_str().unwrap() { - "www.carrefour.com.ar" => Some(Self::Carrefour), - "diaonline.supermercadosdia.com.ar" => Some(Self::Dia), - "www.cotodigital3.com.ar" => Some(Self::Coto), - "www.jumbo.com.ar" => Some(Self::Jumbo), - "www.farmacity.com" => Some(Self::Farmacity), - _ => None, - } + SUPERMERCADOS_HOSTS + .into_iter() + .find(|(_supermercado, host)| *host == url.host_str().unwrap()) + .map(|(supermercado, _host)| supermercado) + } +} + +#[cfg(test)] +mod tests { + use super::Supermercado; + + #[test] + fn host_to_supermercado() { + let supermercado = Supermercado::from_url(&reqwest::Url::parse("https://diaonline.supermercadosdia.com.ar/repelente-para-mosquitos-off--family-aerosol-165-cc-6338/p").unwrap()); + assert_eq!(supermercado, Some(Supermercado::Dia)) + } + #[test] + fn supermercado_to_host() { + let host = Supermercado::Coto.host(); + assert_eq!(host, "www.cotodigital3.com.ar") } }