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: Reintroduce Abigen publicly in a new crate fuels-code-gen #819

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ members = [
"examples/types",
"examples/wallets",
"packages/fuels",
"packages/fuels-code-gen",
"packages/fuels-core",
"packages/fuels-macros",
"packages/fuels-programs",
"packages/fuels-core",
"packages/fuels-signers",
"packages/fuels-test-helpers",
"packages/fuels-types",
Expand Down Expand Up @@ -49,6 +50,7 @@ fuel-core-chain-config = "0.16"
fuel-core-types = "0.16"
fuel-vm = "0.25"
fuels = { version = "0.35.0", path = "./packages/fuels" }
fuels-code-gen = { version = "0.35.0", path = "./packages/fuels-code-gen" }
fuels-core = { version = "0.35.0", path = "./packages/fuels-core" }
fuels-macros = { version = "0.35.0", path = "./packages/fuels-macros" }
fuels-programs = { version = "0.35.0", path = "./packages/fuels-programs" }
Expand Down
21 changes: 21 additions & 0 deletions packages/fuels-code-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "fuels-code-gen"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
description = "Used for code generation in the Fuel Rust SDK"

[dependencies]
Inflector = "0.11.4"
fuel-abi-types = "0.2.0"
itertools = "0.10.5"
proc-macro2 = "1.0.50"
quote = "1.0.23"
serde_json = "1.0.91"
lazy_static = "1.4.0"
regex = "1.7.1"
syn = { version = "1.0.107" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ use std::{
io,
};

pub(crate) struct Error(pub(crate) String);
pub struct Error(pub String);

impl Error {
pub(crate) fn combine<T: Into<Self>>(self, err: T) -> Self {
pub fn combine<T: Into<Self>>(self, err: T) -> Self {
error!("{} {}", self.0, err.into().0)
}
}

#[macro_export]
macro_rules! error {
($fmt_str: literal $(,$arg: expr)*) => {crate::error::Error(format!($fmt_str,$($arg),*))}
($fmt_str: literal $(,$arg: expr)*) => {$crate::error::Error(format!($fmt_str,$($arg),*))}
}

pub(crate) use error;
pub use error;

pub(crate) type Result<T> = std::result::Result<T, Error>;
pub type Result<T> = std::result::Result<T, Error>;

impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand Down
5 changes: 5 additions & 0 deletions packages/fuels-code-gen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use program_bindings::*;

pub mod error;
mod program_bindings;
pub mod utils;
9 changes: 9 additions & 0 deletions packages/fuels-code-gen/src/program_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod abi_types;
mod abigen;
mod custom_types;
mod generated_code;
mod resolved_type;
mod source;
mod utils;

pub use abigen::{Abigen, AbigenTarget, ProgramType};
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use proc_macro2::TokenStream;
use quote::quote;

use crate::{
abigen::code_gen::{
error::Result,
program_bindings::{
abi_types::FullTypeDeclaration,
abigen::{abigen_target::ParsedAbigenTarget, bindings::generate_bindings},
custom_types::generate_types,
generated_code::GeneratedCode,
},
error::Result,
utils::ident,
};

Expand All @@ -32,7 +32,7 @@ impl Abigen {
/// * `targets`: `AbigenTargets` detailing which ABI to generate bindings
/// for, and of what nature (Contract, Script or Predicate).
/// * `no_std`: don't use the Rust std library.
pub(crate) fn generate(targets: Vec<AbigenTarget>, no_std: bool) -> Result<TokenStream> {
pub fn generate(targets: Vec<AbigenTarget>, no_std: bool) -> Result<TokenStream> {
let parsed_targets = Self::parse_targets(targets)?;

let generated_code = Self::generate_code(no_std, parsed_targets)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::convert::TryFrom;

use crate::{
abigen::code_gen::{abi_types::FullProgramABI, source::Source},
error::{Error, Result},
program_bindings::{abi_types::FullProgramABI, source::Source},
};

#[derive(Debug, Clone)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashSet;

use crate::{
abigen::code_gen::{
error::Result,
program_bindings::{
abi_types::FullTypeDeclaration,
abigen::{
abigen_target::ParsedAbigenTarget,
Expand All @@ -12,7 +13,6 @@ use crate::{
},
generated_code::GeneratedCode,
},
error::Result,
utils::ident,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use proc_macro2::{Ident, TokenStream};
use quote::{quote, TokenStreamExt};

use crate::{
abigen::code_gen::{
error::Result,
program_bindings::{
abi_types::{FullABIFunction, FullProgramABI, FullTypeDeclaration},
abigen::{
bindings::function_generator::FunctionGenerator, logs::logs_lookup_instantiation_code,
},
generated_code::GeneratedCode,
type_path::TypePath,
},
error::Result,
utils::ident,
utils::{ident, TypePath},
};

pub(crate) fn contract_bindings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

use crate::{
abigen::code_gen::{
error::{error, Result},
program_bindings::{
abi_types::{FullABIFunction, FullTypeApplication, FullTypeDeclaration},
resolved_type::{resolve_type, ResolvedType},
utils::{param_type_calls, Component},
},
error::{error, Result},
utils::safe_ident,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::{
abigen::code_gen::{
error::Result,
program_bindings::{
abi_types::{FullProgramABI, FullTypeDeclaration},
abigen::bindings::{function_generator::FunctionGenerator, utils::extract_main_fn},
generated_code::GeneratedCode,
type_path::TypePath,
},
error::Result,
utils::TypePath,
};

pub(crate) fn predicate_bindings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::{
abigen::code_gen::{
error::Result,
program_bindings::{
abi_types::{FullProgramABI, FullTypeDeclaration},
abigen::{
bindings::{function_generator::FunctionGenerator, utils::extract_main_fn},
logs::logs_lookup_instantiation_code,
},
generated_code::GeneratedCode,
type_path::TypePath,
},
error::Result,
utils::TypePath,
};

pub(crate) fn script_bindings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abigen::code_gen::abi_types::FullABIFunction,
error::{error, Result},
program_bindings::abi_types::FullABIFunction,
};

pub(crate) fn extract_main_fn(abi: &[FullABIFunction]) -> Result<&FullABIFunction> {
Expand All @@ -26,7 +26,7 @@ pub(crate) fn extract_main_fn(abi: &[FullABIFunction]) -> Result<&FullABIFunctio
#[cfg(test)]
mod tests {
use super::*;
use crate::abigen::code_gen::abi_types::{FullTypeApplication, FullTypeDeclaration};
use crate::program_bindings::abi_types::{FullTypeApplication, FullTypeDeclaration};

#[test]
fn correctly_extracts_the_main_fn() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashSet;
use proc_macro2::TokenStream;
use quote::quote;

use crate::abigen::code_gen::{
use crate::program_bindings::{
abi_types::{FullLoggedType, FullTypeDeclaration},
resolved_type::resolve_type,
utils::single_param_type_call,
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-code-gen/src/program_bindings/code_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use fuel_abi_types::utils::extract_custom_type_name;
use itertools::Itertools;

use crate::{
abigen::code_gen::{
error::Result,
program_bindings::{
abi_types::FullTypeDeclaration,
custom_types::{enums::expand_custom_enum, structs::expand_custom_struct},
generated_code::GeneratedCode,
utils::get_sdk_provided_types,
},
error::Result,
};

mod enums;
Expand Down Expand Up @@ -87,7 +87,7 @@ mod tests {
use quote::quote;

use super::*;
use crate::abigen::code_gen::{abi_types::FullTypeApplication, type_path::TypePath};
use crate::{program_bindings::abi_types::FullTypeApplication, utils::TypePath};

#[test]
fn test_expand_custom_enum() -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::{
abigen::code_gen::{
error::{error, Result},
program_bindings::{
abi_types::FullTypeDeclaration,
custom_types::utils::{extract_components, extract_generic_parameters},
generated_code::GeneratedCode,
type_path::TypePath,
utils::Component,
},
error::{error, Result},
utils::ident,
utils::{ident, TypePath},
};

/// Returns a TokenStream containing the declaration, `Parameterize`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::{
abigen::code_gen::{
error::{error, Result},
program_bindings::{
abi_types::FullTypeDeclaration,
custom_types::utils::{extract_components, extract_generic_parameters},
generated_code::GeneratedCode,
type_path::TypePath,
utils::Component,
},
error::{error, Result},
utils::ident,
utils::{ident, TypePath},
};

/// Returns a TokenStream containing the declaration, `Parameterize`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use proc_macro2::TokenStream;
use quote::quote;

use crate::{
abigen::code_gen::{abi_types::FullTypeDeclaration, utils::Component},
error::Result,
program_bindings::{abi_types::FullTypeDeclaration, utils::Component},
utils::ident,
};

Expand Down Expand Up @@ -47,7 +47,7 @@ mod tests {
use fuel_abi_types::{program_abi::TypeDeclaration, utils::extract_custom_type_name};

use super::*;
use crate::abigen::code_gen::{resolved_type::ResolvedType, utils::param_type_calls};
use crate::program_bindings::{resolved_type::ResolvedType, utils::param_type_calls};

#[test]
fn extracts_generic_types() -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use itertools::Itertools;
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::abigen::code_gen::type_path::TypePath;
use crate::utils::TypePath;

#[derive(Default, Debug)]
pub(crate) struct GeneratedCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use quote::{quote, ToTokens};
use regex::Regex;

use crate::{
abigen::code_gen::{
error::{error, Result},
program_bindings::{
abi_types::{FullTypeApplication, FullTypeDeclaration},
type_path::TypePath,
utils::get_sdk_provided_types,
},
error::{error, Result},
utils::{ident, safe_ident},
utils::{ident, safe_ident, TypePath},
};

// Represents a type alongside its generic parameters. Can be converted into a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use proc_macro2::{Ident, TokenStream};
use quote::{quote, ToTokens};

use crate::{
abigen::code_gen::{
error::Result,
program_bindings::{
abi_types::{FullTypeApplication, FullTypeDeclaration},
resolved_type::{resolve_type, ResolvedType},
type_path::TypePath,
},
error::Result,
utils::safe_ident,
utils::{safe_ident, TypePath},
};

// Represents a component of either a struct(field name) or an enum(variant
Expand Down
5 changes: 5 additions & 0 deletions packages/fuels-code-gen/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use ident::{ident, safe_ident};
pub use type_path::TypePath;

mod ident;
mod type_path;
10 changes: 10 additions & 0 deletions packages/fuels-code-gen/src/utils/ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use proc_macro2::{Ident, Span};

/// Expands a identifier string into an token.
pub fn ident(name: &str) -> Ident {
Ident::new(name, Span::call_site())
}

pub fn safe_ident(name: &str) -> Ident {
syn::parse_str::<Ident>(name).unwrap_or_else(|_| ident(&format!("{name}_")))
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use quote::{quote, ToTokens};
use crate::error::{error, Result};

#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) struct TypePath {
pub struct TypePath {
parts: Vec<String>,
}

Expand Down
1 change: 1 addition & 0 deletions packages/fuels-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rand = "0.8"
syn = { version = "1.0.107", features = ["extra-traits"] }
itertools = "0.10.5"
serde_json = "1.0.91"
fuels-code-gen = { workspace = true }

[dev-dependencies]
trybuild = "1.0.73"
Expand Down
Loading