1 //! `build_const`: crate for creating constants in your build script 2 //! 3 //! The build_const crate exists to help create rust constant files at compile time or in a 4 //! generating script. It is ultra simple and lightweight, making constant creation a simple 5 //! matter. 6 //! 7 //! Recommended use: when developing make your constants in `build.rs`. Once your constants are 8 //! fairly stable create a script instead and have your constants file be generated in either a 9 //! single file or an external crate that you can bring in as a dependency. 10 //! 11 //! # Example 12 //! 13 //! Include `build_const = VERSION` in your `Cargo.toml` file. For `no_std` support (macros only) 14 //! use `default-features = false`. 15 //! 16 //! See `ConstWriter` for how to use in a build.rs or script. To then import a "constants.rs" file 17 //! created in `build.rs` use: 18 //! 19 //! ```c 20 //! #[macro_use] 21 //! extern crate build_const; 22 //! 23 //! build_const!("constants"); 24 //! println!("VALUE: {}", VALUE); 25 //! println!("VALUE: {}", ARRAY); 26 //! ``` 27 //! 28 //! For writing constants in a script, the macro `src_file!` is also provided. 29 //! ```c 30 //! // will write files to `/src/constants.rs` 31 //! let mut consts = ConstWriter::from_path(&Path::from(src_file!("constants.rs"))).unwrap(); 32 //! // ... use consts 33 //! ``` 34 35 #![cfg_attr(not(feature = "std"), no_std)] 36 37 #[cfg(feature = "std")] 38 mod writer; 39 40 #[cfg(feature = "std")] 41 pub use writer::{ 42 ConstWriter, 43 ConstValueWriter, 44 write_array, 45 write_array_raw, 46 }; 47 48 /// Shortcut macro which expands to the same module path used in 49 /// `ConstWriter::for_build(mod_name)`. 50 /// 51 /// If you don't want to include macros, this is simply a one liner: 52 /// ```ignore 53 /// include!(concat!(env!("OUT_DIR"), concat!("/", $mod_name))); 54 /// ``` 55 #[macro_export] 56 macro_rules! build_const { 57 ( $mod_name:expr ) => { 58 include!( 59 concat!( 60 env!("OUT_DIR"), 61 concat!("/", concat!($mod_name, ".rs")) 62 ) 63 ); 64 }; 65 } 66 67 /// Macro which returns the path to file in your `src/` directory. 68 /// 69 /// Example: 70 /// ```ignore 71 /// src_file!("constants.rs"); 72 /// ``` 73 /// returns `/path/to/project/src/constants.rs` 74 /// 75 /// If you need a more custom path, the basic implementation is: 76 /// ```ignore 77 /// concat!(env!("CARGO_MANIFEST_DIR"), "/src/path/to/file") 78 /// ``` 79 #[macro_export] 80 macro_rules! src_file { 81 ( $file_name:expr) => { 82 concat!( 83 env!("CARGO_MANIFEST_DIR"), 84 concat!("/", concat!("src", concat!("/", $file_name))) 85 ) 86 }; 87 } 88 89