Skip to content

Commit

Permalink
refactor: rename asset encoding default and custom config methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed Aug 13, 2024
1 parent 970995d commit 63fc29c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 55 deletions.
4 changes: 2 additions & 2 deletions examples/http-certification/assets/src/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ fn collect_assets<'content, 'path>(
fn certify_all_assets() {
// 1. Define the asset certification configurations.
let encodings = vec![
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default(),
AssetEncoding::Brotli.default_config(),
AssetEncoding::Gzip.default_config(),
];

let asset_configs = vec![
Expand Down
36 changes: 18 additions & 18 deletions packages/ic-asset-certification/src/asset_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use std::fmt::{Display, Formatter};
/// fallback_for: vec![],
/// aliased_by: vec![],
/// encodings: vec![
/// AssetEncoding::Brotli.default(),
/// AssetEncoding::Gzip.default(),
/// AssetEncoding::Brotli.default_config(),
/// AssetEncoding::Gzip.default_config(),
/// ],
/// };
/// ```
Expand All @@ -61,8 +61,8 @@ use std::fmt::{Display, Formatter};
/// }],
/// aliased_by: vec!["/".to_string()],
/// encodings: vec![
/// AssetEncoding::Brotli.default(),
/// AssetEncoding::Gzip.default(),
/// AssetEncoding::Brotli.default_config(),
/// AssetEncoding::Gzip.default_config(),
/// ],
/// };
/// ```
Expand Down Expand Up @@ -113,8 +113,8 @@ use std::fmt::{Display, Formatter};
/// "/not-found/index.html".to_string(),
/// ],
/// encodings: vec![
/// AssetEncoding::Brotli.default(),
/// AssetEncoding::Gzip.default(),
/// AssetEncoding::Brotli.default_config(),
/// AssetEncoding::Gzip.default_config(),
/// ],
/// };
/// ```
Expand All @@ -135,8 +135,8 @@ use std::fmt::{Display, Formatter};
/// ("Cache-Control".to_string(), "public, max-age=31536000, immutable".to_string()),
/// ],
/// encodings: vec![
/// AssetEncoding::Brotli.default(),
/// AssetEncoding::Gzip.default(),
/// AssetEncoding::Brotli.default_config(),
/// AssetEncoding::Gzip.default_config(),
/// ],
/// };
/// ```
Expand Down Expand Up @@ -245,7 +245,7 @@ pub enum AssetConfig {
/// Each entry is a tuple of the [encoding name](AssetEncoding) and the
/// file extension used in the file path. For example, to include Brotli
/// and Gzip encodings:
/// `vec![AssetEncoding::Brotli.default(), AssetEncoding::Gzip.default()]`
/// `vec![AssetEncoding::Brotli.default_config(), AssetEncoding::Gzip.default_config()]`
///
/// Each encoding referenced must be provided to the asset router as a
/// separate file with the same filename as the original file, but with
Expand Down Expand Up @@ -324,7 +324,7 @@ pub enum AssetConfig {
/// Each entry is a tuple of the [encoding name](AssetEncoding) and the
/// file extension used in the file path. For example, to include Brotli
/// and Gzip encodings:
/// `vec![AssetEncoding::Brotli.default(), AssetEncoding::Gzip.default()]`
/// `vec![AssetEncoding::Brotli.default_config(), AssetEncoding::Gzip.default_config()]`
///
/// Each encoding referenced must be provided to the asset router as a
/// separate file with the same filename as the original file, but with
Expand Down Expand Up @@ -435,23 +435,23 @@ impl AssetEncoding {
/// ```
/// use ic_asset_certification::AssetEncoding;
///
/// let (encoding, extension) = AssetEncoding::Brotli.default();
/// let (encoding, extension) = AssetEncoding::Brotli.default_config();
/// assert_eq!(encoding, AssetEncoding::Brotli);
/// assert_eq!(extension, "br");
///
/// let (encoding, extension) = AssetEncoding::Zstd.default();
/// let (encoding, extension) = AssetEncoding::Zstd.default_config();
/// assert_eq!(encoding, AssetEncoding::Zstd);
/// assert_eq!(extension, "zst");
///
/// let (encoding, extension) = AssetEncoding::Gzip.default();
/// let (encoding, extension) = AssetEncoding::Gzip.default_config();
/// assert_eq!(encoding, AssetEncoding::Gzip);
/// assert_eq!(extension, "gz");
///
/// let (encoding, extension) = AssetEncoding::Deflate.default();
/// let (encoding, extension) = AssetEncoding::Deflate.default_config();
/// assert_eq!(encoding, AssetEncoding::Deflate);
/// assert_eq!(extension, "zz");
/// ```
pub fn default(self) -> (AssetEncoding, String) {
pub fn default_config(self) -> (AssetEncoding, String) {
let file_extension = match self {
AssetEncoding::Brotli => "br".to_string(),
AssetEncoding::Zstd => "zst".to_string(),
Expand All @@ -463,20 +463,20 @@ impl AssetEncoding {
}

/// Returns an encoding with a custom file extension. This is useful
/// when the default file extension assigned by [default](AssetEncoding::default)
/// when the default file extension assigned by [default_config](AssetEncoding::default_config)
/// is not desired.
///
/// # Examples
///
/// ```
/// use ic_asset_certification::AssetEncoding;
///
/// let (encoding, extension) = AssetEncoding::Brotli.custom("brotli".to_string());
/// let (encoding, extension) = AssetEncoding::Brotli.custom_config("brotli".to_string());
///
/// assert_eq!(encoding, AssetEncoding::Brotli);
/// assert_eq!(extension, "brotli");
/// ```
pub fn custom(self, extension: String) -> (AssetEncoding, String) {
pub fn custom_config(self, extension: String) -> (AssetEncoding, String) {
(self, extension)
}
}
Expand Down
36 changes: 18 additions & 18 deletions packages/ic-asset-certification/src/asset_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ struct CertifiedAssetResponse<'a> {
/// }],
/// aliased_by: vec!["/".to_string()],
/// encodings: vec![
/// AssetEncoding::Brotli.default(),
/// AssetEncoding::Gzip.default(),
/// AssetEncoding::Brotli.default_config(),
/// AssetEncoding::Gzip.default_config(),
/// ],
/// },
/// AssetConfig::Pattern {
Expand All @@ -63,8 +63,8 @@ struct CertifiedAssetResponse<'a> {
/// "public, max-age=31536000, immutable".to_string(),
/// )],
/// encodings: vec![
/// AssetEncoding::Brotli.default(),
/// AssetEncoding::Gzip.default(),
/// AssetEncoding::Brotli.default_config(),
/// AssetEncoding::Gzip.default_config(),
/// ],
/// },
/// AssetConfig::Pattern {
Expand All @@ -75,8 +75,8 @@ struct CertifiedAssetResponse<'a> {
/// "public, max-age=31536000, immutable".to_string(),
/// )],
/// encodings: vec![
/// AssetEncoding::Brotli.default(),
/// AssetEncoding::Gzip.default(),
/// AssetEncoding::Brotli.default_config(),
/// AssetEncoding::Gzip.default_config(),
/// ],
/// },
/// AssetConfig::Redirect {
Expand Down Expand Up @@ -1551,9 +1551,9 @@ mod tests {
}],
aliased_by: vec!["/".to_string()],
encodings: vec![
AssetEncoding::Gzip.default(),
AssetEncoding::Deflate.default(),
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default_config(),
AssetEncoding::Deflate.default_config(),
AssetEncoding::Brotli.default_config(),
],
},
AssetConfig::Pattern {
Expand All @@ -1564,9 +1564,9 @@ mod tests {
"public, max-age=31536000, immutable".to_string(),
)],
encodings: vec![
AssetEncoding::Gzip.default(),
AssetEncoding::Deflate.default(),
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default_config(),
AssetEncoding::Deflate.default_config(),
AssetEncoding::Brotli.default_config(),
],
},
AssetConfig::Pattern {
Expand All @@ -1577,9 +1577,9 @@ mod tests {
"public, max-age=31536000, immutable".to_string(),
)],
encodings: vec![
AssetEncoding::Gzip.default(),
AssetEncoding::Deflate.default(),
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default_config(),
AssetEncoding::Deflate.default_config(),
AssetEncoding::Brotli.default_config(),
],
},
AssetConfig::File {
Expand Down Expand Up @@ -1607,9 +1607,9 @@ mod tests {
],
// [TODO] - add these files and tests for them
encodings: vec![
AssetEncoding::Gzip.default(),
AssetEncoding::Deflate.default(),
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default_config(),
AssetEncoding::Deflate.default_config(),
AssetEncoding::Brotli.default_config(),
],
},
AssetConfig::Redirect {
Expand Down
32 changes: 16 additions & 16 deletions packages/ic-asset-certification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ In both cases, the following options can be configured for each asset:
- A list of alternative encodings that can be used to serve the asset.
- Each entry is a tuple of the [encoding name](AssetEncoding) and the file
extension used in the file path, that can be conveniently created with
the `default` factory method. For example, to include Brotli and Gzip encodings:
`vec![AssetEncoding::Brotli.default(), AssetEncoding::Gzip.default()]`.
the `default_config` factory method. For example, to include Brotli and Gzip encodings:
`vec![AssetEncoding::Brotli.default_config(), AssetEncoding::Gzip.default_config()]`.
- The default file extensions for each encoding are:
- Brotli: `br`
- Gzip: `gz`
- Deflate: `zz`
- Zstd: `zst`
- Alternatively, a custom file extension can be provided for each encoding
by using the `custom` factory method. For example, to include a custom
by using the `custom_config` factory method. For example, to include a custom
file extension for Brotli and Gzip encodings:
`vec![AssetEncoding::Brotli.custom("brotli"), AssetEncoding::Gzip.custom("gzip")]`.
`vec![AssetEncoding::Brotli.custom_config("brotli"), AssetEncoding::Gzip.custom_config("gzip")]`.
- Each encoding referenced must be provided to the asset router as a
separate file with the same filename as the original file, but with an
additional file extension matching the configuration. For example, if the
Expand Down Expand Up @@ -179,8 +179,8 @@ let config = AssetConfig::File {
}],
aliased_by: vec!["/".to_string()],
encodings: vec![
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default(),
AssetEncoding::Brotli.default_config(),
AssetEncoding::Gzip.default_config(),
],
};
```
Expand Down Expand Up @@ -229,8 +229,8 @@ let config = AssetConfig::File {
"/not-found/index.html".to_string(),
],
encodings: vec![
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default(),
AssetEncoding::Brotli.default_config(),
AssetEncoding::Gzip.default_config(),
],
};
```
Expand Down Expand Up @@ -276,8 +276,8 @@ let config = AssetConfig::Pattern {
("Cache-Control".to_string(), "public, max-age=31536000, immutable".to_string()),
],
encodings: vec![
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default(),
AssetEncoding::Brotli.default_config(),
AssetEncoding::Gzip.default_config(),
],
};
```
Expand Down Expand Up @@ -386,8 +386,8 @@ let asset_configs = vec![
}],
aliased_by: vec!["/".to_string()],
encodings: vec![
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default(),
AssetEncoding::Brotli.default_config(),
AssetEncoding::Gzip.default_config(),
],
},
AssetConfig::Pattern {
Expand All @@ -398,8 +398,8 @@ let asset_configs = vec![
"public, max-age=31536000, immutable".to_string(),
)],
encodings: vec![
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default(),
AssetEncoding::Brotli.default_config(),
AssetEncoding::Gzip.default_config(),
],
},
AssetConfig::Pattern {
Expand All @@ -410,8 +410,8 @@ let asset_configs = vec![
"public, max-age=31536000, immutable".to_string(),
)],
encodings: vec![
AssetEncoding::Brotli.default(),
AssetEncoding::Gzip.default(),
AssetEncoding::Brotli.default_config(),
AssetEncoding::Gzip.default_config(),
],
},
AssetConfig::Redirect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ic_http_certification::{
CERTIFICATE_EXPRESSION_HEADER_NAME, CERTIFICATE_HEADER_NAME,
};
use ic_representation_independent_hash::hash;
use std::{ascii::AsciiExt, collections::HashMap};
use std::collections::HashMap;

/// The minimum verification version supported by this package.
pub const MIN_VERIFICATION_VERSION: u8 = 1;
Expand Down

0 comments on commit 63fc29c

Please sign in to comment.