noodles_fasta/record/
definition.rsuse std::{
error, fmt,
str::{self, FromStr},
};
const PREFIX: char = '>';
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Definition {
name: Vec<u8>,
description: Option<Vec<u8>>,
}
impl Definition {
pub fn new<N>(name: N, description: Option<Vec<u8>>) -> Self
where
N: Into<Vec<u8>>,
{
Self {
name: name.into(),
description,
}
}
pub fn name(&self) -> &[u8] {
&self.name
}
pub fn description(&self) -> Option<&[u8]> {
self.description.as_deref()
}
}
impl fmt::Display for Definition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = str::from_utf8(self.name()).map_err(|_| fmt::Error)?;
write!(f, "{PREFIX}{name}")?;
if let Some(buf) = self.description() {
let description = str::from_utf8(buf).map_err(|_| fmt::Error)?;
write!(f, " {description}")?;
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ParseError {
Empty,
MissingPrefix,
MissingName,
}
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::MissingPrefix => write!(f, "missing prefix ('{PREFIX}')"),
Self::MissingName => f.write_str("missing name"),
}
}
}
impl FromStr for Definition {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Err(ParseError::Empty);
} else if !s.starts_with(PREFIX) {
return Err(ParseError::MissingPrefix);
}
let line = &s[1..];
let mut components = line.splitn(2, |c: char| c.is_ascii_whitespace());
let name = components
.next()
.and_then(|s| if s.is_empty() { None } else { Some(s.into()) })
.ok_or(ParseError::MissingName)?;
let description = components.next().map(|s| s.trim().into());
Ok(Self { name, description })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
let definition = Definition::new("sq0", None);
assert_eq!(definition.to_string(), ">sq0");
let definition = Definition::new("sq0", Some(Vec::from("LN:13")));
assert_eq!(definition.to_string(), ">sq0 LN:13");
}
#[test]
fn test_from_str() {
assert_eq!(">sq0".parse(), Ok(Definition::new("sq0", None)));
assert_eq!(
">sq0 LN:13".parse(),
Ok(Definition::new("sq0", Some(Vec::from("LN:13"))))
);
assert_eq!("".parse::<Definition>(), Err(ParseError::Empty));
assert_eq!("sq0".parse::<Definition>(), Err(ParseError::MissingPrefix));
assert_eq!(">".parse::<Definition>(), Err(ParseError::MissingName));
}
}