Skip to content

Commit

Permalink
feat: bindgen crate (dojoengine#1425)
Browse files Browse the repository at this point in the history
* feat: add first version of plugin-like integration for bindgen

* feat: add unity backend template

* refacto: move the BackendBuilder trait into backends module

* docs: adjust function docs

* fix: rename all to plugin for clarity

* docs: update README

* docs: fix typos

* fix: ensure only dojo contracts are excluded from bindgen

* feat: add DojoMetadata with info about models

* fix: use hashmap instead of vec for models in metadata

* fix: run cairo test fix

* fix: remove unused model

* docs: fix docs

* tests: add tests

* fix: bump cainome and work on tests

* tests: fix tests

* tests: ensure correct path for test file

* feat: add ensure_abi method into model generated contract

* feat: add generate_models_bindings setup for builtin plugins

* fix: improve code parsing and plugin API

* feat: identify systems and use new cainome tokenized abi

* tests: fix building with dojo-test-utils + fix tests

* fix: clean example to have correct class hash

* fix: fix tests

* chore: bump cainome to 0.2.2 to fix composite details in functions

* fix: comment out testing until stack error on windows is investigated
  • Loading branch information
glihm committed Jan 18, 2024
1 parent cd410f9 commit 79278a6
Show file tree
Hide file tree
Showing 13 changed files with 620 additions and 11 deletions.
88 changes: 82 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"

members = [
"crates/benches",
"crates/dojo-bindgen",
"crates/dojo-core",
"crates/dojo-lang",
"crates/dojo-language-server",
Expand Down
22 changes: 22 additions & 0 deletions crates/dojo-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
description = "Dojo specific bindings generator based on Cainome."
edition.workspace = true
license-file.workspace = true
name = "dojo-bindgen"
repository.workspace = true
version.workspace = true

[dependencies]
async-trait.workspace = true
camino.workspace = true
convert_case.workspace = true
starknet.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true

# Some issue with CI on windows, need to be investigated.
# https://github.com/dojoengine/dojo/actions/runs/7548423990/job/20550444492?pr=1425#step:6:1644
#dojo-test-utils = { path = "../dojo-test-utils", features = [ "build-examples" ] }

cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.2.2" }
19 changes: 19 additions & 0 deletions crates/dojo-bindgen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Dojo bindings generator

This crate contains the Dojo bindings generator modules which leverage [cainome](https://github.com/cartridge-gg/cainome) to parse Cairo ABI.

## Architecture

`dojo-bindgen` aims at decoupling at most the knowledge required by `sozo` to output bindings along the contract artifacts. Cainome exposes the `parser` crate, which contains common functions to work with Cairo ABI and generate a list of tokens to have a intermediate representation of the ABI usable at runtime and build logic on top of it to generate the bindings.

[PluginManager](./src/lib.rs): The `PluginManager` is the top level interface that `sozo` uses to request code generation. By providing the artifacts path and the list of plugins (more params in the future), `sozo` indicates which plugin must be invoke to generate the bindings.

[BuiltinPlugin](./src/plugins/mod.rs): The `BuiltinPlugin` are a first lightweight and integrated plugins that are written in rust directly inside this crate. This also comes packaged into the dojo toolchain, ready to be used by developers.

In the future, `dojo-bindgen` will expose a `Plugin` interface similar to protobuf to communicate with a user defined plugin using `stdin` for greater flexibility.

## Builtin Plugins

[Typescript](./src/plugins/typescript/mod.rs)

[Unity](./src/plugins/unity/mod.rs)
16 changes: 16 additions & 0 deletions crates/dojo-bindgen/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use cainome::parser::Error as CainomeError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
IO(#[from] std::io::Error),
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
Cainome(#[from] CainomeError),
#[error("Format error: {0}")]
Format(String),
}

pub type BindgenResult<T, E = Error> = Result<T, E>;
Loading

0 comments on commit 79278a6

Please sign in to comment.