Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update bindgen for dojo.js sdk #2501

Merged
merged 10 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 38 additions & 2 deletions crates/dojo-bindgen/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use std::fmt;
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 +18,7 @@ pub enum BuiltinPlugins {
Typescript,
Unity,
TypeScriptV2,
Recs,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update BuiltinPlugins documentation to include Recs

With the addition of the Recs variant to the BuiltinPlugins enum, please ensure that any associated documentation or comments are updated accordingly.

}

impl fmt::Display for BuiltinPlugins {
Expand All @@ -24,16 +27,49 @@ 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"),
}
}
}

#[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 Vec<String>) -> BindgenResult<String>;
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Document the new code generation traits thoroughly

The introduction of BindgenWriter, BindgenModelGenerator, and BindgenContractGenerator traits is a significant enhancement. To assist other developers:

  • Provide clear documentation for each trait and their methods.
  • Explain the intended use cases and any important implementation details.
  • Ensure all parameters and return types are well-described.

Loading