File tree Expand file tree Collapse file tree 5 files changed +37
-15
lines changed Expand file tree Collapse file tree 5 files changed +37
-15
lines changed Original file line number Diff line number Diff line change 1- use std:: path:: PathBuf ;
2-
3- // use goblin::error::Result;
4- use include_idl:: parse:: parse_idl_from_program_binary;
5-
61use 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 ) ]
Original file line number Diff line number Diff line change @@ -3,8 +3,6 @@ name = "include-idl"
33version = " 0.1.0"
44edition = " 2021"
55
6- # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7-
86[features ]
97shrink = [" flate2" ]
108parse = [" flate2" , " goblin" , " serde_json" ]
Original file line number Diff line number Diff line change 1+ //! This crate provides a way to include IDL files in a program binary.
2+
13pub mod parse;
24
35#[ cfg( feature = "shrink" ) ]
@@ -6,6 +8,29 @@ mod shrink;
68#[ cfg( feature = "shrink" ) ]
79pub 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]
1035macro_rules! include_idl {
1136 ( $type: path, $file: expr) => {
Original file line number Diff line number Diff line change @@ -13,7 +13,10 @@ pub const IDL_TYPE_SECTION: &str = ".idl.type";
1313/// Name of the section containing the IDL data.
1414pub const IDL_DATA_SECTION : & str = ".idl.data" ;
1515
16+ /// `str` representation of the Anchor IDL type.
1617const ANCHOR_IDL_TYPE : & str = "anchor" ;
18+
19+ /// `str` representation of the Codama IDL type.
1720const 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" ) ]
5761pub 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) ]
102107fn get_section_data_offset ( buffer : & [ u8 ] , offset : usize ) -> ( usize , usize ) {
Original file line number Diff line number Diff line change 1- # [ cfg ( feature = "shrink" ) ]
1+ use flate2 :: { write :: ZlibEncoder , Compression } ;
22use 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.
129pub 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)
You can’t perform that action at this time.
0 commit comments