use serde::{Serialize,Deserialize};
use std::str::FromStr;
#[derive(PartialEq,Eq,Clone,Serialize,Deserialize)]
pub struct SchedulingReason(u8);
impl SchedulingReason {
pub const MANUAL: SchedulingReason = SchedulingReason(0);
pub const EXPLORE: SchedulingReason = SchedulingReason(1);
pub const RECRAWL: SchedulingReason = SchedulingReason(2);
pub const FEED: SchedulingReason = SchedulingReason(3);
pub const FLUKE_RETRY: SchedulingReason = SchedulingReason(4);
}
impl ToString for SchedulingReason {
fn to_string(&self) -> String {
match *self {
Self::MANUAL => "manual",
Self::EXPLORE => "explore",
Self::RECRAWL => "recrawl",
Self::FEED => "feed",
Self::FLUKE_RETRY => "fluke_retry",
_ => { return self.0.to_string(); }
}.to_string()
}
}
impl FromStr for SchedulingReason {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"manual" => Ok(Self::MANUAL),
"explore" => Ok(Self::EXPLORE),
"recrawl" => Ok(Self::RECRAWL),
"feed" => Ok(Self::FEED),
"fluke_retry" => Ok(Self::FLUKE_RETRY),
_ => {
if let Ok(code) = u8::from_str(s) {
Ok(Self(code))
} else {
Err("Not a recognized scheduling reason. Make sure it is in lower_snake_case or number in the u8 range.")
}
},
}
}
}
impl From<SchedulingReason> for u8 {
fn from(reason: SchedulingReason) -> u8 {
reason.0
}
}
impl From<u8> for SchedulingReason {
fn from(n: u8) -> Self {
Self(n)
}
}
impl SchedulingReason {
pub fn from_number(n: u8) -> Self {
n.into()
}
pub fn to_number(self) -> u8 {
self.into()
}
}