Geoffrey-rs/geoffrey_models/src/models/parameters/selling_params.rs

82 lines
1.8 KiB
Rust

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<Self, Self::Err> {
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<Self, Self::Err> {
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<ItemSort>,
pub order: Option<Order>,
}
impl SellingParams {
pub fn new(query: String, sort: Option<ItemSort>, order: Option<Order>) -> Self {
Self { query, sort, order }
}
}
impl GeoffreyParam for SellingParams {}