Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde = {version = "^1", optional = true, features = ["derive"]}
serde_bytes = {version = "0.11", optional = true}
thiserror = {version = "^2" }
url = "2.5"
cstr8 = "0.1.4"

[features]
bindgen = ["hts-sys/bindgen"]
Expand Down
10 changes: 4 additions & 6 deletions src/bcf/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use std::str;

use crate::htslib;

use cstr8::CStr8;
use linear_map::LinearMap;

use crate::errors::{Error, Result};
Expand Down Expand Up @@ -398,17 +399,14 @@ impl HeaderView {
}

/// Convert string ID (e.g., for a `FILTER` value) to its numeric identifier.
pub fn name_to_id(&self, id: &[u8]) -> Result<Id> {
let c_str = ffi::CString::new(id).unwrap();
pub fn name_to_id(&self, id: &CStr8) -> Result<Id> {
unsafe {
match htslib::bcf_hdr_id2int(
self.inner,
htslib::BCF_DT_ID as i32,
c_str.as_ptr() as *const c_char,
id.as_ptr() as *const c_char,
) {
-1 => Err(Error::BcfUnknownID {
id: str::from_utf8(id).unwrap().to_owned(),
}),
-1 => Err(Error::BcfUnknownID { id: id.into() }),
i => Ok(Id(i as u32)),
}
}
Expand Down
27 changes: 17 additions & 10 deletions src/bcf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ fn bcf_open(target: &[u8], mode: &[u8]) -> Result<*mut htslib::htsFile> {

#[cfg(test)]
mod tests {
use cstr8::cstr8;
use tempfile::NamedTempFile;

use super::record::Buffer;
Expand Down Expand Up @@ -1222,8 +1223,8 @@ mod tests {
use crate::bcf::header::Id;

assert_eq!(header.id_to_name(Id(4)), b"GT");
assert_eq!(header.name_to_id(b"GT").unwrap(), Id(4));
assert!(header.name_to_id(b"XX").is_err());
assert_eq!(header.name_to_id(cstr8!("GT")).unwrap(), Id(4));
assert!(header.name_to_id(cstr8!("XX")).is_err());
}

#[test]
Expand Down Expand Up @@ -1427,10 +1428,12 @@ mod tests {

record.set_qual(10.0);

record.push_info_integer(b"N1", &[32]).unwrap();
record.push_info_float(b"F1", &[33.0]).unwrap();
record.push_info_string(b"S1", &[b"fourtytwo"]).unwrap();
record.push_info_flag(b"X1").unwrap();
record.push_info_integer(cstr8!("N1"), &[32]).unwrap();
record.push_info_float(cstr8!("F1"), &[33.0]).unwrap();
record
.push_info_string(cstr8!("S1"), &[b"fourtytwo"])
.unwrap();
record.push_info_flag(cstr8!("X1")).unwrap();

record
.push_genotypes(&[
Expand All @@ -1442,12 +1445,16 @@ mod tests {
.unwrap();

record
.push_format_string(b"FS1", &[&b"yes"[..], &b"no"[..]])
.push_format_string(cstr8!("FS1"), &[&b"yes"[..], &b"no"[..]])
.unwrap();
record
.push_format_integer(cstr8!("FF1"), &[43, 11])
.unwrap();
record
.push_format_float(cstr8!("FN1"), &[42.0, 10.0])
.unwrap();
record.push_format_integer(b"FF1", &[43, 11]).unwrap();
record.push_format_float(b"FN1", &[42.0, 10.0]).unwrap();
record
.push_format_char(b"CH1", &[b"A"[0], b"B"[0]])
.push_format_char(cstr8!("CH1"), &[b"A"[0], b"B"[0]])
.unwrap();

// Finally, write out the record.
Expand Down
Loading
Loading