├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── appveyor.yml ├── src └── lib.rs ├── structure-macro-impl ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md └── src │ └── lib.rs └── tests └── test.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .idea/ 3 | **/*.rs.bk 4 | Cargo.lock 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - stable 4 | - beta 5 | - nightly 6 | script: 7 | - cargo build --verbose 8 | - cargo test --verbose 9 | - cargo doc 10 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "structure" 3 | version = "0.1.2" 4 | authors = ["Liran Ringel "] 5 | description = "Use format strings to create strongly-typed data pack/unpack interfaces." 6 | license = "MIT/Apache-2.0" 7 | keywords = ["structure", "struct"] 8 | repository = "https://github.com/liranringel/structure" 9 | homepage = "https://github.com/liranringel/structure" 10 | documentation = "https://docs.rs/structure" 11 | categories = ["encoding"] 12 | readme = "README.md" 13 | include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] 14 | 15 | [badges] 16 | travis-ci = { repository = "liranringel/structure" } 17 | appveyor = { repository = "liranringel/structure" } 18 | 19 | [dependencies] 20 | proc-macro-hack = "0.4" 21 | structure-macro-impl = { version = "0.1.2", path = "structure-macro-impl" } 22 | byteorder = "1" 23 | 24 | [workspace] 25 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 The Structure Developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Structure 2 | 3 | **Use format strings to create strongly-typed data pack/unpack interfaces (inspired by Python's `struct` library).** 4 | 5 | [![Build Status](https://travis-ci.org/liranringel/structure.svg?branch=master)](https://travis-ci.org/liranringel/structure) 6 | [![Build status](https://ci.appveyor.com/api/projects/status/tiwjo6q4eete0nmh/branch/master?svg=true)](https://ci.appveyor.com/project/liran-ringel/structure/branch/master) 7 | [![Crates.io](https://img.shields.io/crates/v/structure.svg)](https://crates.io/crates/structure) 8 | 9 | [Documentation](https://docs.rs/structure) 10 | 11 | ## Installation 12 | 13 | Add this to your `Cargo.toml`: 14 | 15 | ```toml 16 | [dependencies] 17 | structure = "0.1" 18 | ``` 19 | Add this to your module: 20 | 21 | ```rust 22 | use structure::{structure, structure_impl}; 23 | ``` 24 | 25 | Or for pre-2018 versions of rust, add this to your crate root: 26 | 27 | ```rust 28 | #[macro_use] 29 | extern crate structure; 30 | ``` 31 | 32 | ## Examples 33 | 34 | ```rust 35 | // Two `u32` and one `u8` 36 | let s = structure!("2IB"); 37 | let buf: Vec = s.pack(1, 2, 3)?; 38 | assert_eq!(buf, vec![0, 0, 0, 1, 0, 0, 0, 2, 3]); 39 | assert_eq!(s.unpack(buf)?, (1, 2, 3)); 40 | ``` 41 | 42 | It's useful to use `pack_into` and `unpack_from` when using types that implement `Write` or `Read`. 43 | The following example shows how to send a `u32` and a `u8` through sockets: 44 | 45 | ```rust 46 | use std::net::{TcpListener, TcpStream}; 47 | let listener = TcpListener::bind("127.0.0.1:0")?; 48 | let mut client = TcpStream::connect(listener.local_addr()?)?; 49 | let (mut server, _) = listener.accept()?; 50 | let s = structure!("IB"); 51 | s.pack_into(&mut client, 1u32, 2u8)?; 52 | let (n, n2) = s.unpack_from(&mut server)?; 53 | assert_eq!((n, n2), (1u32, 2u8)); 54 | ``` 55 | 56 | ## License 57 | 58 | Licensed under either of 59 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 60 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 61 | at your option. 62 | 63 | ### Contribution 64 | 65 | Unless you explicitly state otherwise, any contribution intentionally submitted 66 | for inclusion in the work by you shall be dual licensed as above, without any 67 | additional terms or conditions. 68 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - TARGET: x86_64-pc-windows-msvc 4 | - TARGET: i686-pc-windows-msvc 5 | install: 6 | # Install Rust 7 | - set PATH=C:\Program Files\Git\mingw64\bin;%PATH% 8 | - curl -sSf -o rustup-init.exe https://win.rustup.rs/ 9 | - rustup-init.exe -y --default-host %TARGET% 10 | - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin 11 | - set PATH=%PATH%;C:\Users\appveyor\.rustup\toolchains\stable-%TARGET%\bin 12 | - rustc -V 13 | - cargo -V 14 | 15 | build: false 16 | 17 | test_script: 18 | - cargo build --verbose 19 | - cargo test --verbose 20 | - cargo doc 21 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Use format strings to create strongly-typed data pack/unpack interfaces (inspired by Python's `struct` library). 2 | //! 3 | //! 4 | //! # Installation 5 | //! 6 | //! Add this to your `Cargo.toml`: 7 | //! 8 | //! ```toml 9 | //! [dependencies] 10 | //! structure = "0.1" 11 | //! ``` 12 | //! 13 | //! And this to your crate root: 14 | //! 15 | //! ```rust 16 | //! #[macro_use] 17 | //! extern crate structure; 18 | //! 19 | //! # fn main() {} 20 | //! ``` 21 | //! 22 | //! # Examples 23 | //! 24 | //! ```rust 25 | //! # #[macro_use] 26 | //! # extern crate structure; 27 | //! # fn foo() -> std::io::Result<()> { 28 | //! // Two `u32` and one `u8` 29 | //! let s = structure!("2IB"); 30 | //! let buf: Vec = s.pack(1, 2, 3)?; 31 | //! assert_eq!(buf, vec![0, 0, 0, 1, 0, 0, 0, 2, 3]); 32 | //! assert_eq!(s.unpack(buf)?, (1, 2, 3)); 33 | //! # Ok(()) 34 | //! # } 35 | //! # fn main() { 36 | //! # foo().unwrap(); 37 | //! # } 38 | //! ``` 39 | //! 40 | //! It's useful to use `pack_into` and `unpack_from` when using types that implement `Write` or `Read`. 41 | //! The following example shows how to send a `u32` and a `u8` through sockets: 42 | //! 43 | //! ```rust 44 | //! # #[macro_use] 45 | //! # extern crate structure; 46 | //! # fn foo() -> std::io::Result<()> { 47 | //! use std::net::{TcpListener, TcpStream}; 48 | //! let listener = TcpListener::bind("127.0.0.1:0")?; 49 | //! let mut client = TcpStream::connect(listener.local_addr()?)?; 50 | //! let (mut server, _) = listener.accept()?; 51 | //! let s = structure!("IB"); 52 | //! s.pack_into(&mut client, 1u32, 2u8)?; 53 | //! let (n, n2) = s.unpack_from(&mut server)?; 54 | //! assert_eq!((n, n2), (1u32, 2u8)); 55 | //! # Ok(()) 56 | //! # } 57 | //! # fn main() { 58 | //! # foo().unwrap(); 59 | //! # } 60 | //! ``` 61 | //! 62 | //! # Format Strings 63 | //! 64 | //! ## Endianness 65 | //! 66 | //! By default, the endianness is big-endian. It could be determined by specifying one of the 67 | //! following characters at the beginning of the format: 68 | //! 69 | //! Character | Endianness 70 | //! --------- | ---------- 71 | //! '=' | native (target endian) 72 | //! '<' | little-endian 73 | //! '>' | big-endian 74 | //! '!' | network (= big-endian) 75 | //! 76 | //! ## Types 77 | //! 78 | //! Character | Type 79 | //! --------- | ---- 80 | //! 'b' | `i8` 81 | //! 'B' | `u8` 82 | //! '?' | `bool` 83 | //! 'h' | `i16` 84 | //! 'H' | `u16` 85 | //! 'i' | `i32` 86 | //! 'I' | `u32` 87 | //! 'q' | `i64` 88 | //! 'Q' | `u64` 89 | //! 'f' | `f32` 90 | //! 'd' | `f64` 91 | //! 's' | `&[u8]` 92 | //! 'S' | `&[u8]` 93 | //! 'P' | `*const c_void` 94 | //! 'x' | padding (1 byte) 95 | //! 96 | //! * Any format character may be preceded by an integral repeat count. For example, the format string '4h' 97 | //! means exactly the same as 'hhhh'. 98 | //! * 'P' may be follow by a ``, so `"P"` means a pointer to u32 (`*const u32`). 99 | //! * When 's' is packed, its value can be smaller than the size specified in the format, 100 | //! and the rest will be filled with zeros. For instance: 101 | //! 102 | //! ```rust 103 | //! # #[macro_use] 104 | //! # extern crate structure; 105 | //! # fn foo() -> std::io::Result<()> { 106 | //! assert_eq!(structure!("3s").pack(&[8, 9])?, vec![8, 9, 0]); 107 | //! # Ok(()) 108 | //! # } 109 | //! # fn main() { 110 | //! # foo().unwrap(); 111 | //! # } 112 | //! ``` 113 | //! 114 | //! * Unlike 's', 'S' is a fixed-size buffer, so the size of its value must be exactly the size 115 | //! specified in the format. 116 | //! * By default, 's' and 'S' are buffers of one byte. To create a fixed-sized buffer with ten bytes, 117 | //! the format would be "10S". 118 | //! * On unpack, 'x' skips a byte. On pack, 'x' always writes a null byte. To skip multiple bytes, 119 | //! prepend the length like in "10x". 120 | //! 121 | //! # Differences from Python struct library 122 | //! 123 | //! While the format strings look very similar to Python's `struct` library, there are a few differences: 124 | //! 125 | //! * Numbers' byte order is big-endian by default (e.g. u32, f64...). 126 | //! * There is no alignment support. 127 | //! * In addition to 's' (buffer) format character, that when packed, its value can be smaller than 128 | //! the size specified in the format, there is the 'S' format character, that the size of its value must 129 | //! be exactly the size specified in the format. 130 | //! * The type of a pointer is `c_void` by default, but can be changed. 131 | //! * 32 bit integer format character is only 'I'/'i' (and not 'L'/'l'). 132 | //! * structure!() macro takes a literal string as an argument. 133 | //! * It's called `structure` because `struct` is a reserved keyword in Rust. 134 | 135 | #[macro_use] 136 | extern crate proc_macro_hack; 137 | 138 | #[doc(hidden)] 139 | pub extern crate byteorder; 140 | 141 | 142 | // Allow the "unused" #[macro_use] because there is a different un-ignorable 143 | // warning otherwise: 144 | // 145 | // proc macro crates and `#[no_link]` crates have no effect without `#[macro_use]` 146 | #[allow(unused_imports)] 147 | #[macro_use] 148 | extern crate structure_macro_impl; 149 | #[doc(hidden)] 150 | pub use structure_macro_impl::*; 151 | 152 | proc_macro_expr_decl! { 153 | structure! => structure_impl 154 | } 155 | -------------------------------------------------------------------------------- /structure-macro-impl/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "structure-macro-impl" 3 | version = "0.1.2" # remember to update the dependency in the root crate 4 | authors = ["Liran Ringel "] 5 | description = "Procedural macro crate for the structure crate." 6 | license = "MIT/Apache-2.0" 7 | readme = "README.md" 8 | include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] 9 | 10 | [lib] 11 | proc-macro = true 12 | 13 | [dependencies] 14 | proc-macro-hack = "0.4" 15 | quote = "0.3" 16 | -------------------------------------------------------------------------------- /structure-macro-impl/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../LICENSE-APACHE -------------------------------------------------------------------------------- /structure-macro-impl/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../LICENSE-MIT -------------------------------------------------------------------------------- /structure-macro-impl/README.md: -------------------------------------------------------------------------------- 1 | ../README.md -------------------------------------------------------------------------------- /structure-macro-impl/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![recursion_limit = "128"] 2 | 3 | #[macro_use] 4 | extern crate proc_macro_hack; 5 | #[macro_use] 6 | extern crate quote; 7 | 8 | use std::mem; 9 | use std::os::raw::c_void; 10 | use std::string::String; 11 | use quote::{Tokens, Ident}; 12 | 13 | proc_macro_expr_impl! { 14 | pub fn structure_impl(input: &str) -> String { 15 | let format = trim_quotes(input); 16 | let struct_name = Ident::from(format_to_struct_name(format)); 17 | let (values, endianness) = format_to_values(&format); 18 | let (args, fn_decl_args, args_types) = build_args_list(&values); 19 | let endianness = match endianness { 20 | Endianness::Native => { 21 | if cfg!(target_endian = "little") { 22 | quote!(LittleEndian) 23 | } else { 24 | quote!(BigEndian) 25 | } 26 | } 27 | Endianness::LittleEndian => quote!(LittleEndian), 28 | Endianness::BigEndian => quote!(BigEndian), 29 | }; 30 | let size = calc_size(&values); 31 | let pack_fn = build_pack_fn(&args, &fn_decl_args, size); 32 | let pack_into_fn = build_pack_into_fn(&values, &fn_decl_args, &endianness); 33 | let unpack_fn = build_unpack_fn(&args_types, size); 34 | let unpack_from_fn = build_unpack_from_fn(&values, &args, &args_types, &endianness); 35 | let size_fn = build_size_fn(size); 36 | let output = quote! {{ 37 | #[derive(Debug)] 38 | #[allow(non_camel_case_types)] 39 | struct #struct_name; 40 | #[allow(unused_imports)] 41 | use std::io::{Result, Write, Read, Error, ErrorKind, Cursor}; 42 | #[allow(unused_imports)] 43 | use std::os::raw::c_void; 44 | #[allow(unused_imports)] 45 | use structure::byteorder::{WriteBytesExt, ReadBytesExt, BigEndian, LittleEndian}; 46 | 47 | #[allow(unused)] static TRUE_BUF: &[u8] = &[1]; 48 | #[allow(unused)] static FALSE_BUF: &[u8] = &[0]; 49 | 50 | impl #struct_name { 51 | #pack_fn 52 | #pack_into_fn 53 | #unpack_fn 54 | #unpack_from_fn 55 | #size_fn 56 | } 57 | 58 | #struct_name // Create structure instance 59 | }}; 60 | 61 | output.into_string() 62 | } 63 | } 64 | 65 | #[derive(PartialEq)] 66 | enum Endianness { 67 | Native, 68 | LittleEndian, 69 | BigEndian, 70 | } 71 | 72 | fn build_pack_fn(args: &Tokens, fn_decl_args: &Tokens, size: usize) -> Tokens { 73 | quote! { 74 | #[allow(unused)] 75 | fn pack(&self, #fn_decl_args) -> Result> { 76 | let mut wtr = Vec::with_capacity(#size); 77 | self.pack_into(&mut wtr, #args)?; 78 | Ok(wtr) 79 | } 80 | } 81 | } 82 | 83 | fn build_pack_into_fn(values: &[StructValue], fn_decl_args: &Tokens, endianness: &Tokens) -> Tokens { 84 | // Pack each argument 85 | let mut writings = Tokens::new(); 86 | let mut arg_index = 0; 87 | for value in values { 88 | let writing = match *value.kind() { 89 | ValueKind::Number | ValueKind::Boolean | ValueKind::Pointer => { 90 | let mut tokens = Tokens::new(); 91 | for _ in 0..value.repeat() { 92 | arg_index += 1; 93 | let current_arg = Ident::from(format!("_{}", arg_index)); 94 | if *value.kind() == ValueKind::Number { 95 | let byteorder_fn = Ident::from(format!("write_{}", value.type_name())); 96 | match value.type_name().as_str() { 97 | "u8" | "i8" => { 98 | tokens.append(quote! {wtr.#byteorder_fn(#current_arg)?;}); 99 | } 100 | _ => { 101 | tokens.append(quote! {wtr.#byteorder_fn::<#endianness>(#current_arg)?;}); 102 | } 103 | } 104 | } else if *value.kind() == ValueKind::Boolean { 105 | tokens.append(quote! { 106 | let buf = if #current_arg { TRUE_BUF } else { FALSE_BUF }; 107 | wtr.write(buf)?; 108 | }); 109 | } else { 110 | let size = mem::size_of::(); 111 | let integer_type = Ident::from(format!("u{}", size * 8)); 112 | let byteorder_fn = Ident::from(format!("write_u{}", size * 8)); 113 | tokens.append(quote! { 114 | let v = #current_arg as #integer_type; 115 | wtr.#byteorder_fn::<#endianness>(v)?; 116 | }); 117 | } 118 | } 119 | tokens 120 | } 121 | ValueKind::Buffer | ValueKind::FixedBuffer => { 122 | arg_index += 1; 123 | let current_arg = Ident::from(format!("_{}", arg_index)); 124 | let buffer_length = value.repeat(); 125 | let length_check = if *value.kind() == ValueKind::Buffer { 126 | // If the type is `ValueKind::Buffer`, and the given buffer is smaller than the 127 | // size determined in the format, the rest will be filled with zeros. 128 | quote! { #current_arg.len() <= #buffer_length } 129 | } else { 130 | quote! { #current_arg.len() == #buffer_length } 131 | }; 132 | let mut tokens = quote! { 133 | if !(#length_check) { 134 | let msg = format!("Buffer length does not match the format \ 135 | (buffer size in format: {}, actual size: {}", #current_arg.len(), #buffer_length); 136 | return Err(Error::new(ErrorKind::InvalidInput, msg)); 137 | } 138 | wtr.write_all(#current_arg)?; 139 | }; 140 | if *value.kind() == ValueKind::Buffer { 141 | tokens.append(quote! { 142 | if #current_arg.len() != #buffer_length { 143 | wtr.write_all(&vec![0; (#buffer_length - #current_arg.len())])?; 144 | } 145 | }); 146 | } 147 | tokens 148 | } 149 | ValueKind::Padding => { 150 | let number = value.repeat(); 151 | quote! { 152 | wtr.write_all(&[0; #number])?; 153 | } 154 | } 155 | }; 156 | writings.append(writing); 157 | } 158 | 159 | quote! { 160 | #[allow(unused)] 161 | fn pack_into(&self, wtr: &mut T, #fn_decl_args) -> Result<()> { 162 | #writings 163 | Ok(()) 164 | } 165 | } 166 | } 167 | 168 | fn build_unpack_fn(args_types: &Tokens, size: usize) -> Tokens { 169 | quote! { 170 | #[allow(unused)] 171 | fn unpack>(&self, buf: T) -> Result<(#args_types,)> { 172 | if buf.as_ref().len() != #size { 173 | let msg = format!("Buffer length does not match the format \ 174 | (format size: {}, actual size: {}", #size, buf.as_ref().len()); 175 | return Err(Error::new(ErrorKind::InvalidInput, msg)) 176 | } 177 | let mut rdr = Cursor::new(buf); 178 | self.unpack_from(&mut rdr) 179 | } 180 | } 181 | } 182 | 183 | fn build_unpack_from_fn(values: &[StructValue], args: &Tokens, args_types: &Tokens, endianness: &Tokens) -> Tokens { 184 | let mut readings = Tokens::new(); 185 | let mut arg_index = 0; 186 | for value in values { 187 | let reading = match *value.kind() { 188 | ValueKind::Number | ValueKind::Boolean | ValueKind::Pointer => { 189 | let mut tokens = Tokens::new(); 190 | for _ in 0..value.repeat() { 191 | arg_index += 1; 192 | let current_arg = Ident::from(format!("_{}", arg_index)); 193 | if *value.kind() == ValueKind::Number { 194 | let byteorder_fn = Ident::from(format!("read_{}", value.type_name())); 195 | match value.type_name().as_str() { 196 | "u8" | "i8" => { 197 | tokens.append(quote! { let #current_arg = rdr.#byteorder_fn()?;}); 198 | } 199 | _ => { 200 | tokens.append(quote! { let #current_arg = rdr.#byteorder_fn::<#endianness>()?;}); 201 | } 202 | } 203 | } else if *value.kind() == ValueKind::Boolean { 204 | tokens.append(quote! { 205 | let #current_arg = rdr.read_u8()?; 206 | let #current_arg = #current_arg != 0; // 0 is false 207 | }); 208 | } else { 209 | let pointer_type = Ident::from(value.type_name().as_str()); 210 | let size = mem::size_of::(); 211 | let byteorder_fn = Ident::from(format!("read_u{}", size * 8)); 212 | tokens.append(quote! { 213 | let #current_arg = { 214 | let v = rdr.#byteorder_fn::<#endianness>()?; 215 | v as #pointer_type 216 | }; 217 | }); 218 | } 219 | } 220 | tokens 221 | } 222 | ValueKind::Buffer | ValueKind::FixedBuffer => { 223 | arg_index += 1; 224 | let current_arg = Ident::from(format!("_{}", arg_index)); 225 | let buffer_length = value.repeat(); 226 | quote! { 227 | let mut #current_arg = vec![0; #buffer_length]; 228 | rdr.read_exact(&mut #current_arg)?; 229 | } 230 | } 231 | ValueKind::Padding => { 232 | let number = value.repeat(); 233 | quote! { 234 | rdr.read_exact(&mut [0; #number])?; 235 | } 236 | } 237 | }; 238 | readings.append(reading); 239 | } 240 | 241 | quote! { 242 | #[allow(unused)] 243 | fn unpack_from(&self, rdr: &mut T) -> Result<(#args_types,)> { 244 | #readings 245 | Ok((#args,)) 246 | } 247 | } 248 | } 249 | 250 | /// Build the args list, the function declaration args list and the type list 251 | fn build_args_list(values: &[StructValue]) -> (Tokens, Tokens, Tokens) { 252 | let mut args = vec![]; 253 | let mut fn_decl_args = vec![]; 254 | let mut args_types = vec![]; 255 | let mut arg_index = 0; 256 | for v in values { 257 | match *v.kind() { 258 | ValueKind::Padding => continue, 259 | ValueKind::Buffer | ValueKind::FixedBuffer => { 260 | arg_index += 1; 261 | args.push(Ident::from(format!("_{}", arg_index))); 262 | fn_decl_args.push(Ident::from(format!("_{}: {}", arg_index, v.type_name()))); 263 | args_types.push(Ident::from("Vec".to_owned())); 264 | } 265 | _ => { 266 | for _ in 0..v.repeat() { 267 | arg_index += 1; 268 | args.push(Ident::from(format!("_{}", arg_index))); 269 | fn_decl_args.push(Ident::from(format!("_{}: {}", arg_index, v.type_name()))); 270 | args_types.push(Ident::from(v.type_name().as_str())); 271 | } 272 | } 273 | } 274 | } 275 | (quote!(#(#args),*), quote!(#(#fn_decl_args),*), quote!(#(#args_types),*)) 276 | } 277 | 278 | fn build_size_fn(size: usize) -> Tokens { 279 | quote! { 280 | #[allow(unused)] 281 | fn size(&self) -> usize { 282 | #size 283 | } 284 | } 285 | } 286 | 287 | fn calc_size(values: &[StructValue]) -> usize { 288 | let mut size = 0; 289 | for v in values { 290 | if v.type_name().starts_with("*") { 291 | mem::size_of::<*const c_void>(); 292 | } 293 | let type_size = match v.type_name().as_str() { 294 | "i8" => mem::size_of::(), 295 | "&[u8]" | "u8" => mem::size_of::(), 296 | "bool" => 1, 297 | "i16" => mem::size_of::(), 298 | "u16" => mem::size_of::(), 299 | "i32" => mem::size_of::(), 300 | "u32" => mem::size_of::(), 301 | "i64" => mem::size_of::(), 302 | "u64" => mem::size_of::(), 303 | "f32" => mem::size_of::(), 304 | "f64" => mem::size_of::(), 305 | t if t.starts_with("*") => mem::size_of::(), 306 | _ => panic!("Unknown type: '{}'", v.type_name()), 307 | }; 308 | size += type_size * v.repeat(); 309 | } 310 | size 311 | } 312 | 313 | fn format_to_struct_name(format: &str) -> String { 314 | format!("Struct_{}", format.replace("?", "Bool") 315 | .replace("=", "Native") 316 | .replace("<", "LittleEndian") 317 | .replace(">", "") 318 | .replace("!", "")) 319 | } 320 | 321 | /// Return the format string without the endianness, and the endianness 322 | fn format_endianness(format: &str) -> (&str, Endianness) { 323 | let first_char = format.chars().nth(0); 324 | let endianness = match first_char { 325 | Some('=') => Endianness::Native, 326 | Some('<') => Endianness::LittleEndian, 327 | _ => Endianness::BigEndian, 328 | }; 329 | let mut chars = format.chars(); 330 | match chars.next() { 331 | Some('=') | Some('<') | Some('>') | Some('!') => (chars.as_str(), endianness), 332 | _ => (format, endianness), 333 | } 334 | } 335 | 336 | fn char_to_type(c: char) -> (&'static str, ValueKind) { 337 | match c { 338 | 'b' => ("i8", ValueKind::Number), 339 | 'B' => ("u8", ValueKind::Number), 340 | '?' => ("bool", ValueKind::Boolean), 341 | 'h' => ("i16", ValueKind::Number), 342 | 'H' => ("u16", ValueKind::Number), 343 | 'i' => ("i32", ValueKind::Number), 344 | 'I' => ("u32", ValueKind::Number), 345 | 'q' => ("i64", ValueKind::Number), 346 | 'Q' => ("u64", ValueKind::Number), 347 | 'f' => ("f32", ValueKind::Number), 348 | 'd' => ("f64", ValueKind::Number), 349 | 's' => ("&[u8]", ValueKind::Buffer), 350 | 'S' => ("&[u8]", ValueKind::FixedBuffer), 351 | 'P' => ("*const c_void", ValueKind::Pointer), 352 | 'x' => ("u8", ValueKind::Padding), 353 | _ => panic!("Unknown format: '{}'", c), 354 | } 355 | } 356 | 357 | fn format_to_values(format: &str) -> (Vec, Endianness) { 358 | let (format, endianness) = format_endianness(format); 359 | let mut values = vec![]; 360 | let mut chars = format.chars().peekable(); 361 | let mut repeat_str = String::new(); 362 | while let Some(c) = chars.next() { 363 | if c.is_digit(10) { 364 | repeat_str.push(c); 365 | } else { 366 | let (type_name, kind) = char_to_type(c); 367 | let mut type_name = type_name.to_owned(); 368 | if kind == ValueKind::Pointer { 369 | // Parse pointer type 370 | if endianness != Endianness::Native { 371 | panic!("Pointer can be used only if the endianness is native. \ 372 | To change the endianness to native, start the format with '='"); 373 | } 374 | if let Some(&'<') = chars.peek() { 375 | chars.next(); 376 | let mut pointer_type_name = String::new(); 377 | loop { 378 | let c = chars.next(); 379 | if c == None { 380 | panic!("Pointer type must end with '>'"); 381 | } else if c == Some('>') { 382 | if pointer_type_name.is_empty() { 383 | panic!("Pointer type cannot be empty"); 384 | } 385 | type_name = format!("*const {}", pointer_type_name); 386 | break; 387 | } else { 388 | pointer_type_name.push(c.unwrap()); 389 | } 390 | } 391 | } 392 | } 393 | let mut repeat = 1; 394 | if !repeat_str.is_empty() { 395 | repeat = repeat_str.parse().expect("not a number"); 396 | repeat_str.clear(); 397 | } 398 | values.push(StructValue::new(type_name, repeat, kind)); 399 | } 400 | } 401 | if !repeat_str.is_empty() { 402 | panic!("No format character is followed by the number {}", repeat_str); 403 | } 404 | (values, endianness) 405 | } 406 | 407 | #[derive(PartialEq)] 408 | enum ValueKind { 409 | Number, 410 | Boolean, 411 | Buffer, 412 | FixedBuffer, 413 | Pointer, 414 | Padding, 415 | } 416 | 417 | struct StructValue { 418 | type_name: String, 419 | repeat: usize, 420 | kind: ValueKind 421 | } 422 | 423 | impl StructValue { 424 | fn new(type_name: String, repeat: usize, kind: ValueKind) -> StructValue { 425 | StructValue { type_name: type_name, repeat: repeat, kind: kind } 426 | } 427 | fn type_name(&self) -> &String { 428 | &self.type_name 429 | } 430 | fn repeat(&self) -> usize { 431 | self.repeat 432 | } 433 | fn kind(&self) -> &ValueKind { 434 | &self.kind 435 | } 436 | } 437 | 438 | fn trim_quotes(input: &str) -> &str { 439 | 440 | if input.chars().nth(0) != Some('"') && input.chars().last() != Some('"') || input.len() < 2 { 441 | panic!("structure!() macro takes a literal string as an argument"); 442 | } 443 | &input[1..(input.len()-1)] 444 | } 445 | -------------------------------------------------------------------------------- /tests/test.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate structure; 3 | 4 | use std::os::raw::c_void; 5 | use std::mem::transmute; 6 | use std::io::ErrorKind; 7 | use std::io::Cursor; 8 | 9 | 10 | #[test] 11 | fn pack() { 12 | assert_eq!(structure!("I").pack(3).unwrap(), vec![0, 0, 0, 3]); 13 | } 14 | 15 | #[test] 16 | fn pack_into() { 17 | let mut v = Vec::new(); 18 | structure!("I").pack_into(&mut v, 3).unwrap(); 19 | assert_eq!(v, vec![0, 0, 0, 3]); 20 | } 21 | 22 | #[test] 23 | fn unpack() { 24 | assert_eq!(structure!("I").unpack(&[0, 0, 0, 3]).unwrap(), (3, )); 25 | } 26 | 27 | #[test] 28 | fn unpack_from() { 29 | assert_eq!(structure!("I").unpack_from(&mut Cursor::new(&[0, 0, 0, 3])).unwrap(), (3, )); 30 | } 31 | 32 | #[test] 33 | fn pack_2_values() { 34 | assert_eq!(structure!("If").pack(6, 5.2).unwrap(), vec![0, 0, 0, 6, 64, 166, 102, 102]); 35 | } 36 | 37 | #[test] 38 | fn unpack_2_values() { 39 | assert_eq!(structure!("If").unpack(&[0, 0, 0, 6, 64, 166, 102, 102]).unwrap(), (6, 5.2)); 40 | } 41 | 42 | #[test] 43 | fn pack_bool() { 44 | assert_eq!(structure!("?").pack(true).unwrap(), vec![1]); 45 | assert_eq!(structure!("?").pack(false).unwrap(), vec![0]); 46 | } 47 | 48 | #[test] 49 | fn unpack_bool() { 50 | assert_eq!(structure!("?").unpack(&[1]).unwrap(), (true, )); 51 | assert_eq!(structure!("?").unpack(&[0]).unwrap(), (false, )); 52 | } 53 | 54 | #[test] 55 | fn pack_big_endian() { 56 | assert_eq!(structure!("I").pack(1).unwrap(), vec![0, 0, 0, 1]); 57 | assert_eq!(structure!(">I").pack(1).unwrap(), vec![0, 0, 0, 1]); 58 | assert_eq!(structure!("!I").pack(1).unwrap(), vec![0, 0, 0, 1]); 59 | } 60 | 61 | #[test] 62 | fn unpack_big_endian() { 63 | assert_eq!(structure!("I").unpack(&[0, 0, 0, 1]).unwrap(), (1, )); 64 | assert_eq!(structure!(">I").unpack(&[0, 0, 0, 1]).unwrap(), (1, )); 65 | assert_eq!(structure!("!I").unpack(&[0, 0, 0, 1]).unwrap(), (1, )); 66 | } 67 | 68 | #[test] 69 | fn pack_little_endian() { 70 | assert_eq!(structure!("(&num) }).unwrap(), unsafe { transmute::<&u32, [u8; 8]>(&num) }); 151 | } 152 | #[cfg(target_pointer_width = "32")] 153 | { 154 | assert_eq!(structure!("=P").pack(unsafe { transmute::<&u32, *const c_void>(&num) }).unwrap(), unsafe { transmute::<&u32, [u8; 4]>(&num) }); 155 | } 156 | let packed_pointers = structure!("=PP").pack(unsafe { transmute::<&u32, *const c_void>(&num) }, unsafe { transmute::<&u32, *const c_void>(&num2) }).unwrap(); 157 | let (p, p2) = structure!("=PP").unpack(packed_pointers).unwrap(); 158 | assert_eq!(p, unsafe { transmute::<&u32, *const c_void>(&num) }); 159 | assert_eq!(p2, unsafe { transmute::<&u32, *const c_void>(&num2) }); 160 | } 161 | 162 | #[test] 163 | fn pack_and_unpack_typed_pointer() { 164 | let num: u32 = 6; 165 | let num2: u8 = 7; 166 | let num3: u8 = 8; 167 | let s = structure!("=P2P"); 168 | let packed_pointers = s.pack(&num as *const u32, &num2 as *const u8, &num3 as *const u8).unwrap(); 169 | let (p, p2, p3) = s.unpack(packed_pointers).unwrap(); 170 | assert_eq!(p, &num as *const u32); 171 | assert_eq!(p2, &num2 as *const u8); 172 | assert_eq!(p3, &num3 as *const u8); 173 | } 174 | 175 | #[test] 176 | fn pack_and_unpack_padding() { 177 | let s = structure!("b3xb"); 178 | let packed = s.pack(-1, -1).unwrap(); 179 | assert_eq!(packed, &[255, 0, 0, 0, 255]); 180 | let (b1, b2) = s.unpack(packed).unwrap(); 181 | assert_eq!((b1, b2), (-1, -1)); 182 | } 183 | --------------------------------------------------------------------------------