Formatter

Struct Formatter 

Source
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: usize

Implementations§

Source§

impl<'a> Formatter<'a>

Source

pub fn new(elements: Vec<FormatElement<'a>>) -> Self

Source

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 specifiers
  • arg_types - Array of DataFusion DataTypes corresponding to the arguments
§Parsing Process

The parser operates in several phases:

  1. String Scanning: Iterates through the format string looking for ‘%’ characters that mark the beginning of format specifiers or special sequences.

  2. Special Sequence Handling: Processes escape sequences:

    • %% becomes a literal ‘%’ character
    • %n becomes a newline character
    • %< indicates reuse of the previous argument with a new format specifier
  3. 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
  4. 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.)
  5. 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
  6. 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 position
  • prev: Remembers the last used argument index for %< references
  • res: Accumulates the parsed FormatElement instances
  • rem: 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.

Source

pub fn format(&self, args: &[ScalarValue]) -> Result<String>

Trait Implementations§

Source§

impl<'a> Debug for Formatter<'a>

Source§

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

Formats the value using the given formatter. Read more

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> ErasedDestructor for T
where T: 'static,