123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- use serde::{Deserialize, Serialize};
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct Idl {
- pub version: String,
- pub name: String,
- pub instructions: Vec<IdlInstruction>,
- #[serde(skip_serializing_if = "Option::is_none", default)]
- pub state: Option<IdlState>,
- #[serde(skip_serializing_if = "Vec::is_empty", default)]
- pub accounts: Vec<IdlTypeDef>,
- #[serde(skip_serializing_if = "Vec::is_empty", default)]
- pub types: Vec<IdlTypeDef>,
- #[serde(skip_serializing_if = "Option::is_none", default)]
- pub errors: Option<Vec<IdlErrorCode>>,
- #[serde(skip_serializing_if = "Option::is_none", default)]
- pub metadata: Option<serde_json::Value>,
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct IdlState {
- #[serde(rename = "struct")]
- pub strct: IdlTypeDef,
- pub methods: Vec<IdlStateMethod>,
- }
- pub type IdlStateMethod = IdlInstruction;
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct IdlInstruction {
- pub name: String,
- pub accounts: Vec<IdlAccountItem>,
- pub args: Vec<IdlField>,
- }
- // A single struct deriving `Accounts`.
- #[derive(Debug, Clone, Serialize, Deserialize)]
- #[serde(rename_all = "camelCase")]
- pub struct IdlAccounts {
- pub name: String,
- pub accounts: Vec<IdlAccountItem>,
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- #[serde(untagged)]
- pub enum IdlAccountItem {
- IdlAccount(IdlAccount),
- IdlAccounts(IdlAccounts),
- }
- // A single field in the accounts struct.
- #[derive(Debug, Clone, Serialize, Deserialize)]
- #[serde(rename_all = "camelCase")]
- pub struct IdlAccount {
- pub name: String,
- pub is_mut: bool,
- pub is_signer: bool,
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct IdlField {
- pub name: String,
- #[serde(rename = "type")]
- pub ty: IdlType,
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct IdlTypeDef {
- pub name: String,
- #[serde(rename = "type")]
- pub ty: IdlTypeDefTy,
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- #[serde(rename_all = "lowercase", tag = "kind")]
- pub enum IdlTypeDefTy {
- Struct { fields: Vec<IdlField> },
- Enum { variants: Vec<EnumVariant> },
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct EnumVariant {
- pub name: String,
- #[serde(skip_serializing_if = "Option::is_none", default)]
- pub fields: Option<EnumFields>,
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- #[serde(untagged)]
- pub enum EnumFields {
- Named(Vec<IdlField>),
- Tuple(Vec<IdlType>),
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- #[serde(rename_all = "camelCase")]
- pub enum IdlType {
- Bool,
- U8,
- I8,
- U16,
- I16,
- U32,
- I32,
- U64,
- I64,
- U128,
- I128,
- Bytes,
- String,
- PublicKey,
- Defined(String),
- Option(Box<IdlType>),
- Vec(Box<IdlType>),
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct IdlTypePublicKey;
- impl std::str::FromStr for IdlType {
- type Err = anyhow::Error;
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- // Eliminate whitespace.
- let mut s = s.to_string();
- s.retain(|c| !c.is_whitespace());
- let r = match s.as_str() {
- "bool" => IdlType::Bool,
- "u8" => IdlType::U8,
- "i8" => IdlType::I8,
- "u16" => IdlType::U16,
- "i16" => IdlType::I16,
- "u32" => IdlType::U32,
- "i32" => IdlType::I32,
- "u64" => IdlType::U64,
- "i64" => IdlType::I64,
- "u128" => IdlType::U128,
- "i128" => IdlType::I128,
- "Vec<u8>" => IdlType::Bytes,
- "String" => IdlType::String,
- "Pubkey" => IdlType::PublicKey,
- _ => match s.to_string().strip_prefix("Option<") {
- None => match s.to_string().strip_prefix("Vec<") {
- None => IdlType::Defined(s.to_string()),
- Some(inner) => {
- let inner_ty = Self::from_str(
- inner
- .strip_suffix(">")
- .ok_or(anyhow::anyhow!("Invalid option"))?,
- )?;
- IdlType::Vec(Box::new(inner_ty))
- }
- },
- Some(inner) => {
- let inner_ty = Self::from_str(
- inner
- .strip_suffix(">")
- .ok_or(anyhow::anyhow!("Invalid option"))?,
- )?;
- IdlType::Option(Box::new(inner_ty))
- }
- },
- };
- Ok(r)
- }
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- pub struct IdlErrorCode {
- pub code: u32,
- pub name: String,
- #[serde(skip_serializing_if = "Option::is_none", default)]
- pub msg: Option<String>,
- }
|