ChatGPT powered Rust proc macro that generates code at compile-time.
Implemented Macros
auto_impl!{}
#[auto_test(...)]
Usage
Get ChatGPT API key and set it to your environment variable OPENAI_API_KEY
before run.
auto_impl!{}
Syntax:
auto_impl! { $STR_LIT $TOKEN_STREAM }
where $STR_LIT
is a prompt string literal, and $TOKEN_STREAM
is target code.
Example:
}
#[test]
fn test_fizzbuzz() {
assert_eq!(fizzbuzz(3), “fizz”);
assert_eq!(fizzbuzz(5), “buzz”);
assert_eq!(fizzbuzz(15), “fizzbuzz”);
assert_eq!(fizzbuzz(1), “1”);
}
}” dir=”auto”>
use gpt_macro::auto_impl; auto_impl! { "Return fizz if the number is divisible by 3, buzz if the number is divisible by 5, and fizzbuzz if the number is divisible by both 3 and 5." fn fizzbuzz(n: u32) -> String { } #[test] fn test_fizzbuzz() { assert_eq!(fizzbuzz(3), "fizz"); assert_eq!(fizzbuzz(5), "buzz"); assert_eq!(fizzbuzz(15), "fizzbuzz"); assert_eq!(fizzbuzz(1), "1"); } }
As you can see, the fizzbuzz()
implementation is incomplete, so the build fails without auto_impl!{}
. The macro parses given prompt and target code, and asks ChatGPT to fill the code when expanding the macro. It replaces the target with code extracted from ChatGPT response. Then Rust compiler continues compiling the code.
Response Example: