pub struct Assets { /* private fields */ }
Expand description
A set of assets.
This is one of the two main entry points of this library. You create an
instance of this type via Assets::new
and then retrieve asset data via
Assets::get
. The macro assets!
is the other main entry
point of this library. It generates a Setup
value at compile time which
you have to pass to Assets::new
.
Implementations§
source§impl Assets
impl Assets
sourcepub async fn new(setup: Setup, config: Config) -> Result<Self, Error>
pub async fn new(setup: Setup, config: Config) -> Result<Self, Error>
Creates a new instance of this type and, in prod mode, prepares all assets.
sourcepub async fn get(&self, public_path: &str) -> Result<Option<Bytes>, GetError>
pub async fn get(&self, public_path: &str) -> Result<Option<Bytes>, GetError>
Returns the file contents of the asset referred to by public_path
.
The given path is the “public” path, as it is a part of the actual
request URI. This doesn’t mean this parameter has to be the full path
component from the request URI; you likely want to serve your assets in
a subdirectory, like /assets/
which you would have to remove from the
URI-path before calling this method. However, for assets with hashed
filenames, this method expects the hashed path and not the one you
specified in assets!
.
If no asset with the specified path exists, Ok(None)
is returned. An
error is returned in debug mode for a variety of reasons. In prod mode,
this method always returns Ok(_)
. See GetError
.
sourcepub fn lookup(&self, public_path: &str) -> Option<AssetId>
pub fn lookup(&self, public_path: &str) -> Option<AssetId>
Resolves the public path to an asset ID. If the public path does not
match any asset, None
is returned.
sourcepub fn asset_ids(&self) -> impl Iterator<Item = AssetId>
pub fn asset_ids(&self) -> impl Iterator<Item = AssetId>
Returns an iterator over the IDs of all assets.
sourcepub fn asset_info(&self, id: AssetId) -> Info<'_>
pub fn asset_info(&self, id: AssetId) -> Info<'_>
Returns meta information about a specific asset. Panics if no asset with the given ID exists.