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

101 lines
2.2 KiB
Rust

use crate::models::parameters::CommandRequest;
use crate::models::player::UserID;
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)]
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 token: String,
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 {
token: Default::default(),
query,
sort,
order,
}
}
}
impl CommandRequest for SellingParams {
fn token(&self) -> String {
self.token.clone()
}
fn user_id(&self) -> Option<UserID> {
None
}
fn set_token(&mut self, token: String) {
self.token = token;
}
}