Skip to content

Commit

Permalink
gff/lazy/record/attributes: Add lookup by tag
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Oct 16, 2023
1 parent 653568a commit 64dfdc5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions noodles-gff/src/lazy/record/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ impl<'a> Attributes<'a> {
self.0.is_empty()
}

/// Returns the value of the given tag.
pub fn get(&self, tag: &str) -> Option<io::Result<&str>> {
for result in self.iter() {
match result {
Ok((t, value)) => {
if t == tag {
return Some(Ok(value));
}
}
Err(e) => return Some(Err(e)),
}
}

None
}

/// Returns an iterator over all tag-value pairs.
pub fn iter(&self) -> impl Iterator<Item = io::Result<(&str, &str)>> {
let mut src = self.0;
Expand Down Expand Up @@ -65,6 +81,13 @@ mod tests {
assert!(!attributes.is_empty());
}

#[test]
fn test_get() {
let attributes = Attributes::new("gene_id=ndls0;gene_name=gene0");
assert!(attributes.get("gene_name").is_some());
assert!(attributes.get("comment").is_none());
}

#[test]
fn test_iter() -> io::Result<()> {
let attributes = Attributes::new("");
Expand Down

0 comments on commit 64dfdc5

Please sign in to comment.