bracket_embedding/
lib.rs

1//! The `bracket-embedding` crate is used to provide resource embedding.
2//! This allows you to include binary assets inside your program when shipping,
3//! with no external files. This can be especially useful for WASM builds.
4//! 
5//! For example:
6//! 
7//! ```rust
8//! use bracket_embedding::prelude::*;
9//! 
10//! embedded_resource!(SOURCE_FILE, "embedding.rs");
11//! 
12//! fn main() {
13//!    // This helper macro links the above embedding, allowing it to be accessed as a resource from various parts of the program.
14//!    link_resource!(SOURCE_FILE, "embedding.rs");
15//! }
16//! ```
17//! 
18//! This crate isn't very useful on its own, but is heavily used by the other parts of `bracket-lib`.
19
20#![warn(clippy::all, clippy::pedantic, clippy::cargo)]
21#![allow(clippy::needless_doctest_main)]
22mod embedding;
23
24pub mod prelude {
25    pub use crate::embedding::*;
26    pub use crate::{embedded_resource, link_resource};
27}
28
29/// Declare an embedded resource.
30/// 
31/// # Arguments
32/// 
33/// * `resource_name` - a constant that will represent the resource.
34/// * `filename` - the path to the file to embed.
35/// 
36/// Once embedded, you need to use `link_resource` to make it available.
37/// 
38/// # Example
39/// 
40/// ```rust
41/// use bracket_embedding::prelude::*;
42/// 
43/// embedded_resource!(SOURCE_FILE, "embedding.rs");
44/// 
45/// fn main() {
46///    // This helper macro links the above embedding, allowing it to be accessed as a resource from various parts of the program.
47///    link_resource!(SOURCE_FILE, "embedding.rs");
48/// }
49/// ```
50#[macro_export]
51macro_rules! embedded_resource {
52    ($resource_name : ident, $filename : expr) => {
53        const $resource_name: &'static [u8] = include_bytes!($filename);
54    };
55}
56
57/// Link an embedded resource, making it available to `bracket-lib` via the resources
58/// system.
59/// 
60/// # Arguments
61/// 
62/// * `resource_name` - a constant that will represent the resource.
63/// * `filename` - the path to the file to embed.
64/// 
65/// The resource must be previously declared with `embedded_resource!`.
66/// 
67/// # Example
68/// 
69/// ```rust
70/// use bracket_embedding::prelude::*;
71/// 
72/// embedded_resource!(SOURCE_FILE, "embedding.rs");
73/// 
74/// fn main() {
75///    // This helper macro links the above embedding, allowing it to be accessed as a resource from various parts of the program.
76///    link_resource!(SOURCE_FILE, "embedding.rs");
77/// }
78/// ```
79#[macro_export]
80macro_rules! link_resource {
81    ($resource_name : ident, $filename : expr) => {
82        EMBED
83            .lock()
84            .add_resource($filename.to_string(), $resource_name);
85    };
86}