Rust Code Generator: Building a Simple Struct Definition
This challenge asks you to build a Rust program that generates Rust code for defining simple structs. This is a fundamental concept in metaprogramming and can be used to automate repetitive coding tasks, create DSLs, or generate boilerplate code.
Problem Description
Your task is to create a Rust program that takes a simplified description of a struct (name and fields) and outputs valid Rust code for defining that struct.
What needs to be achieved:
You need to write a Rust function that accepts a StructDefinition (you'll define this struct) and returns a String containing the generated Rust code.
Key requirements:
StructDefinitionStructure: Define a Rust struct namedStructDefinitionthat can hold:- The name of the struct to be generated (e.g.,
"User"). - A list of fields, where each field has a name (e.g.,
"id","username") and a type (e.g.,"u32","String"). You can represent these asFieldstructs.
- The name of the struct to be generated (e.g.,
FieldStructure: Define a Rust struct namedFieldthat can hold:- The name of the field (e.g.,
"id"). - The type of the field as a string (e.g.,
"u32","String").
- The name of the field (e.g.,
- Code Generation Function: Implement a function, for example
generate_struct_code, that takes aStructDefinitionas input and returns aString. This string should be a valid Rust representation of the struct. - Output Format: The generated code should follow standard Rust syntax for struct definitions:
struct StructName { field_name_1: FieldType1, field_name_2: FieldType2, // ... } - Field Separation: Fields should be separated by commas within the struct definition.
Expected behavior:
Given a StructDefinition, the function should produce a correctly formatted Rust code string.
Important edge cases:
- No fields: A struct with no fields should still be generated correctly.
- Special characters in names/types: While this challenge focuses on simple cases, consider how you might handle valid Rust identifiers and types. For this problem, assume valid Rust identifiers for struct and field names, and valid Rust type names as strings.
Examples
Example 1:
Input StructDefinition:
StructDefinition {
name: "User".to_string(),
fields: vec![
Field { name: "id".to_string(), type_str: "u32".to_string() },
Field { name: "username".to_string(), type_str: "String".to_string() },
],
}
Output:
```rust
struct User {
id: u32,
username: String,
}
Explanation: The input describes a struct named "User" with two fields: "id" of type "u32" and "username" of type "String". The generated code reflects this definition.
Example 2:
Input StructDefinition:
StructDefinition {
name: "EmptyStruct".to_string(),
fields: vec![],
}
Output:
```rust
struct EmptyStruct {}
Explanation: The input describes a struct named "EmptyStruct" with no fields. The generated code correctly represents an empty struct definition.
Example 3:
Input StructDefinition:
StructDefinition {
name: "Product".to_string(),
fields: vec![
Field { name: "sku".to_string(), type_str: "String".to_string() },
Field { name: "price".to_string(), type_str: "f64".to_string() },
Field { name: "in_stock".to_string(), type_str: "bool".to_string() },
],
}
Output:
```rust
struct Product {
sku: String,
price: f64,
in_stock: bool,
}
Explanation: Generates a struct with three fields, demonstrating comma separation and multiple field types.
Constraints
- Struct names and field names will be valid Rust identifiers (alphanumeric characters and underscores, not starting with a digit).
- Field types will be valid Rust type names (e.g.,
"u32","String","bool","f64","Vec<i32>"). - The number of fields in a struct will not exceed 50.
- The length of any struct or field name will not exceed 50 characters.
- The generated code string should not exceed 1000 characters.
Notes
- You'll need to define the
StructDefinitionandFieldstructs yourself. - Consider using string formatting or concatenation to build the output string.
- Think about how to handle the indentation and newline characters correctly for readable output.
- This exercise is a simplified version of real-world code generation. In more complex scenarios, you might use templating engines or abstract syntax tree (AST) manipulation.