unobtanium 3.0.0

Opinioated Web search engine library with crawler and viewer companion.
Documentation
use serde::{Serialize,Deserialize};

use std::str::FromStr;

/// Describes where in a Document the information comes from
/// or how the information is related to the document it is attached to
#[derive(Debug,Clone,Copy,PartialEq,Eq,Serialize,Deserialize)]
pub struct InformationSource(i16);

impl InformationSource {

	/// The source is officially unknown
	///
	/// Don't use this value as a substitute for codes your code does not know.
	pub const UNKNOWN: InformationSource = InformationSource(0);

	/// The information came from a response header
	pub const RESPONSE_HEADER: InformationSource = InformationSource(1);

	/// The information came fram the metadata section of a file
	pub const FILE_METADATA: InformationSource = InformationSource(2);

	/// The information came from the body or content section of a file
	pub const FILE_BODY: InformationSource = InformationSource(3);

	/// The information is a summary derived from other data
	pub const SUMMARY: InformationSource = InformationSource(4);
}

impl ToString for InformationSource {
	/// returns the enum names in snake case
	fn to_string(&self) -> String {
		match *self {
			Self::UNKNOWN => "unknown",
			Self::RESPONSE_HEADER => "response_header",
			Self::FILE_METADATA => "file_metadata",
			Self::FILE_BODY => "file_body",
			Self::SUMMARY => "summary",
			_ => { return self.0.to_string(); }
		}.to_string()
	}
}

impl FromStr for InformationSource {
	type Err = &'static str;
	
	fn from_str(s: &str) -> Result<Self, Self::Err> {
		match s {
			"unknown" => Ok(Self::UNKNOWN),
			"response_header" => Ok(Self::RESPONSE_HEADER),
			"file_metadata" => Ok(Self::FILE_METADATA),
			"file_body" => Ok(Self::FILE_BODY),
			"summary" => Ok(Self::SUMMARY),
			_ => {
				if let Ok(code) = i16::from_str(s) {
					Ok(Self(code))
				} else {
					Err("Not a recognized information source! Make sure it is in lower_snake_case or number in the i16 range.")
				}
			},

		}
	}
}

impl Default for InformationSource {
	fn default() -> Self {
		Self::UNKNOWN
	}
}

impl From<InformationSource> for i16 {
	fn from(information_source: InformationSource) -> i16 {
		information_source.0
	}
}

impl From<i16> for InformationSource {
	fn from(n: i16) -> Self {
		Self(n)
	}
}

impl InformationSource {
	/// Convert an i16 to an information source
	pub fn from_number(n: i16) -> Self {
		n.into()
	}

	/// Convert an exit code to an i16
	pub fn to_number(self) -> i16 {
		self.into()
	}
}