Skip to content

Commit

Permalink
fix(lib): Add macros for loading paths from relative path or from car…
Browse files Browse the repository at this point in the history
…go manifest directory

fix(examples): Both examples now can load examples from manifest directory
  • Loading branch information
448-OG committed Nov 16, 2023
1 parent 48946cd commit 8ff0031
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "puppeteer"
authors = ["448 Engineering Developers <superuser@448.africa>"]
version = "2.2.1"
version = "2.2.2"
edition = "2021"
description = "A Minimal Dependency Easy to Use GUI Builder in Rust using Async Channels"
categories = ["graphics", "gui"]
Expand Down
6 changes: 3 additions & 3 deletions Lib/examples/hello-puppeteer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ fn main() {

tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

const FONTS: [StaticAsset; 2] = puppeteer::load_assets!(
("centauri", "assets/fonts/centauri.woff2"),
("warteg", "assets/fonts/warteg.woff2"),
const FONTS: [StaticAsset; 2] = puppeteer::assets_from_manifest_dir!(
("centauri", "examples/assets/fonts/centauri.woff2"),
("warteg", "examples/assets/fonts/warteg.woff2"),
);

smol::block_on(async {
Expand Down
20 changes: 19 additions & 1 deletion Lib/examples/load_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
let counter = puppeteer::items_counter!(1, 2, 3);
assert_eq!(counter, 3usize);

let assets = puppeteer::load_assets!(
let assets = puppeteer::assets!(
("frow.min", "assets/frow.min.css"),
("centauri", "assets/fonts/centauri.woff2"),
("warteg", "assets/fonts/warteg.woff2"),
Expand All @@ -58,5 +58,23 @@ fn main() {
"1ee55e0400b17f43438b4ca12a94fa83984323095fa6093e9e97ca6b13d906e6",
blake3::hash(assets[2].bytes).to_hex().as_str()
);

let assets = puppeteer::assets_from_manifest_dir!(
("frow.min", "examples/assets/frow.min.css"),
("centauri", "examples/assets/fonts/centauri.woff2"),
("warteg", "examples/assets/fonts/warteg.woff2"),
);
assert_eq!(
"0b3ae879a79a09c1aa75f82b8f4a2482f08842b511b4b075484996e29cd7c3b0",
blake3::hash(assets[0].bytes).to_hex().as_str()
);
assert_eq!(
"b67a70f6e41e9dc758a1ab6b24d30865df90446fb155d6fb905c5789b3f43ce3",
blake3::hash(assets[1].bytes).to_hex().as_str()
);
assert_eq!(
"1ee55e0400b17f43438b4ca12a94fa83984323095fa6093e9e97ca6b13d906e6",
blake3::hash(assets[2].bytes).to_hex().as_str()
);
})
}
29 changes: 27 additions & 2 deletions Lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ macro_rules! asset {
}};
}

/// Include asset bytes at compile time
/// Include asset bytes at compile time from an absolute path
#[macro_export]
macro_rules! load_assets {
macro_rules! assets {
($(($name:expr, $path:expr)),* $(,)?) => {{
const ITEMS_LEN: usize = [$(stringify!($name),)*].len();

Expand All @@ -44,3 +44,28 @@ macro_rules! load_assets {
outcome
}};
}

/// Include asset bytes at compile time but load the path from the manifest directory
#[macro_export]
macro_rules! assets_from_manifest_dir {
($(($name:expr, $path:expr)),* $(,)?) => {{
const ITEMS_LEN: usize = [$(stringify!($name),)*].len();

use puppeteer::StaticAsset;

let mut outcome = [StaticAsset{name: "", bytes: &[0u8]}; ITEMS_LEN];
let mut count = 0usize;

$({
const BYTES: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $path));
const ASSET: StaticAsset = StaticAsset{
name: $name,
bytes: BYTES
};
outcome[count] = ASSET;
count += 1;
})*;

outcome
}};
}

0 comments on commit 8ff0031

Please sign in to comment.