ctb_formats_conversion/
convert_api.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! High-level APIs for bidirectional conversions between `DcMstring` and external documents.
21//!
22//! # Architecture
23//!
24//! Document conversion in Collective Toolbox is organized around [`DcMstring`] as the
25//! universal internal representation. Conversions are bidirectional:
26//! - **Import**: Converts external document formats (e.g. UTF-8, DcText, Dcts, Dcal,
27//!   `DcIntegerList`, `DcMixed`, DcUtf, SEMS, Ascii, `CscJournal`) into [`DcMstring`].
28//! - **Export**: Serializes a [`DcMstring`] into any supported output format
29//!   ([`SupportedDcOutputFormat`]).
30//!
31//! ## In-Memory Conversions
32//!
33//! For whole documents in memory or files on disk:
34//! - [`convert_bytes_to_dcmstring`]: Parses raw bytes with optional format detection.
35//! - [`convert_file_to_dcmstring`]: Reads a file from disk (or stdin) into a [`DcMstring`].
36//! - [`convert_cscjournal_to_dcmstring`]: Converts legacy and v4+ cscjournal files to a [`DcMstring`].
37//! - [`convert_dcmstring_to_bytes`]: Serializes [`DcMstring`] to target format bytes.
38//! - [`convert_dcmstring_to_file`]: Writes [`DcMstring`] to a target format file or stdout.
39//!
40//! ## Streaming Conversions
41//!
42//! For pipeline processing of documents and standard Unix pipelines:
43//! - [`stream_reader_to_dcmwriter`]: Streams an input source into framed `.dcmm` bytes.
44//! - [`stream_file_to_dcmwriter`]: Streams a file on disk into framed `.dcmm` bytes.
45//! - [`stream_dcmreader_to_writer`]: Streams framed `.dcmm` bytes into a target format.
46//! - [`stream_dcmfile_to_writer`]: Streams a `.dcmm` file into a target format writer.
47//!
48//! ## Format Detection
49//!
50//! When [`FormatSpec::Auto`] is specified, format detection is delegated directly to
51//! the authoritative format detection engine in [`ctb_formats_detection`].
52//!
53//! ## Diagnostics and Logs
54//!
55//! All conversions return a [`ctb_formats_utilities::FormatLog`] containing warnings,
56//! errors, and diagnostic notices generated during conversion.
57
58pub use crate::convert_bytes::{
59    DcConversionOptions, DcConversionResult, DcExportOptions, DcExportResult,
60    DcNormalizationMode, FormatSpec, convert_bytes_to_dcmstring,
61    convert_cscjournal_to_dcmstring, convert_dcmstring_to_bytes,
62    convert_dcmstring_to_file, convert_file_to_dcmstring,
63};
64pub use crate::convert_stream::{
65    StreamConversionReceipt, StreamExportReceipt, stream_dcmfile_to_writer,
66    stream_dcmreader_to_writer, stream_file_to_dcmwriter,
67    stream_reader_to_dcmwriter,
68};
69
70use ctb_utilities::FormatId;
71pub use ctb_utilities::format_support::{
72    SupportedDcInputFormat, SupportedDcOutputFormat, SupportedDcTransformation,
73};
74
75/// Returns the ordered list of all supported input formats for conversion to `DcMstring`.
76#[must_use]
77pub fn list_supported_input_formats() -> &'static [FormatId] {
78    SupportedDcInputFormat::ALL_FORMAT_IDS
79}
80
81/// Returns the ordered list of all supported output formats for conversion from `DcMstring`.
82#[must_use]
83pub fn list_supported_output_formats() -> &'static [FormatId] {
84    SupportedDcOutputFormat::ALL_FORMAT_IDS
85}
86
87/// Returns the ordered list of all supported document transformations.
88#[must_use]
89pub fn list_supported_transformations() -> &'static [FormatId] {
90    SupportedDcTransformation::ALL_FORMAT_IDS
91}