mejorar supermercado

This commit is contained in:
Cat /dev/Nulo 2024-06-23 21:48:29 -03:00
parent d495acfc9d
commit ccb5b2c2ef

View file

@ -1,7 +1,15 @@
use clap::ValueEnum; use clap::ValueEnum;
use reqwest::Url; 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 { pub enum Supermercado {
Dia, Dia,
Jumbo, Jumbo,
@ -11,22 +19,32 @@ pub enum Supermercado {
} }
impl Supermercado { impl Supermercado {
pub fn host(&self) -> &'static str { pub fn host(&self) -> &'static str {
match self { SUPERMERCADOS_HOSTS
Self::Dia => "diaonline.supermercadosdia.com.ar", .into_iter()
Self::Carrefour => "www.carrefour.com.ar", .find(|(supermercado, _host)| self == supermercado)
Self::Coto => "www.cotodigital3.com.ar", .map(|(_, host)| host)
Self::Jumbo => "www.jumbo.com.ar", .unwrap()
Self::Farmacity => "www.farmacity.com",
}
} }
pub fn from_url(url: &Url) -> Option<Self> { pub fn from_url(url: &Url) -> Option<Self> {
match url.host_str().unwrap() { SUPERMERCADOS_HOSTS
"www.carrefour.com.ar" => Some(Self::Carrefour), .into_iter()
"diaonline.supermercadosdia.com.ar" => Some(Self::Dia), .find(|(_supermercado, host)| *host == url.host_str().unwrap())
"www.cotodigital3.com.ar" => Some(Self::Coto), .map(|(supermercado, _host)| supermercado)
"www.jumbo.com.ar" => Some(Self::Jumbo),
"www.farmacity.com" => Some(Self::Farmacity),
_ => None,
} }
} }
#[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")
}
} }