Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Display trait for bcf::Record #443

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions src/bcf/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,25 @@ unsafe impl Send for Record {}

unsafe impl Sync for Record {}

impl fmt::Display for Record {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = htslib::kstring_t {
l: 0,
m: 0,
s: ptr::null_mut(),
};
let vcf_str = unsafe {
htslib::vcf_format(self.header().inner, self.inner, &mut buf);
let vcf_str = String::from(ffi::CStr::from_ptr(buf.s).to_str().unwrap());
if !buf.s.is_null() {
libc::free(buf.s as *mut libc::c_void);
}
vcf_str
};
write!(f, "{vcf_str}")
}
}

/// Info tag representation.
#[derive(Debug)]
pub struct Info<'a, B: BorrowMut<Buffer> + Borrow<Buffer>> {
Expand Down Expand Up @@ -1714,4 +1733,18 @@ mod tests {
assert!(!record.has_filter(&bar));
assert!(record.has_filter("PASS".as_bytes()));
}

#[test]
fn test_record_display_trait() {
let tmp = NamedTempFile::new().unwrap();
let path = tmp.path();
let mut header = Header::new();
header.push_record(b"##contig=<ID=chr1,length=1000>");
header.push_record(br#"##FILTER=<ID=foo,Description="sample is a foo fighter">"#);
let vcf = Writer::from_path(path, &header, true, Format::Vcf).unwrap();
let mut record = vcf.empty_record();
record.push_filter("foo".as_bytes()).unwrap();
let s = format!("{record}");
assert_eq!(s, "chr1\t1\t.\t.\t.\t0\tfoo\t.\n");
}
}
Loading