noodles_vcf/header/record/
key.rspub mod other;
pub use self::other::Other;
use std::fmt;
pub const FILE_FORMAT: Key = Key::Standard(Standard::FileFormat);
pub const INFO: Key = Key::Standard(Standard::Info);
pub const FILTER: Key = Key::Standard(Standard::Filter);
pub const FORMAT: Key = Key::Standard(Standard::Format);
pub const ALTERNATIVE_ALLELE: Key = Key::Standard(Standard::AlternativeAllele);
pub const CONTIG: Key = Key::Standard(Standard::Contig);
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Standard {
FileFormat,
Info,
Filter,
Format,
AlternativeAllele,
Contig,
}
impl Standard {
fn new(s: &str) -> Option<Self> {
match s {
"fileformat" => Some(Self::FileFormat),
"INFO" => Some(Self::Info),
"FILTER" => Some(Self::Filter),
"FORMAT" => Some(Self::Format),
"ALT" => Some(Self::AlternativeAllele),
"contig" => Some(Self::Contig),
_ => None,
}
}
}
impl AsRef<str> for Standard {
fn as_ref(&self) -> &str {
match self {
Self::FileFormat => "fileformat",
Self::Info => "INFO",
Self::Filter => "FILTER",
Self::Format => "FORMAT",
Self::AlternativeAllele => "ALT",
Self::Contig => "contig",
}
}
}
impl fmt::Display for Standard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_ref().fmt(f)
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Key {
Standard(Standard),
Other(Other),
}
impl Key {
pub fn other(s: &str) -> Option<Other> {
match Self::from(s) {
Self::Standard(_) => None,
Self::Other(tag) => Some(tag),
}
}
}
impl AsRef<str> for Key {
fn as_ref(&self) -> &str {
match self {
Self::Standard(tag) => tag.as_ref(),
Self::Other(tag) => tag.as_ref(),
}
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_ref().fmt(f)
}
}
impl From<&str> for Key {
fn from(s: &str) -> Self {
match Standard::new(s) {
Some(tag) => Self::Standard(tag),
None => Self::Other(Other(s.into())),
}
}
}
impl From<String> for Key {
fn from(s: String) -> Self {
match Standard::new(&s) {
Some(tag) => Self::Standard(tag),
None => Self::Other(Other(s)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
assert_eq!(FILE_FORMAT.to_string(), "fileformat");
assert_eq!(INFO.to_string(), "INFO");
assert_eq!(FILTER.to_string(), "FILTER");
assert_eq!(FORMAT.to_string(), "FORMAT");
assert_eq!(ALTERNATIVE_ALLELE.to_string(), "ALT");
assert_eq!(CONTIG.to_string(), "contig");
assert_eq!(
Key::Other(Other(String::from("fileDate"))).to_string(),
"fileDate"
);
}
#[test]
fn test_from() {
assert_eq!(Key::from("fileformat"), FILE_FORMAT);
assert_eq!(Key::from("INFO"), INFO);
assert_eq!(Key::from("FILTER"), FILTER);
assert_eq!(Key::from("FORMAT"), FORMAT);
assert_eq!(Key::from("ALT"), ALTERNATIVE_ALLELE);
assert_eq!(Key::from("contig"), CONTIG);
assert_eq!(
Key::from("fileDate"),
Key::Other(Other(String::from("fileDate")))
);
}
}