unobtanium 3.0.0

Opinioated Web search engine library with crawler and viewer companion.
Documentation

//! Crawler SchedulingReason, Datastructure and Constants.

use serde::{Serialize,Deserialize};

use std::str::FromStr;

/// Enumerates reasons why something ended up in the scheduling log.
#[derive(PartialEq,Eq,Clone,Serialize,Deserialize)]
pub struct SchedulingReason(u8);

impl SchedulingReason {
	/// Scheduling happened manually
	pub const MANUAL: SchedulingReason = SchedulingReason(0);

	/// Scheduling happend through an automatic exploration algorithm
	pub const EXPLORE: SchedulingReason = SchedulingReason(1);

	/// Scheduled because the resource was due for a recrawl
	pub const RECRAWL: SchedulingReason = SchedulingReason(2);

	/// Scheduled because an update was announced through a feed
	pub const FEED: SchedulingReason = SchedulingReason(3);

	/// Scheduled because the previous attempt at fetching this
	/// failed because of reasons outside the control of both
	/// the crawlers admin and the crawled website.
	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()
	}
}