Skip to main content

gen_models/annotations/
progress.rs

1//! Progress hooks used while preparing annotation files.
2
3use std::io::{self, Write};
4
5use super::AnnotationFileChecksumOverrides;
6
7/// A stage reported while preparing a local GFF3 or BED annotation file.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub enum AnnotationFileProgressStage {
10    /// Sort source records before creating offset-based indexes.
11    Sorting,
12    /// Compress the sorted source into the retained BGZF asset.
13    Compressing,
14    /// Build the searchable attribute index for the retained source.
15    BuildingIndex,
16}
17
18/// Options shared by annotation-file addition and its optional progress callback.
19pub struct AnnotationFileAddOptions<'a> {
20    /// Checksums collected while reading annotation assets.
21    pub checksum_overrides: AnnotationFileChecksumOverrides,
22    /// Called immediately before each local GFF3 or BED preparation stage.
23    pub progress: Option<&'a mut dyn FnMut(AnnotationFileProgressStage)>,
24}
25
26pub(super) fn report_annotation_file_progress(
27    progress: &mut Option<&mut dyn FnMut(AnnotationFileProgressStage)>,
28    stage: AnnotationFileProgressStage,
29) {
30    if let Some(callback) = progress.as_mut() {
31        callback(stage);
32    }
33}
34
35// GAI sorts all records before it emits sorted rows, so the first BGZF write marks the transition.
36pub(super) struct ReportCompressionOnFirstWrite<W, F> {
37    writer: W,
38    on_first_write: Option<F>,
39}
40
41impl<W, F> ReportCompressionOnFirstWrite<W, F> {
42    pub(super) fn new(writer: W, on_first_write: F) -> Self {
43        Self {
44            writer,
45            on_first_write: Some(on_first_write),
46        }
47    }
48}
49
50impl<W: Write, F: FnMut()> Write for ReportCompressionOnFirstWrite<W, F> {
51    fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
52        if !buffer.is_empty()
53            && let Some(mut on_first_write) = self.on_first_write.take()
54        {
55            on_first_write();
56        }
57
58        self.writer.write(buffer)
59    }
60
61    fn flush(&mut self) -> io::Result<()> {
62        self.writer.flush()
63    }
64}