use crate::models::parameters::GeoffreyParam; use serde::{Deserialize, Serialize}; use std::fmt::{Display, Formatter}; use std::str::FromStr; #[derive(Debug, Serialize, Deserialize, Clone)] pub enum ItemSort { Price, Restock, } impl Display for ItemSort { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let s = match self { ItemSort::Price => "Price", ItemSort::Restock => "Restock Time", }; write!(f, "{}", s) } } impl FromStr for ItemSort { type Err = String; fn from_str(s: &str) -> Result { let sort = match s.to_lowercase().as_str() { "price" => ItemSort::Price, "restock" => ItemSort::Restock, &_ => return Err(format!("Unknown sort '{}'", s)), }; Ok(sort) } } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Copy)] pub enum Order { High, Low, } impl Display for Order { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { let s = match self { Order::High => "High", Order::Low => "Low", }; write!(f, "{}", s) } } impl FromStr for Order { type Err = String; fn from_str(s: &str) -> Result { let order = match s.to_lowercase().as_str() { "high" => Order::High, "low" => Order::Low, &_ => return Err(format!("Unknown sorting '{}'", s)), }; Ok(order) } } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct SellingParams { pub query: String, pub sort: Option, pub order: Option, } impl SellingParams { pub fn new(query: String, sort: Option, order: Option) -> Self { Self { query, sort, order } } } impl GeoffreyParam for SellingParams {}