use std::{error, fmt, str::FromStr};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub enum Strand {
Forward,
Reverse,
}
impl AsRef<str> for Strand {
fn as_ref(&self) -> &str {
match self {
Self::Forward => "+",
Self::Reverse => "-",
}
}
}
impl fmt::Display for Strand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_ref())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
Empty,
Invalid,
}
impl error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("empty input"),
Self::Invalid => f.write_str("invalid input"),
}
}
}
impl FromStr for Strand {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"" => Err(ParseError::Empty),
"+" => Ok(Self::Forward),
"-" => Ok(Self::Reverse),
_ => Err(ParseError::Invalid),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
assert_eq!(Strand::Forward.to_string(), "+");
assert_eq!(Strand::Reverse.to_string(), "-");
}
#[test]
fn test_from_str() {
assert_eq!("+".parse(), Ok(Strand::Forward));
assert_eq!("-".parse(), Ok(Strand::Reverse));
assert_eq!("".parse::<Strand>(), Err(ParseError::Empty));
assert_eq!("ndls".parse::<Strand>(), Err(ParseError::Invalid));
}
}