Struct jomini::text::TextWriter
source · pub struct TextWriter<W> { /* private fields */ }
Expand description
Write data in PDS format.
Instantiated via TextWriterBuilder
Implementations§
source§impl<W> TextWriter<W>where
W: Write,
impl<W> TextWriter<W>where
W: Write,
sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consumes this Writer, returning the underlying writer
sourcepub fn expecting_key(&self) -> bool
pub fn expecting_key(&self) -> bool
Returns true if the next write event would be a key
sourcepub fn at_unknown_start(&self) -> bool
pub fn at_unknown_start(&self) -> bool
Returns true if at the start of an unknown container type
sourcepub fn at_array_value(&self) -> bool
pub fn at_array_value(&self) -> bool
Returns true if the writer is prepared to write an array value
sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Returns the number of objects deep the writer is from the root object
sourcepub fn write_start(&mut self) -> Result<(), Error>
pub fn write_start(&mut self) -> Result<(), Error>
Write out the start of container (‘{’)
If the type of the container is known to be an array or object, prefer
[write_object_start
] or [write_array_start
]
sourcepub fn write_object_start(&mut self) -> Result<(), Error>
pub fn write_object_start(&mut self) -> Result<(), Error>
Write out the start of an object
sourcepub fn write_array_start(&mut self) -> Result<(), Error>
pub fn write_array_start(&mut self) -> Result<(), Error>
Write out the start of an array
sourcepub fn write_bool(&mut self, data: bool) -> Result<(), Error>
pub fn write_bool(&mut self, data: bool) -> Result<(), Error>
Write a boolean
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"hello")?;
writer.write_bool(true)?;
writer.write_unquoted(b"foo")?;
writer.write_bool(false)?;
assert_eq!(&out, b"hello=yes\nfoo=no");
sourcepub fn write_operator(&mut self, data: Operator) -> Result<(), Error>
pub fn write_operator(&mut self, data: Operator) -> Result<(), Error>
Write an operator. Writing an equal operator is optional whenever an object is being written.
If an array was being written, the operator will switch to writing an object
use jomini::{text::Operator, TextWriterBuilder};
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"a")?;
writer.write_operator(Operator::LessThan)?;
writer.write_unquoted(b"b")?;
assert_eq!(&out, b"a < b");
sourcepub fn write_unquoted(&mut self, data: &[u8]) -> Result<(), Error>
pub fn write_unquoted(&mut self, data: &[u8]) -> Result<(), Error>
Write bytes directly to the writer.
The contents of the bytes are not checked, so it is up to the caller to ensure that the data is a valid unquoted field (no spaces or control characters).
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"a")?;
writer.write_unquoted(b"b")?;
assert_eq!(&out, b"a=b");
sourcepub fn write_quoted(&mut self, data: &[u8]) -> Result<(), Error>
pub fn write_quoted(&mut self, data: &[u8]) -> Result<(), Error>
Write a field to be encapsulated in quotes.
Unlike the unquoted variant, this method will inspect the data to ensure everything is properly escaped, like quotes and escape characters. And will trim trailing newlines. The textual data to write out is assumed to already be in the correct encoding (windows-1252 or UTF-8)
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_quoted(b"name")?;
writer.write_quoted(br#"captain "joe" rogers"#)?;
assert_eq!(&out, b"\"name\"=\"captain \\\"joe\\\" rogers\"");
sourcepub fn write_i32(&mut self, data: i32) -> Result<(), Error>
pub fn write_i32(&mut self, data: i32) -> Result<(), Error>
Write a signed 32bit integer.
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"stability")?;
writer.write_i32(-3);
assert_eq!(&out, b"stability=-3");
sourcepub fn write_u32(&mut self, data: u32) -> Result<(), Error>
pub fn write_u32(&mut self, data: u32) -> Result<(), Error>
Write an unsigned 32bit integer.
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"stability")?;
writer.write_u32(3);
assert_eq!(&out, b"stability=3");
sourcepub fn write_u64(&mut self, data: u64) -> Result<(), Error>
pub fn write_u64(&mut self, data: u64) -> Result<(), Error>
Write an unsigned 64bit integer.
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"seed")?;
writer.write_u64(1000000000000);
assert_eq!(&out, b"seed=1000000000000");
sourcepub fn write_i64(&mut self, data: i64) -> Result<(), Error>
pub fn write_i64(&mut self, data: i64) -> Result<(), Error>
Write a signed 64bit integer.
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"ratio")?;
writer.write_i64(-1);
assert_eq!(&out, b"ratio=-1");
sourcepub fn write_f32(&mut self, data: f32) -> Result<(), Error>
pub fn write_f32(&mut self, data: f32) -> Result<(), Error>
Write a 32 bit floating point at full precision
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"morale")?;
writer.write_f32(4.566);
assert_eq!(&out, b"morale=4.566");
sourcepub fn write_f32_precision(
&mut self,
data: f32,
precision: usize,
) -> Result<(), Error>
pub fn write_f32_precision( &mut self, data: f32, precision: usize, ) -> Result<(), Error>
Write a 32 bit floating point at a given precision
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"morale")?;
writer.write_f32_precision(4.0, 3);
assert_eq!(&out, b"morale=4.000");
sourcepub fn write_f64(&mut self, data: f64) -> Result<(), Error>
pub fn write_f64(&mut self, data: f64) -> Result<(), Error>
Write a 64 bit floating point at full precision
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"strength")?;
writer.write_f64(6790.35609);
assert_eq!(&out, b"strength=6790.35609");
sourcepub fn write_f64_precision(
&mut self,
data: f64,
precision: usize,
) -> Result<(), Error>
pub fn write_f64_precision( &mut self, data: f64, precision: usize, ) -> Result<(), Error>
Write a 64 bit floating point at a given precision
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"morale")?;
writer.write_f64_precision(4.0, 3);
assert_eq!(&out, b"morale=4.000");
sourcepub fn write_header(&mut self, header: &[u8]) -> Result<(), Error>
pub fn write_header(&mut self, header: &[u8]) -> Result<(), Error>
Write a header.
It is undefined if the next commands do not write out the start of an array or object
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"color")?;
writer.write_header(b"rgb")?;
writer.write_array_start()?;
writer.write_i32(100)?;
writer.write_i32(200)?;
writer.write_i32(50)?;
writer.write_end()?;
assert_eq!(&out, b"color=rgb {\n 100 200 50\n}");
sourcepub fn write_date(&mut self, data: PdsDateFormatter) -> Result<(), Error>
pub fn write_date(&mut self, data: PdsDateFormatter) -> Result<(), Error>
Write a date formatted in the game style
use jomini::{common::{Date, PdsDate}, TextWriterBuilder};
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
let date = Date::from_ymd(1444, 11, 11);
writer.write_unquoted(b"start")?;
writer.write_date(date.game_fmt())?;
assert_eq!(&out, b"start=1444.11.11");
sourcepub fn write_rgb(&mut self, color: &Rgb) -> Result<(), Error>
pub fn write_rgb(&mut self, color: &Rgb) -> Result<(), Error>
Write an rgb value
use jomini::{binary::Rgb, TextWriterBuilder};
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"start")?;
let val = Rgb { r: 10, g: 9, b: 8, a: None };
writer.write_rgb(&val)?;
writer.write_unquoted(b"end")?;
let val = Rgb { r: 7, g: 6, b: 5, a: Some(4) };
writer.write_rgb(&val)?;
assert_eq!(&out, b"start=rgb {\n 10 9 8\n}\nend=rgb {\n 7 6 5 4\n}");
sourcepub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
Write formatted data
Typically not invoked directly but instead through the write!
macro
use jomini::TextWriterBuilder;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_unquoted(b"start")?;
write!(writer, "unknown_{}", 5)?;
assert_eq!(&out, b"start=unknown_5");
sourcepub fn start_mixed_mode(&mut self)
pub fn start_mixed_mode(&mut self)
Enter mixed mode for writing a container that is a list and an object
sourcepub fn write_binary(&mut self, token: &BinaryToken<'_>) -> Result<(), Error>
pub fn write_binary(&mut self, token: &BinaryToken<'_>) -> Result<(), Error>
Write the binary token with reasonable defaults
This function is intended to be used as a fallback for a higher level
melter that doesn’t need special handling for a given token and thus can
rely on the default behavior of forwarding the token to the writer’s
write_x
methods.
Any caller that uses this function will want to provide the floating point conversion and token translation, which is different from game to game.
§Example
use jomini::{binary::{BinaryTape, BinaryToken}, TextTape, TextWriterBuilder};
let data = [ 0x82, 0x2d, 0x01, 0x00, 0x0f, 0x00, 0x03, 0x00, 0x45, 0x4e, 0x47 ];
let tape = BinaryTape::from_slice(&data[..]).unwrap();
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
for token in tape.tokens() {
match token {
// Define a floating point handler as there isn't a fallback format
BinaryToken::F32(x) => writer.write_f32(f32::from_le_bytes(*x))?,
BinaryToken::F64(x) => writer.write_f64(f64::from_le_bytes(*x))?,
// Every melter will want a custom token resolver
BinaryToken::Token(x) => {
if *x == 0x2d82 {
writer.write_unquoted(b"field1")?;
} else {
writer.write_unquoted(b"unknown")?;
}
},
x => writer.write_binary(x)?,
}
}
assert_eq!(&out, b"field1=\"ENG\"");
sourcepub fn write_tape(&mut self, tape: &TextTape<'_>) -> Result<(), Error>
pub fn write_tape(&mut self, tape: &TextTape<'_>) -> Result<(), Error>
Writes a text tape
Formatting is not preserved.
use jomini::{TextTape, TextWriterBuilder};
let tape = TextTape::from_slice(b"hello=world")?;
let mut out: Vec<u8> = Vec::new();
let mut writer = TextWriterBuilder::new().from_writer(&mut out);
writer.write_tape(&tape)?;
assert_eq!(&out, b"hello=world");