Skip to content

Commit

Permalink
idl: Move IDL types from the anchor-syn crate to the new IDL crate (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Apr 2, 2024
1 parent 475c694 commit c138a55
Show file tree
Hide file tree
Showing 41 changed files with 584 additions and 592 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- ts: `Program` instances use camelCase for everything ([#2824](https://github.com/coral-xyz/anchor/pull/2824)).
- ts: Remove discriminator functions ([#2824](https://github.com/coral-xyz/anchor/pull/2824)).
- ts: Remove `programId` parameter of the `Program` constructor ([#2864](https://github.com/coral-xyz/anchor/pull/2864)).
- idl, syn: Move IDL types from the `anchor-syn` crate to the new IDL crate ([#2882](https://github.com/coral-xyz/anchor/pull/2882)).

## [0.29.0] - 2023-10-16

Expand Down
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions idl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
build = [
"anyhow",
"regex",
"serde",
"serde_json",
]
build = ["anchor-syn", "regex"]

[dependencies]
anchor-syn = { path = "../lang/syn", version = "0.29.0", features = ["idl-types"] }
anyhow = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

# `build` feature only
anyhow = { version = "1", optional = true }
anchor-syn = { path = "../lang/syn", version = "0.29.0", optional = true }
regex = { version = "1", optional = true }
serde = { version = "1", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true }
32 changes: 32 additions & 0 deletions idl/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ use serde::Deserialize;

use crate::types::{Idl, IdlEvent, IdlTypeDef};

/// A trait that types must implement in order to include the type in the IDL definition.
///
/// This trait is automatically implemented for Anchor all types that use the `AnchorSerialize`
/// proc macro. Note that manually implementing the `AnchorSerialize` trait does **NOT** have the
/// same effect.
///
/// Types that don't implement this trait will cause a compile error during the IDL generation.
///
/// The default implementation of the trait allows the program to compile but the type does **NOT**
/// get included in the IDL.
pub trait IdlBuild {
/// Create an IDL type definition for the type.
///
/// The type is only included in the IDL if this method returns `Some`.
fn create_type() -> Option<IdlTypeDef> {
None
}

/// Insert all types that are included in the current type definition to the given map.
fn insert_types(_types: &mut BTreeMap<String, IdlTypeDef>) {}

/// Get the full module path of the type.
///
/// The full path will be used in the case of a conflicting type definition, e.g. when there
/// are multiple structs with the same name.
///
/// The default implementation covers most cases.
fn get_full_path() -> String {
std::any::type_name::<Self>().into()
}
}

/// Generate IDL via compilation.
pub fn build_idl(
program_path: impl AsRef<Path>,
Expand Down
3 changes: 3 additions & 0 deletions idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ pub mod types;

#[cfg(feature = "build")]
pub mod build;

#[cfg(feature = "build")]
pub use serde_json;
Loading

0 comments on commit c138a55

Please sign in to comment.