From d7150f6e96de1e63318b6f1bdb41eecb2ac62e8d Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Thu, 14 Dec 2023 11:22:34 -0600 Subject: [PATCH] sam/lazy/record/cigar: Implement crate::alignment::record::Cigar for Cigar --- noodles-sam/src/lazy/record/cigar.rs | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/noodles-sam/src/lazy/record/cigar.rs b/noodles-sam/src/lazy/record/cigar.rs index 99a2299a5..344c89683 100644 --- a/noodles-sam/src/lazy/record/cigar.rs +++ b/noodles-sam/src/lazy/record/cigar.rs @@ -32,6 +32,36 @@ impl<'a> Cigar<'a> { } } +impl<'a> crate::alignment::record::Cigar for Cigar<'a> { + fn is_empty(&self) -> bool { + self.is_empty() + } + + fn len(&self) -> usize { + self.as_ref() + .iter() + .filter(|&b| { + matches!( + b, + b'M' | b'I' | b'D' | b'N' | b'S' | b'H' | b'P' | b'=' | b'X' + ) + }) + .count() + } + + fn iter(&self) -> Box> + '_> { + Box::new(self.iter().map(|result| { + result + .map(|op| { + // SAFETY: `char::from(op)` only returns ASCII characters. + let raw_op = char::from(op.kind()) as u8; + (raw_op, op.len()) + }) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) + })) + } +} + impl<'a> AsRef<[u8]> for Cigar<'a> { fn as_ref(&self) -> &[u8] { self.0