unobtanium 3.0.0

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

//! CrawlType, Datastructure and Constants.
//!
//! The CrawlType is represented as an integer, currently they are registered
//! by being declared as a constant in this module.

use criterium::number::AsInteger;
use serde::{Serialize,Deserialize};

use std::str::FromStr;

#[derive(Debug,PartialEq,Eq,Clone,Serialize,Deserialize)]
pub struct CrawlType(i64);

/// Enumerates possible types of crawling supported by unobtanium.
impl CrawlType {
	/// Represents a regular file crawl, that attempts some general purpose indexing.
	pub const FILE_CRAWL: CrawlType = CrawlType(0);

	/// Unlikely to be scheduled, but used to log the fact that an agent
	/// downloaded the robots.txt file.
	pub const ROBOTSTXT_FETCH: CrawlType = CrawlType(1);

	/// This crawltype is the quivalent to an http `HEAD` request,
	/// useful to find out wheter a link is still alive without
	/// triggering full indexing.
	///
	/// Can be done even if the robots.txt disapproves.
	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",
			// just convert to a number.
			_ => { 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
	}
}