solana_idl/
lib.rs

1use anchor_lang_idl::types::Idl as NewIdl;
2use errors::{IdlError, IdlResult};
3pub use solana_idl_classic::*;
4use solana_idl_converter::anchor_to_classic;
5
6pub mod errors;
7pub fn try_extract_classic_idl(json: &str) -> IdlResult<Idl> {
8    // First try to parse the JSON as a classic IDL
9    match serde_json::from_str::<Idl>(json) {
10        Ok(idl) => Ok(idl),
11        Err(parse_as_classic_err) => {
12            // If that fails, try to parse it as a new IDL
13            let new_idl = match serde_json::from_str::<NewIdl>(json) {
14                Ok(new_idl) => new_idl,
15                Err(parse_as_new_err) => {
16                    return Err(IdlError::IdlCouldNotBeParsed(
17                        parse_as_classic_err,
18                        parse_as_new_err,
19                    ))
20                }
21            };
22            anchor_to_classic::try_convert(new_idl).map_err(|conversion_err| {
23                IdlError::IdlCouldNotBeConverted(
24                    parse_as_classic_err,
25                    conversion_err,
26                )
27            })
28        }
29    }
30}