Git Product home page Git Product logo

bujosa / hydrogen-labs Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 57 KB

Create a Rust procedural macro that reads a JSON file and generates Rust functions based on its contents. The challenge is dynamically creating Rust functions at compile time, based on the names specified in a JSON file. This should take you 1-3 hours to complete.

License: MIT License

Rust 100.00%
macros procedural-macros rust

hydrogen-labs's Introduction

Hydrogen Labs

Challenge description

Read the challenge.md file.

Codegen Solution

Codegen

The generate_functions macro is a procedural macro in Rust that generates functions based on a JSON file. This macro is defined in the lib.rs file.

How it works

The macro takes a string literal as input, expected to be the path to a JSON file. This JSON file should contain an object with a "methods" key that has an array of strings as its value. Each string in this array is considered a function name. The macro will generate a function for each of these function names.

The macro performs the following operations:

  1. Parse the input: The macro parses the input as a string literal representing the path to the JSON file.
  2. Read the content of the JSON file: The macro attempts to read the content of the JSON file. If it cannot read the file (e.g., if the file does not exist or if there is a reading error), it generates a compilation error with an appropriate error message.
  3. Parse the JSON content: The macro attempts to parse the content of the JSON file into a serde_json::Value. If it cannot parse the content (e.g., if the content is not valid JSON), it generates a compilation error with an appropriate error message.
  4. Extract method names from the JSON: The macro looks for a "methods" key in the JSON object and then tries to convert its value into an array. If it cannot find the "methods" key or if its value is not an array, it generates a compilation error with an appropriate error message.
  5. Generate Rust functions: For each method name in the methods array, the macro generates a Rust function with that name. Each generated function simply prints a message when called.
  6. Combine the generated functions into a TokenStream: The macro combines all the generated functions into a single TokenStream and returns it.

Usage

The macro can be used as follows:

use your_macro_crate::generate_functions;

// Call the macro with the path to your JSON file
generate_functions!("path/to/your/json/file.json");

fn main() {
    // Call the generated functions
    fun1();
    fun2();
    fun3();
}

Dependencies

The macro depends on the following crates:

  • proc-macro2: A library for representing Rust code as a sequence of tokens.
  • quote: A library for turning Rust syntax tree data structures into tokens of source code.
  • syn: A library for parsing Rust code into syntax tree data structures.
  • serde_json: A library for parsing JSON into Rust data structures.

hydrogen-labs's People

Contributors

bujosa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.