pub struct Formatter<'a> {
pub elements: Vec<FormatElement<'a>>,
pub arg_num: usize,
}Expand description
Compatible with java.util.Formatter
Fields§
§elements: Vec<FormatElement<'a>>§arg_num: usizeImplementations§
Source§impl<'a> Formatter<'a>
impl<'a> Formatter<'a>
pub fn new(elements: Vec<FormatElement<'a>>) -> Self
Sourcepub fn parse(fmt: &'a str, arg_types: &[DataType]) -> Result<Self>
pub fn parse(fmt: &'a str, arg_types: &[DataType]) -> Result<Self>
Parses a printf-style format string into a Formatter with validation.
This method implements a comprehensive parser for Java java.util.Formatter syntax,
processing the format string character by character to identify and validate format
specifiers against the provided argument types.
§Arguments
fmt- The format string containing literal text and format specifiersarg_types- Array of DataFusion DataTypes corresponding to the arguments
§Parsing Process
The parser operates in several phases:
-
String Scanning: Iterates through the format string looking for ‘%’ characters that mark the beginning of format specifiers or special sequences.
-
Special Sequence Handling: Processes escape sequences:
%%becomes a literal ‘%’ character%nbecomes a newline character%<indicates reuse of the previous argument with a new format specifier
-
Argument Index Resolution: Determines which argument each format specifier refers to:
- Sequential indexing: arguments are consumed in order (1, 2, 3, …)
- Positional indexing: explicit argument position using
%n$syntax - Previous argument reuse:
%<references the last used argument
-
Format Specifier Parsing: For each format specifier, extracts:
- Flags (-, +, space, #, 0, ‘,’, ‘(’)
- Width specification (minimum field width)
- Precision specification (decimal places or maximum characters)
- Conversion type (d, s, f, x, etc.)
-
Type Validation: Verifies that each format specifier’s conversion type is compatible with the corresponding argument’s DataType. For example:
- Integer conversions (%d, %x, %o) require integer DataTypes
- String conversions (%s, %S) accept any DataType
- Float conversions (%f, %e, %g) require numeric DataTypes
-
Element Construction: Creates FormatElement instances for:
- Verbatim text sections (copied directly to output)
- Validated format specifiers with their parsed parameters
§Internal State Management
The parser maintains several state variables:
argument_index: Tracks the current sequential argument positionprev: Remembers the last used argument index for%<referencesres: Accumulates the parsed FormatElement instancesrem: Points to the remaining unparsed portion of the format string
§Validation and Error Handling
The parser performs extensive validation including:
- Argument index bounds checking against the provided arg_types array
- Format specifier syntax validation
- Type compatibility verification between conversion types and DataTypes
- Detection of malformed numeric parameters and invalid flag combinations
§Returns
Returns a Formatter containing the parsed elements and the maximum argument index encountered, enabling efficient argument validation during formatting.
pub fn format(&self, args: &[ScalarValue]) -> Result<String>
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Formatter<'a>
impl<'a> RefUnwindSafe for Formatter<'a>
impl<'a> Send for Formatter<'a>
impl<'a> Sync for Formatter<'a>
impl<'a> Unpin for Formatter<'a>
impl<'a> UnwindSafe for Formatter<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more