-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vcf/record: Add alternate bases wrapper
- Loading branch information
Showing
3 changed files
with
56 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::fmt; | ||
|
||
/// VCF record alternate bases. | ||
pub struct AlternateBases<'a>(&'a str); | ||
|
||
const DELIMITER: char = ','; | ||
|
||
impl<'a> AlternateBases<'a> { | ||
pub(super) fn new(src: &'a str) -> Self { | ||
Self(src) | ||
} | ||
|
||
/// Returns whether there are any alternate bases. | ||
pub fn is_empty(&self) -> bool { | ||
self.0.is_empty() | ||
} | ||
|
||
/// Returns the number of alternate bases. | ||
pub fn len(&self) -> usize { | ||
if self.is_empty() { | ||
0 | ||
} else { | ||
self.iter().count() | ||
} | ||
} | ||
|
||
/// Returns an iterator over alternate bases. | ||
pub fn iter(&self) -> impl Iterator<Item = &'a str> + '_ { | ||
self.0.split(DELIMITER) | ||
} | ||
} | ||
|
||
impl<'a> fmt::Debug for AlternateBases<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_list().entries(self.iter()).finish() | ||
} | ||
} | ||
|
||
impl<'a> AsRef<str> for AlternateBases<'a> { | ||
fn as_ref(&self) -> &str { | ||
self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters