From e06fc33dfeef7f58387e667fd4e8ae81b882b70f Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Wed, 21 Feb 2024 14:12:45 -0600 Subject: [PATCH] vcf/variant/record_buf/samples/series: Implement crate::variant::record::samples::Series --- .../src/variant/record/samples/series.rs | 2 +- .../src/variant/record_buf/samples/series.rs | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/noodles-vcf/src/variant/record/samples/series.rs b/noodles-vcf/src/variant/record/samples/series.rs index f9ab3c9de..16976d87c 100644 --- a/noodles-vcf/src/variant/record/samples/series.rs +++ b/noodles-vcf/src/variant/record/samples/series.rs @@ -12,5 +12,5 @@ pub trait Series { fn name(&self) -> &str; /// Returns an iterator over values. - fn iter(&self) -> Box>> + '_>; + fn iter(&self) -> Box>>> + '_>; } diff --git a/noodles-vcf/src/variant/record_buf/samples/series.rs b/noodles-vcf/src/variant/record_buf/samples/series.rs index f75eacf89..26f1b0f86 100644 --- a/noodles-vcf/src/variant/record_buf/samples/series.rs +++ b/noodles-vcf/src/variant/record_buf/samples/series.rs @@ -1,3 +1,5 @@ +use std::io; + use super::sample::Value; /// A variant record samples buffer series. @@ -12,11 +14,6 @@ impl<'a> Series<'a> { Self { name, values, i } } - /// Returns the name. - pub fn name(&self) -> &str { - self.name - } - /// Returns the value at the given index. pub fn get(&self, i: usize) -> Option> { self.values @@ -25,11 +22,29 @@ impl<'a> Series<'a> { } } +impl<'a> crate::variant::record::samples::Series for Series<'a> { + fn name(&self) -> &str { + self.name + } + + fn iter( + &self, + ) -> Box< + dyn Iterator>>> + + '_, + > { + Box::new(self.values.iter().map(|sample| { + Ok(sample + .get(self.i) + .and_then(|value| value.as_ref().map(|v| v.into()))) + })) + } +} + #[cfg(test)] mod tests { - use crate::variant::record_buf::samples::keys::key; - use super::*; + use crate::variant::{record::samples::Series as _, record_buf::samples::keys::key}; #[test] fn test_name() {