Skip to content

Commit

Permalink
feat: update bindgen for dojo.js sdk (#2501)
Browse files Browse the repository at this point in the history
* feat: update bindgen for dojo.js sdk

* feat: generate models

* feat: generate contracts bindings

* feat: add recs for retrocompat

* fix: big contracts types

* refac: use specific type alias to avoid duplication

* feat: add torii erc support

* fix: fmt

* fix: fix fmt

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
MartianGreed and glihm authored Oct 18, 2024
1 parent 90a6580 commit f037f84
Show file tree
Hide file tree
Showing 14 changed files with 2,153 additions and 606 deletions.
1 change: 1 addition & 0 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 crates/dojo-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async-trait.workspace = true
camino.workspace = true
chrono.workspace = true
convert_case.workspace = true
log.workspace = true
serde.workspace = true
serde_json.workspace = true
starknet.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/dojo-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod error;
use error::BindgenResult;

mod plugins;
use plugins::recs::TypescriptRecsPlugin;
use plugins::typescript::TypescriptPlugin;
use plugins::typescript_v2::TypeScriptV2Plugin;
use plugins::unity::UnityPlugin;
Expand Down Expand Up @@ -89,6 +90,7 @@ impl PluginManager {
BuiltinPlugins::Typescript => Box::new(TypescriptPlugin::new()),
BuiltinPlugins::Unity => Box::new(UnityPlugin::new()),
BuiltinPlugins::TypeScriptV2 => Box::new(TypeScriptV2Plugin::new()),
BuiltinPlugins::Recs => Box::new(TypescriptRecsPlugin::new()),
};

let files = builder.generate_code(&data).await?;
Expand Down
78 changes: 76 additions & 2 deletions crates/dojo-bindgen/src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::collections::HashMap;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;

use async_trait::async_trait;
use cainome::parser::tokens::{Composite, Function};

use crate::error::BindgenResult;
use crate::DojoData;
use crate::{DojoContract, DojoData};

pub mod recs;
pub mod typescript;
pub mod typescript_v2;
pub mod unity;
Expand All @@ -16,6 +19,7 @@ pub enum BuiltinPlugins {
Typescript,
Unity,
TypeScriptV2,
Recs,
}

impl fmt::Display for BuiltinPlugins {
Expand All @@ -24,16 +28,86 @@ impl fmt::Display for BuiltinPlugins {
BuiltinPlugins::Typescript => write!(f, "typescript"),
BuiltinPlugins::Unity => write!(f, "unity"),
BuiltinPlugins::TypeScriptV2 => write!(f, "typescript_v2"),
BuiltinPlugins::Recs => write!(f, "recs"),
}
}
}

pub struct Buffer(Vec<String>);
impl Buffer {
pub fn new() -> Self {
Self(Vec::new())
}

pub fn has(&self, s: &str) -> bool {
self.0.iter().any(|b| b.contains(s))
}

pub fn push(&mut self, s: String) {
self.0.push(s.clone());
}

pub fn insert_after(&mut self, s: String, pos: &str, sep: &str, idx: usize) {
let pos = self.0.iter().position(|b| b.contains(pos)).unwrap();
if let Some(st) = self.0.get_mut(pos) {
let indices = st.match_indices(sep).map(|(i, _)| i).collect::<Vec<usize>>();
let append_after = indices[indices.len() - idx] + 1;
st.insert_str(append_after, &s);
}
}
pub fn join(&mut self, sep: &str) -> String {
self.0.join(sep)
}
}

impl Deref for Buffer {
type Target = Vec<String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Buffer {
fn deref_mut(&mut self) -> &mut Vec<String> {
&mut self.0
}
}

#[async_trait]
pub trait BuiltinPlugin {
pub trait BuiltinPlugin: Sync {
/// Generates code by executing the plugin.
///
/// # Arguments
///
/// * `data` - Dojo data gathered from the compiled project.
async fn generate_code(&self, data: &DojoData) -> BindgenResult<HashMap<PathBuf, Vec<u8>>>;
}

pub trait BindgenWriter: Sync {
/// Writes the generated code to the specified path.
///
/// # Arguments
///
/// * `code` - The generated code.
fn write(&self, path: &str, data: &DojoData) -> BindgenResult<(PathBuf, Vec<u8>)>;
fn get_path(&self) -> &str;
}

pub trait BindgenModelGenerator: Sync {
/// Generates code by executing the plugin.
/// The generated code is written to the specified path.
/// This will write file sequentially (for now) so we need one generator per part of the file.
/// (header, type definitions, interfaces, functions and so on)
/// TODO: add &mut ref to what's currently generated to place specific code at specific places.
///
/// # Arguments
fn generate(&self, token: &Composite, buffer: &mut Buffer) -> BindgenResult<String>;
}

pub trait BindgenContractGenerator: Sync {
fn generate(
&self,
contract: &DojoContract,
token: &Function,
buffer: &mut Buffer,
) -> BindgenResult<String>;
}
Loading

0 comments on commit f037f84

Please sign in to comment.