lang: Rename some idl types (#344)

This commit is contained in:
Armani Ferrante 2021-05-30 23:21:09 -07:00 committed by GitHub
parent 2f5a9fcc7d
commit 571a96515e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 33 deletions

View File

@ -53,7 +53,7 @@ pub fn parse(filename: impl AsRef<Path>) -> Result<Idl> {
let accounts_strct = let accounts_strct =
accs.get(&method.anchor_ident.to_string()).unwrap(); accs.get(&method.anchor_ident.to_string()).unwrap();
let accounts = idl_accounts(accounts_strct, &accs); let accounts = idl_accounts(accounts_strct, &accs);
IdlStateMethod { IdlInstruction {
name, name,
args, args,
accounts, accounts,
@ -70,7 +70,7 @@ pub fn parse(filename: impl AsRef<Path>) -> Result<Idl> {
.iter() .iter()
.filter(|arg| match arg { .filter(|arg| match arg {
syn::FnArg::Typed(pat_ty) => { 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); let mut arg_str = parser::tts_to_string(&pat_ty.ty);
arg_str.retain(|c| !c.is_whitespace()); arg_str.retain(|c| !c.is_whitespace());
!arg_str.starts_with("Context<") !arg_str.starts_with("Context<")
@ -92,7 +92,7 @@ pub fn parse(filename: impl AsRef<Path>) -> Result<Idl> {
.collect(); .collect();
let accounts_strct = accs.get(&anchor_ident.to_string()).unwrap(); let accounts_strct = accs.get(&anchor_ident.to_string()).unwrap();
let accounts = idl_accounts(&accounts_strct, &accs); let accounts = idl_accounts(&accounts_strct, &accs);
IdlStateMethod { IdlInstruction {
name, name,
args, args,
accounts, accounts,
@ -118,9 +118,9 @@ pub fn parse(filename: impl AsRef<Path>) -> Result<Idl> {
.collect::<Vec<IdlField>>(), .collect::<Vec<IdlField>>(),
_ => panic!("State must be a struct"), _ => panic!("State must be a struct"),
}; };
IdlTypeDef { IdlTypeDefinition {
name: state.name, name: state.name,
ty: IdlTypeDefTy::Struct { fields }, ty: IdlTypeDefinitionTy::Struct { fields },
} }
}; };
@ -160,7 +160,7 @@ pub fn parse(filename: impl AsRef<Path>) -> Result<Idl> {
// todo: don't unwrap // todo: don't unwrap
let accounts_strct = accs.get(&ix.anchor_ident.to_string()).unwrap(); let accounts_strct = accs.get(&ix.anchor_ident.to_string()).unwrap();
let accounts = idl_accounts(accounts_strct, &accs); let accounts = idl_accounts(accounts_strct, &accs);
IdlIx { IdlInstruction {
name: ix.ident.to_string().to_mixed_case(), name: ix.ident.to_string().to_mixed_case(),
accounts, accounts,
args, args,
@ -359,7 +359,7 @@ fn parse_account_derives(f: &syn::File) -> HashMap<String, AccountsStruct> {
} }
// Parse all user defined types in the file. // Parse all user defined types in the file.
fn parse_ty_defs(f: &syn::File) -> Result<Vec<IdlTypeDef>> { fn parse_ty_defs(f: &syn::File) -> Result<Vec<IdlTypeDefinition>> {
f.items f.items
.iter() .iter()
.filter_map(|i| match i { .filter_map(|i| match i {
@ -387,9 +387,9 @@ fn parse_ty_defs(f: &syn::File) -> Result<Vec<IdlTypeDef>> {
_ => panic!("Only named structs are allowed."), _ => panic!("Only named structs are allowed."),
}; };
return Some(fields.map(|fields| IdlTypeDef { return Some(fields.map(|fields| IdlTypeDefinition {
name, name,
ty: IdlTypeDefTy::Struct { fields }, ty: IdlTypeDefinitionTy::Struct { fields },
})); }));
} }
None None
@ -421,12 +421,12 @@ fn parse_ty_defs(f: &syn::File) -> Result<Vec<IdlTypeDef>> {
Some(EnumFields::Named(fields)) Some(EnumFields::Named(fields))
} }
}; };
EnumVariant { name, fields } IdlEnumVariant { name, fields }
}) })
.collect::<Vec<EnumVariant>>(); .collect::<Vec<IdlEnumVariant>>();
Some(Ok(IdlTypeDef { Some(Ok(IdlTypeDefinition {
name, name,
ty: IdlTypeDefTy::Enum { variants }, ty: IdlTypeDefinitionTy::Enum { variants },
})) }))
} }
_ => None, _ => None,

View File

@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
pub mod file; pub mod file;
@ -6,38 +7,35 @@ pub mod file;
pub struct Idl { pub struct Idl {
pub version: String, pub version: String,
pub name: String, pub name: String,
pub instructions: Vec<IdlIx>, pub instructions: Vec<IdlInstruction>,
#[serde(skip_serializing_if = "Option::is_none", default)] #[serde(skip_serializing_if = "Option::is_none", default)]
pub state: Option<IdlState>, pub state: Option<IdlState>,
#[serde(skip_serializing_if = "Vec::is_empty", default)] #[serde(skip_serializing_if = "Vec::is_empty", default)]
pub accounts: Vec<IdlTypeDef>, pub accounts: Vec<IdlTypeDefinition>,
#[serde(skip_serializing_if = "Vec::is_empty", default)] #[serde(skip_serializing_if = "Vec::is_empty", default)]
pub types: Vec<IdlTypeDef>, pub types: Vec<IdlTypeDefinition>,
#[serde(skip_serializing_if = "Option::is_none", default)] #[serde(skip_serializing_if = "Option::is_none", default)]
pub events: Option<Vec<IdlEvent>>, pub events: Option<Vec<IdlEvent>>,
#[serde(skip_serializing_if = "Option::is_none", default)] #[serde(skip_serializing_if = "Option::is_none", default)]
pub errors: Option<Vec<IdlErrorCode>>, pub errors: Option<Vec<IdlErrorCode>>,
#[serde(skip_serializing_if = "Option::is_none", default)] #[serde(skip_serializing_if = "Option::is_none", default)]
pub metadata: Option<serde_json::Value>, pub metadata: Option<JsonValue>,
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlState { pub struct IdlState {
#[serde(rename = "struct")] #[serde(rename = "struct")]
pub strct: IdlTypeDef, pub strct: IdlTypeDefinition,
pub methods: Vec<IdlStateMethod>, pub methods: Vec<IdlInstruction>,
} }
pub type IdlStateMethod = IdlIx;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlIx { pub struct IdlInstruction {
pub name: String, pub name: String,
pub accounts: Vec<IdlAccountItem>, pub accounts: Vec<IdlAccountItem>,
pub args: Vec<IdlField>, pub args: Vec<IdlField>,
} }
// A single struct deriving `Accounts`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct IdlAccounts { pub struct IdlAccounts {
@ -52,7 +50,6 @@ pub enum IdlAccountItem {
IdlAccounts(IdlAccounts), IdlAccounts(IdlAccounts),
} }
// A single field in the accounts struct.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct IdlAccount { pub struct IdlAccount {
@ -83,21 +80,21 @@ pub struct IdlEventField {
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlTypeDef { pub struct IdlTypeDefinition {
pub name: String, pub name: String,
#[serde(rename = "type")] #[serde(rename = "type")]
pub ty: IdlTypeDefTy, pub ty: IdlTypeDefinitionTy,
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase", tag = "kind")] #[serde(rename_all = "lowercase", tag = "kind")]
pub enum IdlTypeDefTy { pub enum IdlTypeDefinitionTy {
Struct { fields: Vec<IdlField> }, Struct { fields: Vec<IdlField> },
Enum { variants: Vec<EnumVariant> }, Enum { variants: Vec<IdlEnumVariant> },
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EnumVariant { pub struct IdlEnumVariant {
pub name: String, pub name: String,
#[serde(skip_serializing_if = "Option::is_none", default)] #[serde(skip_serializing_if = "Option::is_none", default)]
pub fields: Option<EnumFields>, pub fields: Option<EnumFields>,
@ -133,14 +130,10 @@ pub enum IdlType {
Array(Box<IdlType>, usize), Array(Box<IdlType>, usize),
} }
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlTypePublicKey;
impl std::str::FromStr for IdlType { impl std::str::FromStr for IdlType {
type Err = anyhow::Error; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
// Eliminate whitespace.
let mut s = s.to_string(); let mut s = s.to_string();
s.retain(|c| !c.is_whitespace()); s.retain(|c| !c.is_whitespace());
let r = match s.as_str() { let r = match s.as_str() {