Trait pw_format::macros::PrintfFormatMacroGenerator
source · pub trait PrintfFormatMacroGenerator {
// Required methods
fn finalize(
self,
format_string_fragments: &[PrintfFormatStringFragment]
) -> Result<TokenStream>;
fn string_fragment(&mut self, string: &str) -> Result<()>;
fn integer_conversion(
&mut self,
ty: Ident,
expression: Arg
) -> Result<Option<String>>;
fn string_conversion(&mut self, expression: Arg) -> Result<Option<String>>;
fn char_conversion(&mut self, expression: Arg) -> Result<Option<String>>;
// Provided method
fn untyped_conversion(&mut self, _expression: Arg) -> Result<()> { ... }
}
Expand description
A specialized generator for proc macros that produce printf
style format strings.
For proc macros that need to translate a pw_format
invocation into a
printf
style format string, PrintfFormatMacroGenerator
offer a
specialized form of FormatMacroGenerator
that builds the format string
and provides it as an argument to
finalize
.
In cases where a generator needs to override the conversion specifier it
can return it from its appropriate conversion method. An example of using
this would be wanting to pass a Rust string directly to a printf
call
over FFI. In that case,
string_conversion
could
return Ok(Some("%.*s".to_string()))
to allow both the length and string
pointer to be passed to printf
.
Required Methods§
sourcefn finalize(
self,
format_string_fragments: &[PrintfFormatStringFragment]
) -> Result<TokenStream>
fn finalize( self, format_string_fragments: &[PrintfFormatStringFragment] ) -> Result<TokenStream>
Called by generate_printf
at the end of code generation.
Works like FormatMacroGenerator::finalize
with the addition of
being provided a printf_style
format string.
sourcefn string_fragment(&mut self, string: &str) -> Result<()>
fn string_fragment(&mut self, string: &str) -> Result<()>
Process a string fragment.
NOTE: This string may contain unescaped %
characters.
However, most implementations of this train can simply ignore string
fragments as they will be included (with properly escaped %
characters) as part of the format string passed to
PrintfFormatMacroGenerator::finalize
.
See FormatMacroGenerator::string_fragment
for a disambiguation
between a string fragment and string conversion.
sourcefn integer_conversion(
&mut self,
ty: Ident,
expression: Arg
) -> Result<Option<String>>
fn integer_conversion( &mut self, ty: Ident, expression: Arg ) -> Result<Option<String>>
Process an integer conversion.
May optionally return a printf format string (i.e. “%d”) to override the default.
sourcefn string_conversion(&mut self, expression: Arg) -> Result<Option<String>>
fn string_conversion(&mut self, expression: Arg) -> Result<Option<String>>
Process a string conversion.
May optionally return a printf format string (i.e. “%s”) to override the default.
See FormatMacroGenerator::string_fragment
for a disambiguation
between a string fragment and string conversion.
Provided Methods§
sourcefn untyped_conversion(&mut self, _expression: Arg) -> Result<()>
fn untyped_conversion(&mut self, _expression: Arg) -> Result<()>
Process and untyped conversion.