Module writer

Source
Expand description

Buffered writer for serializing AIM data structures.

This module provides a buffered writer implementation for efficiently serializing AST and AIM data structures to their text representations. It includes specialized formatting functions for different data types and supports various output modes including escaped and quoted formats.

The Writer uses several optimization techniques:

  • Buffered writing with a single String buffer to minimize allocations
  • Efficient number formatting using itoa and ryu crates
  • Direct character writing when possible
  • Specialized formatters for each data type

§Examples

use aimx::writer::{Writer, PrintMode, Prefix};
use aimx::{Literal, Value};

// Writing simple text
let mut writer = Writer::stringizer();
writer.write_str("Hello, world!");
assert_eq!(writer.finish(), "Hello, world!");

// Writing values with different modes
let value = Value::Literal(Literal::from_text("test".to_string()));

// String mode - no escaping
let mut writer = Writer::stringizer();
writer.print_value(&Prefix::None, &value);
assert_eq!(writer.finish(), "test");

// Sanitized mode - escape special characters
let mut writer = Writer::sanitizer();
writer.print_value(&Prefix::None, &value);
assert_eq!(writer.finish(), "test");

// Formula mode - quote and escape special characters
let mut writer = Writer::formulizer();
writer.print_value(&Prefix::None, &value);
assert_eq!(writer.finish(), "\"test\"");

§Modes and Formatting

The Writer supports three main formatting modes through the PrintMode enum:

Prefix styles for list formatting are available through the Prefix enum:

§Implementation Details

The Writer is designed for performance with several key optimizations:

  • Buffered writing with a single String buffer to minimize allocations
  • Efficient number formatting using itoa and ryu crates for integers and floats
  • Direct character writing when possible
  • Specialized formatters for each data type
  • Thread-safe operation for use in concurrent scenarios

§See Also

  • Value - Runtime value representation that uses Writer for formatting
  • Literal - Compile-time literal value representation
  • Reference - Variable reference representation

Structs§

Writer
A buffered writer for serializing AST/AIM data structures.

Enums§

Prefix
Prefix style for list items.
PrintMode
Output formatting mode for the writer.