rakata_formats/txi/
writer.rs1use std::io::{Cursor, Write};
4
5use crate::binary;
6
7use super::{Txi, TxiEntry, TxiError, TxiWriteOptions};
8
9#[cfg_attr(
15 feature = "tracing",
16 tracing::instrument(level = "debug", skip(writer, txi))
17)]
18pub fn write_txi<W: Write>(writer: &mut W, txi: &Txi) -> Result<(), TxiError> {
19 write_txi_with_options(writer, txi, TxiWriteOptions::default())
20}
21
22#[cfg_attr(
30 feature = "tracing",
31 tracing::instrument(level = "debug", skip(writer, txi, _options))
32)]
33pub fn write_txi_with_options<W: Write>(
34 writer: &mut W,
35 txi: &Txi,
36 _options: TxiWriteOptions,
37) -> Result<(), TxiError> {
38 let mut lines = Vec::new();
39
40 for entry in &txi.entries {
41 match entry {
42 TxiEntry::Directive(directive) => {
43 if directive.arguments.is_empty() {
44 lines.push(directive.command.clone());
45 } else {
46 lines.push(format!("{} {}", directive.command, directive.arguments));
47 }
48 }
49 TxiEntry::CoordinateBlock(block) => {
50 let command = &block.command;
51 let declared_count = block.declared_count;
52 lines.push(format!("{command} {declared_count}"));
53 for coordinate in &block.coordinates {
54 lines.push(format!(
55 "{} {} {}",
56 coordinate.u, coordinate.v, coordinate.w
57 ));
58 }
59 }
60 }
61 }
62
63 let text = lines.iter().map(|l| format!("{l}\n")).collect::<String>();
70 binary::write_cp1252(writer, &text, "TXI payload".into(), |context, source| {
71 TxiError::TextEncoding { context, source }
72 })
73}
74
75#[cfg_attr(feature = "tracing", tracing::instrument(level = "debug", skip(txi)))]
81pub fn write_txi_to_vec(txi: &Txi) -> Result<Vec<u8>, TxiError> {
82 write_txi_to_vec_with_options(txi, TxiWriteOptions::default())
83}
84
85#[cfg_attr(
92 feature = "tracing",
93 tracing::instrument(level = "debug", skip(txi, options))
94)]
95pub fn write_txi_to_vec_with_options(
96 txi: &Txi,
97 options: TxiWriteOptions,
98) -> Result<Vec<u8>, TxiError> {
99 let mut cursor = Cursor::new(Vec::new());
100 write_txi_with_options(&mut cursor, txi, options)?;
101 Ok(cursor.into_inner())
102}