Skip to content

A Rust procedural macro to convert struct definitions into a string representation.

License

Notifications You must be signed in to change notification settings

Houski/struct_to_string

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A Rust procedural macro to convert struct definitions into a string representation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages