1#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
2use std::path::Path;
3
4use crate::{
5 ion::{
6 IonResult,
7 encoder::{ion_writer::IonEncoder, scan_stream::ScanStream, utilities::WriteBytes},
8 },
9 mzml::structs::{Chromatogram, MzML, Spectrum},
10};
11
12#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
13use crate::ion::encoder::utilities::FileWriter;
14
15use super::options::WriteOptions;
16
17enum Output<'out> {
18 Borrowed(&'out mut dyn WriteBytes),
19 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
20 Owned(FileWriter),
21}
22
23impl WriteBytes for Output<'_> {
24 fn write(&mut self, bytes: &[u8]) -> IonResult<()> {
25 match self {
26 Output::Borrowed(output) => output.write(bytes),
27 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
28 Output::Owned(file) => file.write(bytes),
29 }
30 }
31
32 fn patch(&mut self, at: u64, bytes: &[u8]) -> IonResult<()> {
33 match self {
34 Output::Borrowed(output) => output.patch(at, bytes),
35 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
36 Output::Owned(file) => file.patch(at, bytes),
37 }
38 }
39
40 fn position(&mut self) -> IonResult<u64> {
41 match self {
42 Output::Borrowed(output) => output.position(),
43 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
44 Output::Owned(file) => file.position(),
45 }
46 }
47}
48
49fn strip_lists(metadata: &MzML) -> MzML {
50 let mut metadata = metadata.clone();
51 metadata.run.spectrum_list = None;
52 metadata.run.chromatogram_list = None;
53 metadata
54}
55
56pub struct IonWriter<'out> {
57 output: Output<'out>,
58 encoder: IonEncoder,
59 metadata: MzML,
60}
61
62#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
63impl IonWriter<'static> {
64 pub fn create(
65 path: impl AsRef<Path>,
66 metadata: &MzML,
67 options: &WriteOptions,
68 ) -> IonResult<Self> {
69 let file = FileWriter::open_path(path.as_ref())?;
70 let mut output = Output::Owned(file);
71 let encoder = IonEncoder::begin(&mut output, metadata, options)?;
72 Ok(Self {
73 output,
74 encoder,
75 metadata: strip_lists(metadata),
76 })
77 }
78}
79
80impl<'out> IonWriter<'out> {
81 pub fn to(
82 output: &'out mut dyn WriteBytes,
83 metadata: &MzML,
84 options: &WriteOptions,
85 ) -> IonResult<Self> {
86 let mut output = Output::Borrowed(output);
87 let encoder = IonEncoder::begin(&mut output, metadata, options)?;
88 Ok(Self {
89 output,
90 encoder,
91 metadata: strip_lists(metadata),
92 })
93 }
94
95 pub fn write_spectrum(&mut self, spectrum: &Spectrum) -> IonResult<()> {
96 self.encoder.push_spectrum(&mut self.output, spectrum)
97 }
98
99 pub fn write_chromatogram(&mut self, chromatogram: &Chromatogram) -> IonResult<()> {
100 self.encoder.push_chromatogram(&mut self.output, chromatogram)
101 }
102
103 pub fn write_stream(mut self, scans: &mut dyn ScanStream) -> IonResult<()> {
104 self.encoder.write_stream(&mut self.output, scans)?;
105 self.flush_file()
106 }
107
108 pub fn finish(mut self) -> IonResult<()> {
109 self.encoder.finish(&mut self.output, &self.metadata)?;
110 self.flush_file()
111 }
112
113 fn flush_file(&mut self) -> IonResult<()> {
114 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
115 if let Output::Owned(file) = &mut self.output {
116 file.flush()?;
117 }
118
119 Ok(())
120 }
121}