use util::{ErrorKind, Ntoa, ResultType};
use std::mem;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Section {
TrEMBL = 0,
SwissProt = 1,
#[doc(hidden)]
Unknown = 2
}
impl Section {
const MIN: u8 = 0;
const MAX: u8 = 2;
#[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> {
Section::from_int(s.parse::<u8>()?)
}
}
impl Ntoa for Section {
#[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_section_test() {
let text = format!("{:?}", Section::TrEMBL);
assert_eq!(text, "TrEMBL");
let text = format!("{:?}", Section::SwissProt);
assert_eq!(text, "SwissProt");
}
fn serialize_section(section: Section, expected: &str) {
let text = section.to_string();
assert_eq!(text, expected);
let result = Section::from_str(&text).unwrap();
assert_eq!(result, section);
let text = section.ntoa().unwrap();
assert_eq!(text, expected);
}
#[test]
fn serialize_section_test() {
serialize_section(Section::TrEMBL, "0");
serialize_section(Section::SwissProt, "1");
}
}