use serde::{Serialize,Deserialize};
use std::str::FromStr;
#[derive(Debug,Clone,Copy,PartialEq,Eq,Serialize,Deserialize)]
pub struct InformationSource(i16);
impl InformationSource {
pub const UNKNOWN: InformationSource = InformationSource(0);
pub const RESPONSE_HEADER: InformationSource = InformationSource(1);
pub const FILE_METADATA: InformationSource = InformationSource(2);
pub const FILE_BODY: InformationSource = InformationSource(3);
pub const SUMMARY: InformationSource = InformationSource(4);
}
impl ToString for InformationSource {
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 {
pub fn from_number(n: i16) -> Self {
n.into()
}
pub fn to_number(self) -> i16 {
self.into()
}
}