Skip to content

Commit

Permalink
fmted
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrinathNR committed Dec 4, 2024
1 parent 0d62fd4 commit ce74045
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
36 changes: 21 additions & 15 deletions src/rs_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::quote;
use std::collections::HashMap;
use swc_ecma_ast::{
BindingIdent, CallExpr, Callee, ClassExpr, ClassMethod, Expr, ExprOrSpread, Lit, MemberExpr,
NewExpr, Stmt, TsExprWithTypeArgs, TsInterfaceDecl, TsKeywordTypeKind, TsType,
TsTypeParamInstantiation, TsTypeRef,
BindingIdent, CallExpr, ClassExpr, ClassMethod, Expr, ExprOrSpread, Lit, MemberExpr, Stmt,
TsExprWithTypeArgs, TsInterfaceDecl, TsType, TsTypeParamInstantiation,
};

use crate::{
Expand All @@ -26,7 +25,7 @@ pub struct Ta {
pub struct Mint {
mint_authority_token: TokenStream,
decimals_token: TokenStream,
freeze_authority_token: Option<TokenStream>
freeze_authority_token: Option<TokenStream>,
}
#[derive(Clone, Debug)]

Expand Down Expand Up @@ -78,8 +77,12 @@ impl InstructionAccount {
let of_type = &self.of_type;
let constraints: TokenStream;
// this is evaluated this way coz, ta might not have seeds
if (self.mint.is_none() & self.seeds.is_none() & self.ta.is_none()) & (self.is_close | self.is_init | self.is_initifneeded) {
panic!(r##"use derive or deriveWithBump with all the necessary arguments while using "init" or "initIfNeeded" or "close" "##);
if (self.mint.is_none() & self.seeds.is_none() & self.ta.is_none())
& (self.is_close | self.is_init | self.is_initifneeded)
{
panic!(
r##"use derive or deriveWithBump with all the necessary arguments while using "init" or "initIfNeeded" or "close" "##
);
}
let payer = match &self.payer {
Some(s) => {
Expand Down Expand Up @@ -115,20 +118,20 @@ impl InstructionAccount {
let decimal_token = &m.decimals_token;
let mint_auth_token = &m.mint_authority_token;

if let Some(freeze_auth) = &m.freeze_authority_token {
quote!{
if let Some(freeze_auth) = &m.freeze_authority_token {
quote! {
mint::decimals = #decimal_token,
mint::authority = #mint_auth_token,
mint::freeze_authority = #freeze_auth,
}
} else {
quote!{
quote! {
mint::decimals = #decimal_token,
mint::authority = #mint_auth_token,
}
}
}
None => quote!{},
None => quote! {},
};
let close = match &self.close {
Some(c) => {
Expand Down Expand Up @@ -263,7 +266,11 @@ impl ProgramInstruction {
}
}

pub fn get_rs_arg_from_ts_arg(&mut self, ts_arg_expr: &Expr, is_account_struct: bool) -> Result<TokenStream> {
pub fn get_rs_arg_from_ts_arg(
&mut self,
ts_arg_expr: &Expr,
is_account_struct: bool,
) -> Result<TokenStream> {
let ts_arg: TokenStream;
let mut ix_attribute_token: Vec<TokenStream> = vec![];
match ts_arg_expr {
Expand Down Expand Up @@ -305,7 +312,6 @@ impl ProgramInstruction {
ctx.accounts.#ts_arg_obj_ident.#ts_arg_prop_ident
};
}

}
Expr::Ident(i) => {
let ts_arg_str = i.sym.as_ref();
Expand All @@ -329,9 +335,9 @@ impl ProgramInstruction {
};
}
Expr::Lit(Lit::Num(literal_value)) => {
let literal_token = Literal::u8_unsuffixed(literal_value.value as u8);
let literal_token = Literal::u8_unsuffixed(literal_value.value as u8);

ts_arg = quote!{#literal_token}
ts_arg = quote! {#literal_token}
}
_ => {
panic!("{:#?} not provided in proper format", ts_arg_expr)
Expand Down Expand Up @@ -770,7 +776,7 @@ impl ProgramInstruction {
if !seeds_token.is_empty() {
cur_ix_acc.seeds = Some(seeds_token);
}

}
if prop == "deriveWithBump" {
let bump_members = c.args.last().ok_or(anyhow!("no last element in vector"))?.expr.as_member().ok_or(PoseidonError::MemberNotFound)?;
Expand Down
8 changes: 5 additions & 3 deletions src/transpiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use core::panic;
use rust_format::{Formatter, PrettyPlease};
use std::{collections::HashMap, fs::{self, File}, io::Write};
use std::{
collections::HashMap,
fs::{self},
};

use crate::rs_types::{ProgramAccount, ProgramModule};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -66,7 +69,6 @@ pub fn transpile(module: &Module, output_file_name: &String) -> Result<()> {
let extracted_account_struct = extract_accounts_structs(&formatted_program);

for account_struct in extracted_account_struct {

let (header, reordered_account_struct) = reorder_struct(&account_struct)?;

formatted_program = replace_struct(&formatted_program, &header, &reordered_account_struct);
Expand Down Expand Up @@ -94,7 +96,7 @@ fn extract_accounts_structs(input: &str) -> Vec<String> {

fn reorder_struct(input: &str) -> Result<(String, String)> {
let field_regex = Regex::new(
r"(?ms)^(?P<attrs>(\s*#\[[^\]]*\](\s*|.*?))*?)\s*pub\s+(?P<name>\w+):\s+(?P<type>[^\n]+),"
r"(?ms)^(?P<attrs>(\s*#\[[^\]]*\](\s*|.*?))*?)\s*pub\s+(?P<name>\w+):\s+(?P<type>[^\n]+),",
)
.unwrap();

Expand Down

0 comments on commit ce74045

Please sign in to comment.