#string-representation #string #struct #convert #proc-macro #string-conversion

macro struct_to_string

A Rust proc-macro library to convert struct definitions to a string representation. An example use case would be for API documentation, and you want to present the Rust struct's for the API response on a webpage.

2 unstable releases

0.2.0 Oct 2, 2023
0.1.0 Sep 23, 2023

#902 in Procedural macros

MIT license

23KB
446 lines

struct_to_string

A Rust procedural macro crate for converting struct definitions into a string representation.

Useful for things like API documentation where you want to display your Rust structs on a webpage.

To further assist with applications such as API documentation, it can also convert your structs to structs/classes in other languages, such as Go, Python, TypeScript, Java, and C#.

The conversion of structs to other languages may not always be perfect for complicated structs.

Installation

Add struct_to_string to your Cargo.toml:

[dependencies]
struct_to_string = "0.2.0"

Usage

Add the #[derive(StructToString)] attribute to the structs you'd like to generate string representations for:

#[derive(StructToString)]
struct MyStruct {
    field1: i32,
    field2: String,
}

You can then use the generated to_rust_string() function:

let my_struct_as_string = MyStruct::to_rust_string();

Which outputs a string like:

"struct MyStruct {
    field1: i32,
    field2: String,
}"

You can also convert your structs to structs/class strings in other languages (useful for API documentation):

let my_struct_as_typescript_string = MyStruct::to_typescript_string();

Which outputs a string like:

"interface MyStruct {
  field1: number;
  field2: string;
}"

The languages that this crate can convert Rust structs can be converted to are:

  • Rust
  • Go
  • Python
  • TypeScript
  • Java
  • C#

Though conversion may not always be perfect for complicated structs.

License

MIT


Dependencies

~320–770KB
~18K SLoC