Skip to content

Commit

Permalink
Add commemts
Browse files Browse the repository at this point in the history
  • Loading branch information
febo committed Nov 1, 2024
1 parent edc624b commit dccaaad
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
7 changes: 2 additions & 5 deletions include-idl-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::path::PathBuf;

// use goblin::error::Result;
use include_idl::parse::parse_idl_from_program_binary;

use clap::{Error, Parser, Subcommand};
use include_idl::parse::parse_idl_from_program_binary;
use std::path::PathBuf;

#[derive(Parser)]
#[command(version, about, long_about = None)]
Expand Down
2 changes: 0 additions & 2 deletions include-idl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name = "include-idl"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
shrink = ["flate2"]
parse = ["flate2", "goblin", "serde_json"]
Expand Down
25 changes: 25 additions & 0 deletions include-idl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This crate provides a way to include IDL files in a program binary.

pub mod parse;

#[cfg(feature = "shrink")]
Expand All @@ -6,6 +8,29 @@ mod shrink;
#[cfg(feature = "shrink")]
pub use shrink::compress_idl;

/// Include an IDL file in the program binary.
///
/// This macro creates two ELF sections in the program binary:
/// - `.idl.type` contains the type of the IDL file.
/// - `.idl.data` contains the IDL file itself.
///
/// In general you should use this macro in conbination with a `build.rs` build script
/// that compresses the IDL file to reduce the final size of the program binary.
///
/// # Arguments
///
/// This macro takes two arguments:
///
/// - `type`: The type of the IDL file. This should be one of the variants of the [`IdlType``] enum.
/// - `file`: The path to the IDL file.
///
/// # Example
///
/// Include the following in your `lib.rs` file:
///
/// ```ignore
/// include_idl!(IdlType::Codama, concat!(env!("OUT_DIR"), "/codama.idl.zip"));
/// ```
#[macro_export]
macro_rules! include_idl {
($type:path, $file:expr) => {
Expand Down
5 changes: 5 additions & 0 deletions include-idl/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ pub const IDL_TYPE_SECTION: &str = ".idl.type";
/// Name of the section containing the IDL data.
pub const IDL_DATA_SECTION: &str = ".idl.data";

/// `str` representation of the Anchor IDL type.
const ANCHOR_IDL_TYPE: &str = "anchor";

/// `str` representation of the Codama IDL type.
const CODAMA_IDL_TYPE: &str = "codama";

/// Defines the IDL type.
Expand Down Expand Up @@ -53,6 +56,7 @@ impl std::str::FromStr for IdlType {
}
}

/// Parses the IDL data from the program binary.
#[cfg(feature = "parse")]
pub fn parse_idl_from_program_binary(buffer: &[u8]) -> goblin::error::Result<(IdlType, Value)> {
let elf = Elf::parse(buffer)?;
Expand Down Expand Up @@ -97,6 +101,7 @@ pub fn parse_idl_from_program_binary(buffer: &[u8]) -> goblin::error::Result<(Id
}
}

/// Retrieves the location and size of an ELF section data.
#[cfg(feature = "parse")]
#[inline(always)]
fn get_section_data_offset(buffer: &[u8], offset: usize) -> (usize, usize) {
Expand Down
13 changes: 5 additions & 8 deletions include-idl/src/shrink.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
#[cfg(feature = "shrink")]
use flate2::{write::ZlibEncoder, Compression};
use std::{
fs::File,
io::{Read, Write},
path::PathBuf,
};

#[cfg(feature = "shrink")]
use flate2::{write::ZlibEncoder, Compression};

#[cfg(feature = "shrink")]
/// Compresses the IDL JSON file using zlib and writes the compressed data to a file.
pub fn compress_idl(idl_path: &PathBuf, dest_path: &PathBuf) {
let mut idl_json = File::open(idl_path).unwrap();
let mut json_contents = String::new();
idl_json
.read_to_string(&mut json_contents)
.expect("Failed to read JSON file");

// Compress the JSON contents using zlib
// Compress the JSON contents using zlib.
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder
.write_all(json_contents.as_bytes())
.expect("Failed to compress JSON data");
let compressed_data = encoder.finish().expect("Failed to finish compression");

// Get the output directory for the build script
// Write the compressed data to a file in the output directory
// Get the output directory for the build script and write the compressed
// data to a file in the output directory.
let mut output_file = File::create(dest_path).expect("Failed to create output file");
output_file
.write_all(&compressed_data)
Expand Down

0 comments on commit dccaaad

Please sign in to comment.