use util::{ErrorKind, Ntoa, ResultType};
use std::mem;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum ProteinEvidence {
ProteinLevel = 1,
TranscriptLevel = 2,
Inferred = 3,
Predicted = 4,
#[doc(hidden)]
Unknown = 5,
}
impl ProteinEvidence {
const PROTEIN_LEVEL_VERBOSE: &'static str = "Evidence at protein level";
const TRANSCRIPT_LEVEL_VERBOSE: &'static str = "Evidence at transcript level";
const INFERRED_LEVEL_VERBOSE: &'static str = "Inferred from homology";
const PREDICTED_LEVEL_VERBOSE: &'static str = "Predicted";
const UNKNOWN_LEVEL_VERBOSE: &'static str = "";
const MIN: u8 = 1;
const MAX: u8 = 5;
#[inline]
pub fn verbose(&self) -> &'static str {
match self {
ProteinEvidence::ProteinLevel => Self::PROTEIN_LEVEL_VERBOSE,
ProteinEvidence::TranscriptLevel => Self::TRANSCRIPT_LEVEL_VERBOSE,
ProteinEvidence::Inferred => Self::INFERRED_LEVEL_VERBOSE,
ProteinEvidence::Predicted => Self::PREDICTED_LEVEL_VERBOSE,
ProteinEvidence::Unknown => Self::UNKNOWN_LEVEL_VERBOSE,
}
}
#[inline]
pub fn from_verbose(text: &str) -> ResultType<Self> {
match text {
Self::PROTEIN_LEVEL_VERBOSE => Ok(ProteinEvidence::ProteinLevel),
Self::TRANSCRIPT_LEVEL_VERBOSE => Ok(ProteinEvidence::TranscriptLevel),
Self::INFERRED_LEVEL_VERBOSE => Ok(ProteinEvidence::Inferred),
Self::PREDICTED_LEVEL_VERBOSE => Ok(ProteinEvidence::Predicted),
Self::UNKNOWN_LEVEL_VERBOSE => Ok(ProteinEvidence::Unknown),
_ => Err(From::from(ErrorKind::InvalidEnumeration)),
}
}
#[inline]
pub fn to_int(&self) -> u8 {
*self as u8
}
#[inline]
pub fn from_int(int: u8) -> ResultType<Self> {
if int >= Self::MIN && int <= Self::MAX {
Ok(unsafe { mem::transmute(int) })
} else {
Err(From::from(ErrorKind::InvalidEnumeration))
}
}
#[inline(always)]
pub fn to_string(&self) -> String {
self.to_int().to_string()
}
#[inline(always)]
pub fn from_str(s: &str) -> ResultType<Self> {
ProteinEvidence::from_int(s.parse::<u8>()?)
}
}
#[cfg(feature = "xml")]
impl ProteinEvidence {
const PROTEIN_LEVEL_XML_VERBOSE: &'static str = "evidence at protein level";
const TRANSCRIPT_LEVEL_XML_VERBOSE: &'static str = "evidence at transcript level";
const INFERRED_LEVEL_XML_VERBOSE: &'static str = "inferred from homology";
const PREDICTED_LEVEL_XML_VERBOSE: &'static str = "predicted";
const UNKNOWN_LEVEL_XML_VERBOSE: &'static str = "";
#[inline]
pub fn xml_verbose(&self) -> &'static str {
match self {
ProteinEvidence::ProteinLevel => Self::PROTEIN_LEVEL_XML_VERBOSE,
ProteinEvidence::TranscriptLevel => Self::TRANSCRIPT_LEVEL_XML_VERBOSE,
ProteinEvidence::Inferred => Self::INFERRED_LEVEL_XML_VERBOSE,
ProteinEvidence::Predicted => Self::PREDICTED_LEVEL_XML_VERBOSE,
ProteinEvidence::Unknown => Self::UNKNOWN_LEVEL_XML_VERBOSE,
}
}
#[inline]
pub fn from_xml_verbose(text: &str) -> ResultType<Self> {
match text {
Self::PROTEIN_LEVEL_XML_VERBOSE => Ok(ProteinEvidence::ProteinLevel),
Self::TRANSCRIPT_LEVEL_XML_VERBOSE => Ok(ProteinEvidence::TranscriptLevel),
Self::INFERRED_LEVEL_XML_VERBOSE => Ok(ProteinEvidence::Inferred),
Self::PREDICTED_LEVEL_XML_VERBOSE => Ok(ProteinEvidence::Predicted),
Self::UNKNOWN_LEVEL_XML_VERBOSE => Ok(ProteinEvidence::Unknown),
_ => Err(From::from(ErrorKind::InvalidEnumeration)),
}
}
}
impl Ntoa for ProteinEvidence {
#[inline(always)]
fn ntoa(&self) -> ResultType<String> {
self.to_int().ntoa()
}
#[inline(always)]
fn ntoa_with_capacity(&self, capacity: usize) -> ResultType<String> {
self.to_int().ntoa_with_capacity(capacity)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_protein_evidence_test() {
let text = format!("{:?}", ProteinEvidence::ProteinLevel);
assert_eq!(text, "ProteinLevel");
let text = format!("{:?}", ProteinEvidence::TranscriptLevel);
assert_eq!(text, "TranscriptLevel");
let text = format!("{:?}", ProteinEvidence::Inferred);
assert_eq!(text, "Inferred");
let text = format!("{:?}", ProteinEvidence::Predicted);
assert_eq!(text, "Predicted");
}
#[test]
fn protein_evidence_verbose_test() {
let text = ProteinEvidence::ProteinLevel.verbose();
assert_eq!(text, "Evidence at protein level");
let text = ProteinEvidence::TranscriptLevel.verbose();
assert_eq!(text, "Evidence at transcript level");
let text = ProteinEvidence::Inferred.verbose();
assert_eq!(text, "Inferred from homology");
let text = ProteinEvidence::Predicted.verbose();
assert_eq!(text, "Predicted");
}
#[cfg(feature = "xml")]
#[test]
fn protein_evidence_xml_verbose_test() {
let text = ProteinEvidence::ProteinLevel.xml_verbose();
assert_eq!(text, "evidence at protein level");
let text = ProteinEvidence::TranscriptLevel.xml_verbose();
assert_eq!(text, "evidence at transcript level");
let text = ProteinEvidence::Inferred.xml_verbose();
assert_eq!(text, "inferred from homology");
let text = ProteinEvidence::Predicted.xml_verbose();
assert_eq!(text, "predicted");
}
fn serialize_protein_evidence(evidence: ProteinEvidence, expected: &str) {
let text = evidence.to_string();
assert_eq!(text, expected);
let result = ProteinEvidence::from_str(&text).unwrap();
assert_eq!(result, evidence);
let text = evidence.ntoa().unwrap();
assert_eq!(text, expected);
}
#[test]
fn serialize_protein_evidence_test() {
serialize_protein_evidence(ProteinEvidence::ProteinLevel, "1");
serialize_protein_evidence(ProteinEvidence::TranscriptLevel, "2");
serialize_protein_evidence(ProteinEvidence::Inferred, "3");
serialize_protein_evidence(ProteinEvidence::Predicted, "4");
}
}