use criterium::number::AsInteger;
use serde::{Serialize,Deserialize};
use std::str::FromStr;
#[derive(Debug,PartialEq,Eq,Clone,Serialize,Deserialize)]
pub struct CrawlType(i64);
impl CrawlType {
pub const FILE_CRAWL: CrawlType = CrawlType(0);
pub const ROBOTSTXT_FETCH: CrawlType = CrawlType(1);
pub const METADATA_CRAWL: CrawlType = CrawlType(2);
}
impl ToString for CrawlType {
fn to_string(&self) -> String {
match *self {
Self::FILE_CRAWL => "file_crawl",
Self::ROBOTSTXT_FETCH => "robotstxt_fetch",
Self::METADATA_CRAWL => "metadata_crawl",
_ => { return self.0.to_string(); }
}.to_string()
}
}
impl FromStr for CrawlType {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"file_crawl" => Ok(Self::FILE_CRAWL),
"robotstxt_fetch" => Ok(Self::ROBOTSTXT_FETCH),
"metadata_crawl" => Ok(Self::METADATA_CRAWL),
_ => {
if let Ok(code) = i64::from_str(s) {
Ok(Self(code))
} else {
Err("Not a recognized crawl type! Make sure it is in lower_snake_case or number in the i64 range.")
}
},
}
}
}
impl From<CrawlType> for i64 {
fn from(crawl_type: CrawlType) -> i64 {
crawl_type.0
}
}
impl From<i64> for CrawlType {
fn from(n: i64) -> Self {
Self(n)
}
}
impl CrawlType {
pub fn from_number(n: i64) -> Self {
n.into()
}
pub fn to_number(self) -> i64 {
self.into()
}
}
impl AsInteger for CrawlType {
fn as_criterium_i64(&self) -> i64 {
self.0
}
}