Skip to content

Commit 5478428

Browse files
SeriousBugclaude
andcommitted
Make compression-zstd opt-in and add documentation
Changes: - Remove compression-zstd from default features (requires C bindings) - Add comprehensive documentation for zstd compression feature - Document compression functions and config methods - Reorganize README features section for clarity The zstd feature is now opt-in due to its C library dependency, which may not be compatible with all build environments. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5e5bb44 commit 5478428

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ zstd = "0.13"
2323
actix-web = "4.4"
2424

2525
[features]
26-
default = ["interpolate-folder-path", "include-exclude", "compression-zstd"]
26+
default = ["interpolate-folder-path", "include-exclude"]
2727
# Even in debug mode use a release embed.
2828
# We use this to test embed code in our tests.
2929
always-embed = ["rust-embed-for-web-impl/always-embed"]

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ archives already don't include their compressed versions. However you can
7676

7777
## Features
7878

79-
Both of the following features are enabled by default.
79+
### Default Features
8080

81-
### `interpolate-folder-path`
81+
The following features are enabled by default.
82+
83+
#### `interpolate-folder-path`
8284

8385
Allow environment variables and `~`s to be used in the `folder` path. Example:
8486

@@ -91,7 +93,7 @@ struct Asset;
9193
`~` will expand into your home folder, and `${PROJECT_NAME}` will expand into
9294
the value of the `PROJECT_NAME` environment variable.
9395

94-
### `include-exclude`
96+
#### `include-exclude`
9597

9698
You can filter which files are embedded by adding one or more `#[include = "*.txt"]` and `#[exclude = "*.jpg"]` attributes.
9799
Matching is done on relative file paths --the paths you use for the `.get` call-- via [`globset`](https://docs.rs/globset/latest/globset/).
@@ -111,7 +113,26 @@ For example, if you wanted to exclude all `.svg` files except for one named
111113
struct Assets;
112114
```
113115

114-
### `prefix`
116+
### Optional Features
117+
118+
#### `compression-zstd`
119+
120+
Enables zstd compression support for embedded files. When enabled, files will be compressed with zstd (in addition to gzip and brotli), allowing you to serve zstd-compressed content to clients that support it.
121+
122+
**Note:** This feature is **not enabled by default** because the `zstd` crate uses C bindings, which may not be compatible with all build environments.
123+
124+
To enable zstd compression, add this to your `Cargo.toml`:
125+
126+
```toml
127+
[dependencies]
128+
rust-embed-for-web = { version = "11.2.1", features = ["compression-zstd"] }
129+
```
130+
131+
You can also disable zstd compression for specific embeds using the `#[zstd = false]` attribute as described in the "Disabling compression" section above.
132+
133+
### Other Configuration
134+
135+
#### `prefix`
115136

116137
You can specify a prefix, which will be added to the path of the files. For example:
117138

impl/src/compress.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ pub(crate) fn compress_br(data: &[u8]) -> Option<Vec<u8>> {
4242
}
4343
}
4444

45+
/// Compresses data using zstd compression.
46+
///
47+
/// Returns the compressed data if it's smaller than the threshold, `None` otherwise.
48+
/// Uses compression level 3 as a balance between compression ratio and speed.
4549
#[cfg(feature = "compression-zstd")]
4650
pub(crate) fn compress_zstd(data: &[u8]) -> Option<Vec<u8>> {
4751
let mut data_zstd: Vec<u8> = Vec::new();
52+
// Level 3 provides good compression with reasonable speed for build-time compression
4853
let mut encoder = ZstdEncoder::new(&mut data_zstd, 3).expect("Failed to create zstd encoder");
4954
encoder
5055
.write_all(data)

utils/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl Config {
6161
self.br = status;
6262
}
6363

64+
/// Enable or disable zstd compression for embedded files.
6465
pub fn set_zstd(&mut self, status: bool) {
6566
self.zstd = status;
6667
}
@@ -109,6 +110,10 @@ impl Config {
109110
self.br
110111
}
111112

113+
/// Check if zstd compression should be used for embedded files.
114+
///
115+
/// Returns `false` when the compression-zstd feature is not enabled,
116+
/// even if the config value is set to `true`.
112117
pub fn should_zstd(&self) -> bool {
113118
#[cfg(feature = "compression-zstd")]
114119
{

0 commit comments

Comments
 (0)