mirror of
https://github.com/RGBCube/embd-rs
synced 2025-08-02 08:27:45 +00:00
Compare commits
4 commits
d61798c7e0
...
3d45164562
Author | SHA1 | Date | |
---|---|---|---|
3d45164562 | |||
9ec0768a8e | |||
601d4fed5f | |||
a4724700b2 |
10 changed files with 79 additions and 230 deletions
|
@ -1,4 +1,5 @@
|
|||
condense_wildcard_suffixes = true
|
||||
max_width = 120
|
||||
enum_discrim_align_threshold = 25
|
||||
force_explicit_abi = false
|
||||
force_multiline_blocks = true
|
||||
|
|
28
Cargo.lock
generated
28
Cargo.lock
generated
|
@ -1,19 +1,19 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "embd"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
dependencies = [
|
||||
"embd-macros 0.1.1",
|
||||
"embd-macros 0.1.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "embd-macros"
|
||||
version = "0.1.1"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d8aff0b18a634834d77cf70effb2ee3f5ab2b4f36f1caa7fabe213ac7767b0f"
|
||||
checksum = "ad70201855c78504fbd460954eb86c33c8ed9262f3514ea3b6a55162fc231a24"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -22,7 +22,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "embd-macros"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -31,27 +31,27 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.83"
|
||||
version = "1.0.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b33eb56c327dec362a9e55b3ad14f9d2f0904fb5a5b03b513ab5465399e9f43"
|
||||
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.36"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.65"
|
||||
version = "2.0.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2863d96a84c6439701d7a38f9de935ec562c8832cc55d1dde0f513b52fad106"
|
||||
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -60,6 +60,6 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
version = "1.0.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034"
|
||||
|
|
|
@ -6,9 +6,9 @@ license = "MIT"
|
|||
keywords = ["embedding", "files", "debug-optimization", "bundling"]
|
||||
categories = ["filesystem"]
|
||||
authors = ["RGBCube"]
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
edition = "2021"
|
||||
include = [ "src/**/*.rs", "README.md" ]
|
||||
include = ["src/**/*.rs", "../README.md"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustc-args = ["--cfg", "procmacro2_semver_exempt"]
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
# embd-rs
|
||||
|
||||
A super simple file and directory embedding crate,
|
||||
that loads files from the filesystem on debug mode,
|
||||
allowing for quick edit-and-test cycles without compilation.
|
||||
|
||||
It is also super efficient, and does not heap allocate when the
|
||||
files are embedded on release mode by utilizing `std::borrow::Cow`.
|
||||
|
||||
On release mode it falls back to `include_str!`, `include_bytes!`
|
||||
and our own custom `include_dir!`-like implementation.
|
||||
|
||||
## Usage
|
||||
|
||||
Add this to your Cargo.toml:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
embd = "0.1"
|
||||
```
|
||||
|
||||
Then you can use this crate like so:
|
||||
|
||||
```rs
|
||||
let contents: Cow<'_, str> = embd::string!("path/to/file.txt");
|
||||
let bytes: Cow<'_, [u8]> = embd::bytes!("path/to/image.png");
|
||||
|
||||
let dir: embd::Dir = embd::dir!("path/to");
|
||||
let files: Vec<embd::File> = dir.flatten();
|
||||
```
|
||||
|
||||
Note that you will need to enable the `procmacro2_semver_exempt` cfg
|
||||
option to use this crate, you can enable it like so, by putting this in
|
||||
`.cargo/config.toml` in the project root:
|
||||
|
||||
```toml
|
||||
[build]
|
||||
rustflags = [ "--cfg", "procmacro2_semver_exempt" ]
|
||||
```
|
||||
|
||||
## To-Do
|
||||
|
||||
- [ ] Hot reloading of files in debug mode.
|
||||
- [ ] Don't depend on `procmacro2_semver_exempt`.
|
||||
|
||||
## License
|
||||
|
||||
```
|
||||
Copyright (c) 2023-present RGBCube
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
|
@ -74,14 +74,11 @@ fn dir_debug(path: &str) -> TokenStream {
|
|||
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
fn dir_release(input: TokenStream, path: &str) -> TokenStream {
|
||||
let neighbor = TokenStream::from(input).span().source_file().path();
|
||||
let neighbor = input.span().source_file().path();
|
||||
|
||||
let base = neighbor.parent().expect("Failed to get the parent of file");
|
||||
|
||||
let directory = base
|
||||
.join(path)
|
||||
.canonicalize()
|
||||
.expect("Failed to canonicalize path");
|
||||
let directory = base.join(path).canonicalize().expect("Failed to canonicalize path");
|
||||
|
||||
let directory_str = directory.to_str().expect("Failed to convert OsStr to str");
|
||||
|
||||
|
@ -108,9 +105,7 @@ fn read_dir(directory: &Path) -> TokenVec {
|
|||
.to_str()
|
||||
.expect("Failed to get the string representation of PathBuf");
|
||||
|
||||
let filetype = fs::metadata(&path)
|
||||
.expect("Failed to get file metadata")
|
||||
.file_type();
|
||||
let filetype = fs::metadata(&path).expect("Failed to get file metadata").file_type();
|
||||
|
||||
if filetype.is_dir() {
|
||||
let children = read_dir(&path);
|
||||
|
|
|
@ -6,9 +6,9 @@ license = "MIT"
|
|||
keywords = ["embedding", "files", "debug-optimization", "bundling"]
|
||||
categories = ["filesystem"]
|
||||
authors = ["RGBCube"]
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
edition = "2021"
|
||||
include = [ "src/**/*.rs", "README.md" ]
|
||||
include = ["src/**/*.rs", "../README.md"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustc-args = ["--cfg", "procmacro2_semver_exempt"]
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
# embd-rs
|
||||
|
||||
A super simple file and directory embedding crate,
|
||||
that loads files from the filesystem on debug mode,
|
||||
allowing for quick edit-and-test cycles without compilation.
|
||||
|
||||
It is also super efficient, and does not heap allocate when the
|
||||
files are embedded on release mode by utilizing `std::borrow::Cow`.
|
||||
|
||||
On release mode it falls back to `include_str!`, `include_bytes!`
|
||||
and our own custom `include_dir!`-like implementation.
|
||||
|
||||
## Usage
|
||||
|
||||
Add this to your Cargo.toml:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
embd = "0.1"
|
||||
```
|
||||
|
||||
Then you can use this crate like so:
|
||||
|
||||
```rs
|
||||
let contents: Cow<'_, str> = embd::string!("path/to/file.txt");
|
||||
let bytes: Cow<'_, [u8]> = embd::bytes!("path/to/image.png");
|
||||
|
||||
let dir: embd::Dir = embd::dir!("path/to");
|
||||
let files: Vec<embd::File> = dir.flatten();
|
||||
```
|
||||
|
||||
Note that you will need to enable the `procmacro2_semver_exempt` cfg
|
||||
option to use this crate, you can enable it like so, by putting this in
|
||||
`.cargo/config.toml` in the project root:
|
||||
|
||||
```toml
|
||||
[build]
|
||||
rustflags = [ "--cfg", "procmacro2_semver_exempt" ]
|
||||
```
|
||||
|
||||
## To-Do
|
||||
|
||||
- [ ] Hot reloading of files in debug mode.
|
||||
- [ ] Don't depend on `procmacro2_semver_exempt`.
|
||||
|
||||
## License
|
||||
|
||||
```
|
||||
Copyright (c) 2023-present RGBCube
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
|
@ -1,6 +1,8 @@
|
|||
#![allow(unexpected_cfgs)]
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::VecDeque,
|
||||
fs,
|
||||
path::Path,
|
||||
};
|
||||
|
@ -31,9 +33,7 @@ pub fn __string_runtime(neighbor: &str, path: &str) -> String {
|
|||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// fn main() {
|
||||
/// let content: Cow<'static, str> = embd::string!("main.rs");
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
|
@ -66,10 +66,8 @@ pub fn __bytes_runtime(neighbor: &str, path: &str) -> Vec<u8> {
|
|||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// fn main() {
|
||||
/// // `assets/` is in the same directory as `src/`
|
||||
/// let content: Cow<'static, [u8]> = embd::string!("../assets/icon.png");
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
|
@ -128,17 +126,19 @@ impl Dir {
|
|||
|
||||
/// Collects all files from the directory into a vector.
|
||||
pub fn flatten(self) -> Vec<File> {
|
||||
let mut entries = Vec::new();
|
||||
let mut files = Vec::new();
|
||||
let mut dirs = VecDeque::from([self]);
|
||||
|
||||
for child in self.__children.into_owned() {
|
||||
// TODO: Eliminate allocation.
|
||||
while let Some(dir) = dirs.pop_front() {
|
||||
for child in dir.__children.iter().cloned() {
|
||||
match child {
|
||||
DirEntry::File(file) => entries.push(file),
|
||||
DirEntry::Dir(dir) => entries.append(&mut dir.flatten()),
|
||||
DirEntry::File(file) => files.push(file),
|
||||
DirEntry::Dir(dir) => dirs.push_back(dir),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entries
|
||||
files
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -171,15 +171,9 @@ fn read_dir(directory: &Path) -> Vec<DirEntry> {
|
|||
|
||||
let filetype = entry.file_type().expect("Failed to read entry filetype");
|
||||
|
||||
let path = entry
|
||||
.path()
|
||||
.canonicalize()
|
||||
.expect("Failed to canonicalize path");
|
||||
let path = entry.path().canonicalize().expect("Failed to canonicalize path");
|
||||
|
||||
let path_str = path
|
||||
.to_str()
|
||||
.expect("Failed to convert OsStr to str")
|
||||
.to_string();
|
||||
let path_str = path.to_str().expect("Failed to convert OsStr to str").to_string();
|
||||
|
||||
if filetype.is_dir() {
|
||||
let children = read_dir(&path);
|
||||
|
@ -210,10 +204,7 @@ pub fn __dir_runtime(neighbor: &str, path: &str) -> Dir {
|
|||
.canonicalize()
|
||||
.expect("Failed to canonicalize path");
|
||||
|
||||
let directory_str = directory
|
||||
.to_str()
|
||||
.expect("Failed to convert OsStr to str")
|
||||
.to_string();
|
||||
let directory_str = directory.to_str().expect("Failed to convert OsStr to str").to_string();
|
||||
|
||||
let children = read_dir(&directory);
|
||||
|
||||
|
@ -230,9 +221,7 @@ pub fn __dir_runtime(neighbor: &str, path: &str) -> Dir {
|
|||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// fn main() {
|
||||
/// let content: embd::Dir = embd::dir!("../assets");
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
pub use embd_macros::__dir as dir;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue