Skip to main content

molframe_pdb/writer/
stream.rs

1//! Incremental PDB output over the shared bounded text adapter.
2
3use super::pdb::{PdbOptions, render_selected};
4use molframe_core::diagnostic::{Code, Diagnostic};
5use molframe_core::io::{Select, SelectAll, TextOutput};
6use molframe_core::structure::Structure;
7use std::io::Write;
8
9/// Streams a complete PDB directly to a byte destination.
10///
11/// # Errors
12///
13/// Returns every fixed-column refusal before the first byte, or an output
14/// diagnostic if the destination stops accepting bytes.
15pub fn write_to<W: Write>(
16    structure: &Structure,
17    options: &PdbOptions,
18    output: &mut W,
19) -> Result<(), Vec<Diagnostic>> {
20    write_selected_to(structure, options, &SelectAll, output)
21}
22
23/// Streams selected PDB records without materialising the complete text.
24///
25/// # Errors
26///
27/// Returns capacity, required-field, or destination diagnostics.
28pub fn write_selected_to<W: Write>(
29    structure: &Structure,
30    options: &PdbOptions,
31    select: &impl Select,
32    output: &mut W,
33) -> Result<(), Vec<Diagnostic>> {
34    let mut output = TextOutput::new(output);
35    render_selected(&mut output, structure, options, select)?;
36    output
37        .finish()
38        .map_err(|error| vec![output_diagnostic(error)])
39}
40
41fn output_diagnostic(error: impl std::fmt::Display) -> Diagnostic {
42    Diagnostic::new(Code::E7901).with_context("reason", error.to_string())
43}