#![doc = include_str!("../README.md")]
#![warn(clippy::all, missing_docs, nonstandard_style, future_incompatible)]
#[macro_export]
macro_rules! embed_string {
($path:literal) => {{
#[cfg(debug_assertions)]
{
use std::{borrow::Cow, fs, path::Path};
let this_file = Path::new(file!());
let this_dir = this_file
.parent()
.expect(&format!("embed_file: no parent for `{:?}`", this_file));
let path = this_dir.join($path);
let data = match fs::read_to_string(&path) {
Ok(s) => s,
Err(e) => panic!("embed_file: can't read `{:?}` because of {:?}", path, e),
};
let s: Cow<'static, str> = Cow::Owned(data);
s
}
#[cfg(not(debug_assertions))]
{
use std::borrow::Cow;
let s: Cow<'static, str> = Cow::Borrowed(include_str!($path));
s
}
}};
}
#[macro_export]
macro_rules! embed_bytes {
($path:literal) => {{
#[cfg(debug_assertions)]
{
use std::{borrow::Cow, fs, path::Path};
let this_file = Path::new(file!());
let this_dir = this_file
.parent()
.expect(&format!("embed_file: no parent for `{:?}`", this_file));
let path = this_dir.join($path);
let data = match fs::read(&path) {
Ok(bytes) => bytes,
Err(e) => panic!("embed_file: can't read `{:?}` because of {:?}", path, e),
};
let bytes: Cow<'static, [u8]> = Cow::Owned(data);
bytes
}
#[cfg(not(debug_assertions))]
{
use std::borrow::Cow;
let bytes: Cow<'static, [u8]> = Cow::Borrowed(include_bytes!($path));
bytes
}
}};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_embed_string() {
assert!(embed_string!("lib.rs").contains("# embed-file"));
}
#[test]
fn test_embed_bytes() {
assert!(String::from_utf8(embed_bytes!("lib.rs").to_vec())
.unwrap()
.contains("# embed-file"));
}
}