pub mod entry;
pub use self::entry::Entry;
use std::{
error,
fmt::{self, Write},
ops::Deref,
str::FromStr,
};
const DELIMITER: char = ' ';
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Attributes(Vec<Entry>);
impl Deref for Attributes {
type Target = [Entry];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Vec<Entry>> for Attributes {
fn from(entries: Vec<Entry>) -> Self {
Self(entries)
}
}
impl fmt::Display for Attributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, entry) in self.0.iter().enumerate() {
write!(f, "{entry}")?;
f.write_char(entry::TERMINATOR)?;
if i < self.0.len() - 1 {
f.write_char(DELIMITER)?;
}
}
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
Empty,
Invalid,
InvalidEntry(entry::ParseError),
}
impl error::Error for ParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::InvalidEntry(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => write!(f, "empty input"),
Self::Invalid => write!(f, "invalid input"),
Self::InvalidEntry(_) => write!(f, "invalid entry"),
}
}
}
impl FromStr for Attributes {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Err(ParseError::Empty);
}
let s = s.strip_suffix(entry::TERMINATOR).unwrap_or(s);
let entries = s
.split(entry::TERMINATOR)
.map(|t| t.trim().parse().map_err(ParseError::InvalidEntry))
.collect::<Result<_, _>>()?;
Ok(Self(entries))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
let attributes = Attributes::from(vec![Entry::new("gene_id", "g0")]);
assert_eq!(attributes.to_string(), r#"gene_id "g0";"#);
let attributes = Attributes::from(vec![
Entry::new("gene_id", "g0"),
Entry::new("transcript_id", "t0"),
]);
assert_eq!(
attributes.to_string(),
r#"gene_id "g0"; transcript_id "t0";"#
);
}
#[test]
fn test_from_str() {
assert_eq!(
r#"gene_id "g0";"#.parse(),
Ok(Attributes::from(vec![Entry::new("gene_id", "g0")]))
);
assert_eq!(
r#"gene_id "g0""#.parse::<Attributes>(),
Ok(Attributes::from(vec![Entry::new("gene_id", "g0")]))
);
assert_eq!(
r#"gene_id "g0"; transcript_id "t0";"#.parse(),
Ok(Attributes::from(vec![
Entry::new("gene_id", "g0"),
Entry::new("transcript_id", "t0")
]))
);
assert_eq!(
r#"gene_id "g0";transcript_id "t0";"#.parse::<Attributes>(),
Ok(Attributes::from(vec![
Entry::new("gene_id", "g0"),
Entry::new("transcript_id", "t0")
]))
);
assert_eq!(
r#"gene_id "g0"; transcript_id "t0";"#.parse::<Attributes>(),
Ok(Attributes::from(vec![
Entry::new("gene_id", "g0"),
Entry::new("transcript_id", "t0")
]))
);
assert_eq!("".parse::<Attributes>(), Err(ParseError::Empty));
assert!(matches!(
r#";"#.parse::<Attributes>(),
Err(ParseError::InvalidEntry(_))
));
}
}