Skip to content

Commit 797965c

Browse files
authored
feat: move empty_record to header (#453)
* move empty_record to header this makes it so we don't need access to the VCF struct in order to make an empty record. since the header is always available as record.header() then we can create an empty record * wider thiserror
1 parent d9fe03a commit 797965c

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ newtype_derive = "0.1"
3030
regex = "1.3"
3131
serde = {version = "^1", optional = true, features = ["derive"]}
3232
serde_bytes = {version = "0.11", optional = true}
33-
thiserror = "2"
33+
thiserror = {version = "^2" }
3434
url = "2.1"
3535

3636
[features]

src/bcf/header.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
3535
use std::ffi;
3636
use std::os::raw::c_char;
37+
use std::rc::Rc;
3738
use std::slice;
3839
use std::str;
3940

@@ -506,6 +507,13 @@ impl HeaderView {
506507
}
507508
result
508509
}
510+
511+
/// Create an empty record using this header view.
512+
///
513+
/// The record can be reused multiple times.
514+
pub fn empty_record(&self) -> crate::bcf::Record {
515+
crate::bcf::Record::new(Rc::new(self.clone()))
516+
}
509517
}
510518

511519
impl Clone for HeaderView {
@@ -540,3 +548,25 @@ pub enum TagLength {
540548
Genotypes,
541549
Variable,
542550
}
551+
552+
#[cfg(test)]
553+
mod tests {
554+
use super::*;
555+
use crate::bcf::Reader;
556+
557+
#[test]
558+
fn test_header_view_empty_record() {
559+
// Open a VCF file to get a HeaderView
560+
let vcf = Reader::from_path("test/test_string.vcf").expect("Error opening file");
561+
let header_view = vcf.header.clone();
562+
563+
// Create an empty record from the HeaderView
564+
let record = header_view.empty_record();
565+
eprintln!("{:?}", record.rid());
566+
567+
// Verify the record is properly initialized with default/empty values
568+
assert_eq!(record.rid(), Some(0)); // No chromosome/contig set
569+
assert_eq!(record.pos(), 0); // No position set
570+
assert_eq!(record.qual(), 0.0); // No quality score set
571+
}
572+
}

src/bcf/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl Read for Reader {
237237

238238
/// Return empty record. Can be reused multiple times.
239239
fn empty_record(&self) -> Record {
240-
Record::new(Rc::clone(&self.header))
240+
self.header.empty_record()
241241
}
242242
}
243243

0 commit comments

Comments
 (0)