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
itoaandryucrates - 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:
PrintMode::None- No special formatting (stringizer)PrintMode::Escape- Escape special characters (sanitizer)PrintMode::QuoteEscape- Quote and escape special characters (formulizer)
Prefix styles for list formatting are available through the Prefix enum:
Prefix::None- No prefixPrefix::Unordered- Unordered list prefix (e.g., “- “)Prefix::Ordered- Ordered list prefix (e.g., “1. “)
§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
itoaandryucrates 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
Structs§
- Writer
- A buffered writer for serializing AST/AIM data structures.