diff --git a/lang/syn/src/idl/file.rs b/lang/syn/src/idl/file.rs index 1e9b8e827..adb613f2f 100644 --- a/lang/syn/src/idl/file.rs +++ b/lang/syn/src/idl/file.rs @@ -53,7 +53,7 @@ pub fn parse(filename: impl AsRef) -> Result { let accounts_strct = accs.get(&method.anchor_ident.to_string()).unwrap(); let accounts = idl_accounts(accounts_strct, &accs); - IdlStateMethod { + IdlInstruction { name, args, accounts, @@ -70,7 +70,7 @@ pub fn parse(filename: impl AsRef) -> Result { .iter() .filter(|arg| match arg { syn::FnArg::Typed(pat_ty) => { - // TODO: this filtering should be donein the parser. + // TODO: this filtering should be done in the parser. let mut arg_str = parser::tts_to_string(&pat_ty.ty); arg_str.retain(|c| !c.is_whitespace()); !arg_str.starts_with("Context<") @@ -92,7 +92,7 @@ pub fn parse(filename: impl AsRef) -> Result { .collect(); let accounts_strct = accs.get(&anchor_ident.to_string()).unwrap(); let accounts = idl_accounts(&accounts_strct, &accs); - IdlStateMethod { + IdlInstruction { name, args, accounts, @@ -118,9 +118,9 @@ pub fn parse(filename: impl AsRef) -> Result { .collect::>(), _ => panic!("State must be a struct"), }; - IdlTypeDef { + IdlTypeDefinition { name: state.name, - ty: IdlTypeDefTy::Struct { fields }, + ty: IdlTypeDefinitionTy::Struct { fields }, } }; @@ -160,7 +160,7 @@ pub fn parse(filename: impl AsRef) -> Result { // todo: don't unwrap let accounts_strct = accs.get(&ix.anchor_ident.to_string()).unwrap(); let accounts = idl_accounts(accounts_strct, &accs); - IdlIx { + IdlInstruction { name: ix.ident.to_string().to_mixed_case(), accounts, args, @@ -359,7 +359,7 @@ fn parse_account_derives(f: &syn::File) -> HashMap { } // Parse all user defined types in the file. -fn parse_ty_defs(f: &syn::File) -> Result> { +fn parse_ty_defs(f: &syn::File) -> Result> { f.items .iter() .filter_map(|i| match i { @@ -387,9 +387,9 @@ fn parse_ty_defs(f: &syn::File) -> Result> { _ => panic!("Only named structs are allowed."), }; - return Some(fields.map(|fields| IdlTypeDef { + return Some(fields.map(|fields| IdlTypeDefinition { name, - ty: IdlTypeDefTy::Struct { fields }, + ty: IdlTypeDefinitionTy::Struct { fields }, })); } None @@ -421,12 +421,12 @@ fn parse_ty_defs(f: &syn::File) -> Result> { Some(EnumFields::Named(fields)) } }; - EnumVariant { name, fields } + IdlEnumVariant { name, fields } }) - .collect::>(); - Some(Ok(IdlTypeDef { + .collect::>(); + Some(Ok(IdlTypeDefinition { name, - ty: IdlTypeDefTy::Enum { variants }, + ty: IdlTypeDefinitionTy::Enum { variants }, })) } _ => None, diff --git a/lang/syn/src/idl/mod.rs b/lang/syn/src/idl/mod.rs index 6a519435c..44c0b353c 100644 --- a/lang/syn/src/idl/mod.rs +++ b/lang/syn/src/idl/mod.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use serde_json::Value as JsonValue; pub mod file; @@ -6,38 +7,35 @@ pub mod file; pub struct Idl { pub version: String, pub name: String, - pub instructions: Vec, + pub instructions: Vec, #[serde(skip_serializing_if = "Option::is_none", default)] pub state: Option, #[serde(skip_serializing_if = "Vec::is_empty", default)] - pub accounts: Vec, + pub accounts: Vec, #[serde(skip_serializing_if = "Vec::is_empty", default)] - pub types: Vec, + pub types: Vec, #[serde(skip_serializing_if = "Option::is_none", default)] pub events: Option>, #[serde(skip_serializing_if = "Option::is_none", default)] pub errors: Option>, #[serde(skip_serializing_if = "Option::is_none", default)] - pub metadata: Option, + pub metadata: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct IdlState { #[serde(rename = "struct")] - pub strct: IdlTypeDef, - pub methods: Vec, + pub strct: IdlTypeDefinition, + pub methods: Vec, } -pub type IdlStateMethod = IdlIx; - #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -pub struct IdlIx { +pub struct IdlInstruction { pub name: String, pub accounts: Vec, pub args: Vec, } -// A single struct deriving `Accounts`. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct IdlAccounts { @@ -52,7 +50,6 @@ pub enum IdlAccountItem { IdlAccounts(IdlAccounts), } -// A single field in the accounts struct. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct IdlAccount { @@ -83,21 +80,21 @@ pub struct IdlEventField { } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -pub struct IdlTypeDef { +pub struct IdlTypeDefinition { pub name: String, #[serde(rename = "type")] - pub ty: IdlTypeDefTy, + pub ty: IdlTypeDefinitionTy, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "lowercase", tag = "kind")] -pub enum IdlTypeDefTy { +pub enum IdlTypeDefinitionTy { Struct { fields: Vec }, - Enum { variants: Vec }, + Enum { variants: Vec }, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -pub struct EnumVariant { +pub struct IdlEnumVariant { pub name: String, #[serde(skip_serializing_if = "Option::is_none", default)] pub fields: Option, @@ -133,14 +130,10 @@ pub enum IdlType { Array(Box, usize), } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -pub struct IdlTypePublicKey; - impl std::str::FromStr for IdlType { type Err = anyhow::Error; fn from_str(s: &str) -> Result { - // Eliminate whitespace. let mut s = s.to_string(); s.retain(|c| !c.is_whitespace()); let r = match s.as_str() {