Struct jomini::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,

source

pub fn inner(&mut self) -> &mut W

Get inner writer, keeping ownership

source

pub fn into_inner(self) -> W

Consumes this Writer, returning the underlying writer

source

pub fn expecting_key(&self) -> bool

Returns true if the next write event would be a key

source

pub fn depth(&self) -> usize

Returns the number of objects deep the writer is from the root object

source

pub fn write_object_start(&mut self) -> Result<(), Error>

Write out the start of an object

source

pub fn write_array_start(&mut self) -> Result<(), Error>

Write out the start of an array

source

pub fn write_end(&mut self) -> Result<(), Error>

Write the end of an array or object

source

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");
source

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");
source

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");
source

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\"");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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}");
source

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");
source

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}");
source

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");
source

pub fn start_mixed_mode(&mut self)

Enter mixed mode for writing a container that is a list and an object

source

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\"");
source

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");

Trait Implementations§

source§

impl<W: Debug> Debug for TextWriter<W>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Freeze for TextWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for TextWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for TextWriter<W>
where W: Send,

§

impl<W> Sync for TextWriter<W>
where W: Sync,

§

impl<W> Unpin for TextWriter<W>
where W: Unpin,

§

impl<W> UnwindSafe for TextWriter<W>
where W: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.