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 8 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
79 changes: 77 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,
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 +28,87 @@ 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
Loading