1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
//! include-lines
//!
//! Rust macros for reading in all lines from a file at compile time. This can be very useful for loading static data.
//!
//! # Examples
//!
//! For the examples, there is a file `file.txt` in the same directory as the project's Cargo.toml file:
//!
//! ```text
//! these
//! are
//! file
//! lines
//! ```
//!
//! ## Read in a file and store it an an array of type `[&'static str]`
//!
//! ```ignore
//! use include_lines::include_lines;
//! let lines = include_lines!("file.txt");
//! ```
//!
//! For the example file, this expands to:
//!
//! ```ignore
//! let lines = [
//! "these",
//! "are",
//! "file",
//! "lines",
//! ];
//! ```
//!
//! ## Read in a file and store it an an array of type `[String]`
//!
//! ```ignore
//! use include_lines::include_lines_s;
//! let lines = include_lines_s!("file.txt");
//! ```
//!
//! For the example file, this expands to:
//!
//! ```ignore
//! let lines = [
//! String::from("these"),
//! String::from("are"),
//! String::from("file"),
//! String::from("lines"),
//! ];
//! ```
//!
//! ## Get the number of lines in a file at compile time as type `usize`
//!
//! ```ignore
//! use include_lines::count_lines;
//! let num_lines = count_lines!("file.txt");
//! ```
//!
//! For the example file, this expands to:
//!
//! ```ignore
//! let num_lines = 4usize;
//! ```
//!
//! ## Create a static array from a file at compile time
//!
//! You can use the `static_include_lines!` and `static_include_lines_s!` macros to initialize static text arrays at compile time:
//!
//! ```ignore
//! use include_lines::{static_include_lines};
//! static_include_lines!(LINES, "file.txt");
//! ```
//!
//! For the example file, this expands to:
//!
//! ```ignore
//! static LINES: [&str; 4] = [
//! "these",
//! "are",
//! "file",
//! "lines",
//! ];
//! ```
pub use include_lines_proc::*;
/// Include lines from a file and save them to a static variable with the given name.
/// Each line is saved as type `&str`.
///
/// Example:
/// ```ignore
/// static_include_lines!(MY_LINES, "file.txt");
/// ```
///
/// Expands to:
/// ```ignore
/// static MY_LINES: [&str; include_lines::count_lines!("file.txt")] = include_lines::include_lines!("file.txt");
/// ```
#[allow(unused_macros)]
#[macro_export]
macro_rules! static_include_lines {
($var: ident, $filename: literal) => {
static $var: [&str; include_lines::count_lines!($filename)] =
include_lines::include_lines!($filename);
};
}
/// This macro functions identically to [static_include_lines], but each line is returned as type `String`.
#[allow(unused_macros)]
#[macro_export]
macro_rules! static_include_lines_s {
($var: ident, $filename: literal) => {
static $var: [String; include_lines::count_lines!($filename)] =
include_lines::include_lines_s!($filename);
};
}