Function macro_tools::orphan::item::ensure_comma
source · pub fn ensure_comma(input: &ItemStruct) -> ItemStruct
Expand description
Ensures the last field in a struct has a trailing comma.
This function checks and modifies the fields of a given struct, input
, ensuring that the last field, whether in
named or unnamed structs, ends with a trailing comma. This adjustment is commonly needed in macro-generated
code to maintain consistency and syntactical correctness across different struct types, including unit structs
which are unaffected as they do not contain fields.
§Arguments
input
- A reference to the struct (syn::ItemStruct
) whose fields are to be checked and modified.
§Returns
Returns a modified clone of the input struct (syn::ItemStruct
) where the last field in named or unnamed
structs has a trailing comma. Unit structs remain unchanged as they do not contain fields.
§Examples
use macro_tools::
{
syn::{ parse_quote, ItemStruct },
quote::quote,
};
// Create a struct using `parse_quote!` macro
let input_struct : ItemStruct = parse_quote!
{
struct Example
{
field1 : i32,
field2 : String
}
};
// Apply `ensure_comma` to ensure the last field has a trailing comma
let modified_struct = macro_tools::item::ensure_comma( &input_struct );
// Now `modified_struct` will have a trailing comma after `field2`
assert_eq!( quote!( #modified_struct ).to_string(), quote!
{
struct Example
{
field1 : i32,
field2 : String,
}
}.to_string() );