Skip to content

Commit

Permalink
vcf/record/samples/series: Implement crate::variant::record::samples:…
Browse files Browse the repository at this point in the history
…:Series
  • Loading branch information
zaeleus committed Feb 22, 2024
1 parent 722f618 commit d2e87d7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
38 changes: 25 additions & 13 deletions noodles-vcf/src/record/samples/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,49 @@ use std::io;
use super::Samples;
use crate::{variant::record::samples::series::Value, Header};

pub struct Series<'a> {
name: &'a str,
samples: &'a Samples<'a>,
pub struct Series<'r> {
name: &'r str,
samples: &'r Samples<'r>,
i: usize,
}

impl<'a> Series<'a> {
pub(super) fn new(name: &'a str, samples: &'a Samples<'a>, i: usize) -> Self {
impl<'r> Series<'r> {
pub(super) fn new(name: &'r str, samples: &'r Samples<'r>, i: usize) -> Self {
Self { name, samples, i }
}

/// Returns the name.
pub fn name(&self) -> &str {
self.name
}

/// Returns the value at the given index.
pub fn get<'h: 'a>(
pub fn get<'h: 'r>(
&self,
header: &'h Header,
i: usize,
) -> Option<Option<io::Result<Value<'a>>>> {
) -> Option<Option<io::Result<Value<'r>>>> {
let sample = self.samples.iter().nth(i)??;
sample.get_index(header, self.i)
}
}

impl<'r> crate::variant::record::samples::Series for Series<'r> {
fn name(&self) -> &str {
self.name
}

fn iter<'a, 'h: 'a>(
&'a self,
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<Option<Value<'a>>>> + 'a> {
Box::new(self.samples.iter().map(|sample| {
sample
.and_then(|s| s.get_index(header, self.i)?)
.transpose()
}))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::variant::record_buf::samples::keys::key;
use crate::variant::{record::samples::Series as _, record_buf::samples::keys::key};

#[test]
fn test_name() {
Expand Down
6 changes: 5 additions & 1 deletion noodles-vcf/src/variant/record/samples/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ pub mod value;
use std::io;

pub use self::value::Value;
use crate::Header;

/// A variant record samples series.
pub trait Series {
/// Returns the name.
fn name(&self) -> &str;

/// Returns an iterator over values.
fn iter(&self) -> Box<dyn Iterator<Item = io::Result<Option<Value<'_>>>> + '_>;
fn iter<'a, 'h: 'a>(
&'a self,
header: &'h Header,
) -> Box<dyn Iterator<Item = io::Result<Option<Value<'a>>>> + 'a>;
}
22 changes: 12 additions & 10 deletions noodles-vcf/src/variant/record_buf/samples/series.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::io;

use super::sample::Value;
use crate::Header;

/// A variant record samples buffer series.
pub struct Series<'a> {
name: &'a str,
values: &'a [Vec<Option<Value>>],
pub struct Series<'s> {
name: &'s str,
values: &'s [Vec<Option<Value>>],
i: usize,
}

impl<'a> Series<'a> {
pub(super) fn new(name: &'a str, values: &'a [Vec<Option<Value>>], i: usize) -> Self {
impl<'s> Series<'s> {
pub(super) fn new(name: &'s str, values: &'s [Vec<Option<Value>>], i: usize) -> Self {
Self { name, values, i }
}

Expand All @@ -22,16 +23,17 @@ impl<'a> Series<'a> {
}
}

impl<'a> crate::variant::record::samples::Series for Series<'a> {
impl<'s> crate::variant::record::samples::Series for Series<'s> {
fn name(&self) -> &str {
self.name
}

fn iter(
&self,
fn iter<'a, 'h: 'a>(
&'a self,
_: &'h Header,
) -> Box<
dyn Iterator<Item = io::Result<Option<crate::variant::record::samples::series::Value<'_>>>>
+ '_,
dyn Iterator<Item = io::Result<Option<crate::variant::record::samples::series::Value<'a>>>>
+ 'a,
> {
Box::new(self.values.iter().map(|sample| {
Ok(sample
Expand Down

0 comments on commit d2e87d7

Please sign in to comment.