use std::io::Read;
use flate2::read::ZlibDecoder;
use solana_idl::Idl;
use super::IDL_HEADER_SIZE;
use crate::errors::{ChainparserError, ChainparserResult};
pub fn try_parse_idl_json(json: &str) -> ChainparserResult<Idl> {
Ok(solana_idl::try_extract_classic_idl(json)?)
}
pub fn decode_idl_account_data(
account_data: &[u8],
) -> ChainparserResult<(Idl, String)> {
decode_idl_data(&account_data[IDL_HEADER_SIZE..])
}
pub fn unzip_idl_account_json(bytes: &[u8]) -> ChainparserResult<String> {
unzip_bytes(&bytes[IDL_HEADER_SIZE..])
}
fn decode_idl_data(data: &[u8]) -> ChainparserResult<(Idl, String)> {
let json = unzip_bytes(data)?;
let idl: Idl = solana_idl::try_extract_classic_idl(&json)?;
Ok((idl, json))
}
fn unzip_bytes(bytes: &[u8]) -> ChainparserResult<String> {
let mut zlib = ZlibDecoder::new(bytes);
let mut write = String::new();
zlib.read_to_string(&mut write).map_err(|err| {
ChainparserError::IdlContainerShouldContainZlibData(err.to_string())
})?;
Ok(write)
}