definition_string

Crates.iodefinition_string
lib.rsdefinition_string
version0.1.2
created_at2025-01-14 05:28:09.237628+00
updated_at2025-02-14 22:24:43.06433+00
descriptionRust macro to convert a struct definition to a String.
homepage
repositoryhttps://github.com/simp4t7/definition_string
max_upload_size
id1515490
size9,310
(simp4t7)

documentation

https://docs.rs/definition_string

README

definition_string

Rust macro to convert a struct defintion into a string (including comments).

crates.io Docs

Installation & Usage

cargo add definition_string
use definition_string::definition_string;

#[definition_string]
MyStruct {
    Stuff: i32,
}

let new_string: String = MyStruct::definition_string();

assert_eq!(new_string, r#"#[definition_string]\nMyStruct {\n    Stuff: i32,\n}"#)

Limitations

#[definition_string] is currently only usable on structs and enums. It would be possible to adapt it to functions or methods or basically anywhere you could slap on an attribute macro but at the moment there's no need.

#[definition_string] will only capture code BELOW it. For example:

#[derive(Debug, PartialEq, Eq)]
#[definition_string]
MyStruct {
    Stuff: i32,
}

Will NOT capture the derive macros in the generated String.

#[definition_string] will not capture non-doc comments between the macros and the struct, but it will capture comments on the fields. For example:

#[derive(Debug, PartialEq, Eq)]
#[definition_string]
//This comment will NOT be captured.
MyStruct {
    //This comment will be.
    Stuff: i32,
}

Just as a quick note most of these limitations could be avoided with nightly features or more complicated code, but for my use case it's not too important and preferable to keep the code simple.

Use Cases

My main motivation for creating this macro was for generating structured content from large language models. I found it was necessary to pass in a lot of struct definitions and obviously wasn't going to copy paste like some kind of barbarian.

Commit count: 19

cargo fmt