Skip to content

Commit dccaaad

Browse files
committed
Add commemts
1 parent edc624b commit dccaaad

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

include-idl-cli/src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use std::path::PathBuf;
2-
3-
// use goblin::error::Result;
4-
use include_idl::parse::parse_idl_from_program_binary;
5-
61
use clap::{Error, Parser, Subcommand};
2+
use include_idl::parse::parse_idl_from_program_binary;
3+
use std::path::PathBuf;
74

85
#[derive(Parser)]
96
#[command(version, about, long_about = None)]

include-idl/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name = "include-idl"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
86
[features]
97
shrink = ["flate2"]
108
parse = ["flate2", "goblin", "serde_json"]

include-idl/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! This crate provides a way to include IDL files in a program binary.
2+
13
pub mod parse;
24

35
#[cfg(feature = "shrink")]
@@ -6,6 +8,29 @@ mod shrink;
68
#[cfg(feature = "shrink")]
79
pub use shrink::compress_idl;
810

11+
/// Include an IDL file in the program binary.
12+
///
13+
/// This macro creates two ELF sections in the program binary:
14+
/// - `.idl.type` contains the type of the IDL file.
15+
/// - `.idl.data` contains the IDL file itself.
16+
///
17+
/// In general you should use this macro in conbination with a `build.rs` build script
18+
/// that compresses the IDL file to reduce the final size of the program binary.
19+
///
20+
/// # Arguments
21+
///
22+
/// This macro takes two arguments:
23+
///
24+
/// - `type`: The type of the IDL file. This should be one of the variants of the [`IdlType``] enum.
25+
/// - `file`: The path to the IDL file.
26+
///
27+
/// # Example
28+
///
29+
/// Include the following in your `lib.rs` file:
30+
///
31+
/// ```ignore
32+
/// include_idl!(IdlType::Codama, concat!(env!("OUT_DIR"), "/codama.idl.zip"));
33+
/// ```
934
#[macro_export]
1035
macro_rules! include_idl {
1136
($type:path, $file:expr) => {

include-idl/src/parse.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ pub const IDL_TYPE_SECTION: &str = ".idl.type";
1313
/// Name of the section containing the IDL data.
1414
pub const IDL_DATA_SECTION: &str = ".idl.data";
1515

16+
/// `str` representation of the Anchor IDL type.
1617
const ANCHOR_IDL_TYPE: &str = "anchor";
18+
19+
/// `str` representation of the Codama IDL type.
1720
const CODAMA_IDL_TYPE: &str = "codama";
1821

1922
/// Defines the IDL type.
@@ -53,6 +56,7 @@ impl std::str::FromStr for IdlType {
5356
}
5457
}
5558

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

104+
/// Retrieves the location and size of an ELF section data.
100105
#[cfg(feature = "parse")]
101106
#[inline(always)]
102107
fn get_section_data_offset(buffer: &[u8], offset: usize) -> (usize, usize) {

include-idl/src/shrink.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
#[cfg(feature = "shrink")]
1+
use flate2::{write::ZlibEncoder, Compression};
22
use std::{
33
fs::File,
44
io::{Read, Write},
55
path::PathBuf,
66
};
77

8-
#[cfg(feature = "shrink")]
9-
use flate2::{write::ZlibEncoder, Compression};
10-
11-
#[cfg(feature = "shrink")]
8+
/// Compresses the IDL JSON file using zlib and writes the compressed data to a file.
129
pub fn compress_idl(idl_path: &PathBuf, dest_path: &PathBuf) {
1310
let mut idl_json = File::open(idl_path).unwrap();
1411
let mut json_contents = String::new();
1512
idl_json
1613
.read_to_string(&mut json_contents)
1714
.expect("Failed to read JSON file");
1815

19-
// Compress the JSON contents using zlib
16+
// Compress the JSON contents using zlib.
2017
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
2118
encoder
2219
.write_all(json_contents.as_bytes())
2320
.expect("Failed to compress JSON data");
2421
let compressed_data = encoder.finish().expect("Failed to finish compression");
2522

26-
// Get the output directory for the build script
27-
// Write the compressed data to a file in the output directory
23+
// Get the output directory for the build script and write the compressed
24+
// data to a file in the output directory.
2825
let mut output_file = File::create(dest_path).expect("Failed to create output file");
2926
output_file
3027
.write_all(&compressed_data)

0 commit comments

Comments
 (0)