Skip to content

Commit

Permalink
bcf/lazy/record/value: Borrow string value
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Sep 6, 2023
1 parent 8453545 commit 92bde94
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions noodles-bcf/src/lazy/record/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ mod ty;
pub use self::{array::Array, float::Float, int16::Int16, int32::Int32, int8::Int8, ty::Type};

#[derive(Clone, Debug, PartialEq)]
pub enum Value {
pub enum Value<'a> {
Int8(Option<Int8>),
Int16(Option<Int16>),
Int32(Option<Int32>),
Float(Option<Float>),
String(Option<String>),
String(Option<&'a str>),
Array(Array),
}
2 changes: 1 addition & 1 deletion noodles-bcf/src/record/codec/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn read_ref_alt(src: &mut &[u8], len: usize) -> io::Result<(ReferenceBases,

for _ in 0..len {
match read_value(src)? {
Some(Value::String(Some(s))) => alleles.push(s),
Some(Value::String(Some(s))) => alleles.push(s.into()),
Some(Value::String(None)) => alleles.push(String::from(".")),
v => {
return Err(io::Error::new(
Expand Down
18 changes: 12 additions & 6 deletions noodles-bcf/src/record/codec/decoder/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod ty;

pub use self::ty::read_type;

use std::io::{self, Read};
use std::{io, str};

use byteorder::{LittleEndian, ReadBytesExt};

Expand All @@ -11,7 +11,7 @@ use crate::lazy::record::{
Value,
};

pub fn read_value(src: &mut &[u8]) -> io::Result<Option<Value>> {
pub fn read_value<'a>(src: &mut &'a [u8]) -> io::Result<Option<Value<'a>>> {
let ty = read_type(src)?;

match ty {
Expand Down Expand Up @@ -111,10 +111,16 @@ fn read_float_array(src: &mut &[u8], len: usize) -> io::Result<Vec<f32>> {
Ok(buf)
}

fn read_string(src: &mut &[u8], len: usize) -> io::Result<String> {
let mut buf = vec![0; len];
src.read_exact(&mut buf)?;
String::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
fn read_string<'a>(src: &mut &'a [u8], len: usize) -> io::Result<&'a str> {
if src.len() < len {
return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
}

let (buf, rest) = src.split_at(len);
let s = str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
*src = rest;

Ok(s)
}

#[cfg(test)]
Expand Down
18 changes: 10 additions & 8 deletions noodles-bcf/src/record/codec/encoder/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ pub(crate) fn write_id<W>(writer: &mut W, ids: &vcf::record::Ids) -> io::Result<
where
W: Write,
{
let value = if ids.is_empty() {
Some(Value::String(None))
if ids.is_empty() {
let value = Some(Value::String(None));
write_value(writer, value)
} else {
Some(Value::String(Some(ids.to_string())))
};

write_value(writer, value)
let s = ids.to_string();
let value = Some(Value::String(Some(&s)));
write_value(writer, value)
}
}

pub(crate) fn write_ref_alt<W>(
Expand All @@ -198,12 +199,13 @@ where
W: Write,
{
let r#ref = reference_bases.to_string();
let ref_value = Some(Value::String(Some(r#ref)));
let ref_value = Some(Value::String(Some(&r#ref)));
write_value(writer, ref_value)?;

if !alternate_bases.is_empty() {
for allele in alternate_bases.iter() {
let alt_value = Some(Value::String(Some(allele.to_string())));
let alt = allele.to_string();
let alt_value = Some(Value::String(Some(&alt)));
write_value(writer, alt_value)?;
}
}
Expand Down
9 changes: 5 additions & 4 deletions noodles-bcf/src/record/codec/encoder/site/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ fn write_info_field_character_value<W>(writer: &mut W, c: char) -> io::Result<()
where
W: Write,
{
write_value(writer, Some(Value::String(Some(c.into()))))
let s = c.to_string();
write_value(writer, Some(Value::String(Some(&s))))
}

fn write_info_field_string_value<W>(writer: &mut W, s: &str) -> io::Result<()>
where
W: Write,
{
write_value(writer, Some(Value::String(Some(s.into()))))
write_value(writer, Some(Value::String(Some(s))))
}

fn write_info_field_integer_array_value<W>(writer: &mut W, values: &[Option<i32>]) -> io::Result<()>
Expand Down Expand Up @@ -297,7 +298,7 @@ where
s.push(c);
}

write_value(writer, Some(Value::String(Some(s))))
write_value(writer, Some(Value::String(Some(&s))))
}

fn write_info_field_string_array_value<W>(
Expand All @@ -321,7 +322,7 @@ where
}
}

write_value(writer, Some(Value::String(Some(s))))
write_value(writer, Some(Value::String(Some(&s))))
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions noodles-bcf/src/record/codec/encoder/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use byteorder::{LittleEndian, WriteBytesExt};

use crate::lazy::record::value::{Array, Float, Int16, Int32, Int8, Type, Value};

pub fn write_value<W>(writer: &mut W, value: Option<Value>) -> io::Result<()>
pub fn write_value<W>(writer: &mut W, value: Option<Value<'_>>) -> io::Result<()>
where
W: Write,
{
Expand Down Expand Up @@ -153,7 +153,7 @@ where
Ok(())
}

fn write_string<W>(writer: &mut W, value: Option<String>) -> io::Result<()>
fn write_string<W>(writer: &mut W, value: Option<&str>) -> io::Result<()>
where
W: Write,
{
Expand Down Expand Up @@ -238,11 +238,11 @@ mod tests {
assert_eq!(buf, [0x07]);

buf.clear();
write_value(&mut buf, Some(Value::String(Some(String::from("n")))))?;
write_value(&mut buf, Some(Value::String(Some("n"))))?;
assert_eq!(buf, [0x17, 0x6e]);

buf.clear();
write_value(&mut buf, Some(Value::String(Some(String::from("ndls")))))?;
write_value(&mut buf, Some(Value::String(Some("ndls"))))?;
assert_eq!(buf, [0x47, 0x6e, 0x64, 0x6c, 0x73]);

Ok(())
Expand Down

0 comments on commit 92bde94

Please sign in to comment.