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

PR testing. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions crates/anchor-idl/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ pub fn generate_account(

let doc = format!(" Account: {}", account_name);
let struct_name = format_ident!("{}", account_name);
let fields_rendered = generate_fields(fields);
let fields_rendered = generate_fields(fields, true);
quote! {
#derive_account
#[doc = #doc]
#derive_copy
#derive_default
pub struct #struct_name {
#fields_rendered
}
Expand Down
41 changes: 29 additions & 12 deletions crates/anchor-idl/src/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,20 @@ pub fn get_type_properties(defs: &[IdlTypeDefinition], ty: &IdlType) -> FieldLis
}

/// Generates struct fields from a list of [IdlField]s.
pub fn generate_fields(fields: &[IdlField]) -> TokenStream {
pub fn generate_fields(fields: &[IdlField], public: bool) -> TokenStream {
let fields_rendered = fields.iter().map(|arg| {
let name = format_ident!("{}", arg.name.to_snake_case());
let type_name = crate::ty_to_rust_type(&arg.ty);
let stream: proc_macro2::TokenStream = type_name.parse().unwrap();
quote! {
pub #name: #stream

if public {
quote! {
pub #name: #stream
}
} else {
quote! {
#name: #stream
}
}
});
quote! {
Expand All @@ -139,7 +146,7 @@ pub fn generate_struct(
fields: &[IdlField],
opts: StructOpts,
) -> TokenStream {
let fields_rendered = generate_fields(fields);
let fields_rendered = generate_fields(fields, true);
let props = get_field_list_properties(defs, fields);

let derive_default = if props.can_derive_default {
Expand Down Expand Up @@ -180,7 +187,6 @@ pub fn generate_struct(
quote! {
#derive_serializers
#[derive(Debug)]
#derive_default
pub struct #struct_name {
#fields_rendered
}
Expand All @@ -193,7 +199,24 @@ pub fn generate_enum(
enum_name: &Ident,
variants: &[IdlEnumVariant],
) -> TokenStream {
let variant_idents = variants.iter().map(|v| format_ident!("{}", v.name));
let variant_idents = variants.iter().map(|v| {
let name = format_ident!("{}", v.name);

match &v.fields {
Some(EnumFields::Named(fields)) => {
let fields_rendered = generate_fields(fields, false);
quote! {
#name { #fields_rendered }
}
}
Some(EnumFields::Tuple(_fields)) => {
quote! { #name }
} // TODO: fix this
None => {
quote! { #name }
}
}
});
let props = get_variant_list_properties(defs, variants);

let derive_copy = if props.can_copy {
Expand All @@ -212,12 +235,6 @@ pub fn generate_enum(
pub enum #enum_name {
#(#variant_idents),*
}

impl Default for #enum_name {
fn default() -> Self {
Self::#default_variant
}
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions examples/jupiter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Created by https://www.toptal.com/developers/gitignore/api/rust
# Edit at https://www.toptal.com/developers/gitignore?templates=rust

### Rust ###
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# End of https://www.toptal.com/developers/gitignore/api/rust

20 changes: 20 additions & 0 deletions examples/jupiter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "jupiter"
version = "4.0.0"
edition = "2021"
description = "Autogenerated CPI client for the Jupiter program."
authors = ["Siong Ong <[email protected]>"]
repository = "https://github.com/saber-hq/anchor-gen"
license = "Apache-2.0"
keywords = ["solana", "anchor"]

[features]
default = ["cpi"]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]

[dependencies]
anchor-gen = { version = "0.3.0", path = "../../crates/anchor-gen" }
anchor-lang = ">=0.20"
Empty file added examples/jupiter/README.md
Empty file.
Loading