ChatGPT powered Rust proc macro that generates code at compile-time.

Overview

gpt-macro

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:

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:

fn fizzbuzz(n: u32) -> String {
    if n % 3 == 0 && n % 5 == 0 {
        return String::from("fizzbuzz");
    } else if n % 3 == 0 {
        return String::from("fizz");
    } else if n % 5 == 0 {
        return String::from("buzz");
    } else {
        return n.to_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");
}

#[auto_test]

See this example:

use gpt_macro::auto_test;

#[auto_test(test_valid, test_div_by_zero)]
fn div_u32(a: u32, b: u32) -> u32 {
    if b == 0 {
        panic!("attempt to divide by zero");
    }
    a / b
}

Supported Models

  • ChatGPT: gpt-3.5-turbo (default)
  • Text Completion: text-davinci-003 (Specify davinci feature to enable it)

License

gpt-macro is released under the MIT license.

You might also like...
Utilites for working with `bevy_ecs` when not all types are known at compile time

bevy_ecs_dynamic Utilities for working with bevy_ecs in situations where the types you're dealing with might not be known at compile time (e.g. script

tidy-builder is a builder generator that is compile-time correct.
tidy-builder is a builder generator that is compile-time correct.

The Builder derive macro creates a compile-time correct builder which means that it only allows you to build the given struct if and only if you provi

Ask ChatGPT for a shell script, code, or anything, directly from your terminal 🤖🧠👨‍💻
Ask ChatGPT for a shell script, code, or anything, directly from your terminal 🤖🧠👨‍💻

ShellGPT Ask ChatGPT for a shell script, code, or anything, directly from your terminal 🤖 🧠 👨‍💻 Demo Install The binary is named gpt when installe

turbocommit is a Rust-based CLI tool that generates high-quality git commit messages in accordance with the Conventional Commits specification, using OpenAI's
turbocommit is a Rust-based CLI tool that generates high-quality git commit messages in accordance with the Conventional Commits specification, using OpenAI's

turbocommit is a Rust-based CLI tool that generates high-quality git commit messages in accordance with the Conventional Commits specification, using OpenAI's `gpt-3.5-turbo` language model. It is easy to use and a cost-effective way to keep git commit history at a higher quality, helping developers stay on track with their work.

Evaluate performance gains to expect when EVM were to compile hot contracts into machine code

Convert evm bytecode to native machine code and go vroom - just an experiment, probably broken, reach out to [email protected] to contribute / productionize.

CLI for video images. Generates animated video contact sheets fast.

vimg CLI for video images. Generate animated video contact sheets fast. Uses ffmpeg. Note: Support for animated avif isn't everywhere yet, try viewing

Generates an Anchor CPI crate from a JSON IDL.

anchor-gen Generates a crate for cross-program invocations to an Anchor program from a JSON IDL. Usage In a new crate, write: anchor_gen::generate_cpi

Croc-look is a tool to make testing and debuging proc macros easier

croc-look croc-look is a tool to make testing and debuging proc macros easier by these two features Printing the implementation specific generated cod

An LLM-powered (CodeLlama or OpenAI) local diff code review tool.

augre An LLM-powered (CodeLlama or OpenAI) local diff code review tool. Binary Usage Install Windows: $ iwr https://github.com/twitchax/augre/releases

Comments
  • build(deps): update syn requirement from 1.0 to 2.0

    build(deps): update syn requirement from 1.0 to 2.0

    Updates the requirements on syn to permit the latest version.

    Release notes

    Sourced from syn's releases.

    2.0.2

    • Documentation improvements
    Commits
    • cac5cc6 Release 2.0.2
    • 8f826ef Touch up spacing in example for parse_multi_with_leading_vert
    • fc58fcf Fix typo in Stmt::Macro documentation
    • e298152 Release 2.0.1
    • b87a0a1 Merge pull request #1409 from dtolnay/requiremeta
    • 59dd7cc Add methods on Meta for error reporting an incorrect kind of attribute
    • 11c0b6c Build attribute parse errors using std::fmt system
    • e6cf741 Move parse_args_with error handler into parsing module
    • 906fa56 Release 2.0.0
    • 29bd855 Fix warning about Cargo.toml exclude/include
    • Additional commits viewable in compare view

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies 
    opened by dependabot[bot] 3
  • chore: remove useless imports

    chore: remove useless imports

    Though compiles successfully, cargo complains "unused imports":

    $ OPENAI_API_KEY=<...> /Users/lei/.cargo/bin/cargo test --color=always --package gpt-macro --test tests "" 
    warning: unused import: `log::debug`
      --> src/internal/chatgpt.rs:12:5
       |
    12 | use log::debug;
       |     ^^^^^^^^^^
       |
       = note: `#[warn(unused_imports)]` on by default
    
    warning: `gpt-macro` (lib) generated 1 warning
        Finished test [unoptimized + debuginfo] target(s) in 0.05s
         Running tests/tests.rs (target/debug/deps/tests-289b39449a0fcc46)
    
    running 1 test
    warning: unused import: `log::debug`
      --> /Users/lei/Workspace/Rust/gpt-macro/src/internal/chatgpt.rs:12:5
       |
    12 | use log::debug;
       |     ^^^^^^^^^^
       |
       = note: `#[warn(unused_imports)]` on by default
    
    warning: `gpt-macro` (lib) generated 1 warning
       Compiling gpt-macro-tests v0.0.0 (/Users/lei/Workspace/Rust/gpt-macro/target/tests/trybuild/gpt-macro)
        Finished dev [unoptimized + debuginfo] target(s) in 0.15s
    
    
    test tests/auto_test_fn.rs ... ok
    
    STDERR:
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    warning: unused import: `log::debug`
      --> /Users/lei/Workspace/Rust/gpt-macro/src/internal/chatgpt.rs:12:5
       |
    12 | use log::debug;
       |     ^^^^^^^^^^
       |
       = note: `#[warn(unused_imports)]` on by default
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    
    test tests/auto_impl_fn.rs ... ok
    
    STDERR:
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    warning: unused import: `log::debug`
      --> /Users/lei/Workspace/Rust/gpt-macro/src/internal/chatgpt.rs:12:5
       |
    12 | use log::debug;
       |     ^^^^^^^^^^
       |
       = note: `#[warn(unused_imports)]` on by default
    ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
    
    
    
    test tests ... ok
    
    test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.56s
    
    
    opened by v0y4g3r 0
Owner
Akira Moroo
UEFI firmware enthusiast
Akira Moroo
Rust-powered CLI tool designed to simplify and streamline the release process with help of ChatGPT

$ releasecraftsman ????‍♂️?? Automate Your Release Process with Precision and Ease. ?? Features Generate well-crafted release notes using GPT-3.5 and

Tornike Gomareli 7 Sep 21, 2023
A programming and system administration assistant, powered by chatGPT

TermGPT Interact with ChatGPT from your terminal! ?? ?? Install Cargo cargo install termgpt termgpt --help From source git clone [email protected]:bahdot

Gokul 5 May 11, 2023
Stockbook embeds 1-bit raster images in your code at compile time

stockbook Stockbook embeds 1-bit raster images in your code at compile time. Designed primarily for #![no_std] usage, in embedded or other program-mem

Karol Belina 3 Oct 27, 2022
Catch Tailwindcss Errors at Compile-Time Before They Catch You, without making any change to your code! Supports overriding, extending, custom classes, custom modifiers, Plugins and many more 🚀🔥🦀

twust Twust is a powerful static checker in rust for TailwindCSS class names at compile-time. Table of Contents Overview Installation Usage Statement

null 15 Nov 8, 2023
ChatGPT-Code-Review is a Rust application that uses the OpenAI GPT-3.5 language model to review code

ChatGPT-Code-Review is a Rust application that uses the OpenAI GPT-3.5 language model to review code. It accepts a local path to a folder containing code, and generates a review for each file in the folder and its subdirectories.

Greg P. 15 Apr 22, 2023
A HTML to node macro powered by rstml.

html-node A HTML to node macro powered by rstml. Features Text escaping Pretty-printing NEW: type-safe elements and attributes! (example) Example let

Vidhan Bhatt 6 Jul 28, 2023
Write simple proc-macros inline with other source code.

script-macro An experimental way to write simple proc-macros inline with other source code. Did you ever end up getting frustrated at the boilerplate

Markus Unterwaditzer 17 Jun 10, 2023
Rust library to convert RGB 24-bit colors into ANSI 256 (8-bit) color codes with zero dependencies and at compile-time.

rgb2ansi256 rgb2ansi256 is a small Rust library to convert RGB 24-bit colors into ANSI 256 (8-bit) color codes with zero dependencies and const fn. Th

Linda_pp 7 Nov 17, 2022
A strong, compile-time enforced authorization framework for rust applications.

DACquiri A compile-time enforced authorization framework for Rust applications. Authorization In typical applications, authorization checks are perfor

resync 247 Dec 20, 2022
Choose Rust types at compile-time via boolean constants

condtype Choose Rust types at compile-time via boolean constants, brought to you by Nikolai Vazquez. If you find this library useful, consider starrin

Nikolai Vazquez 36 May 8, 2023