Skip to main content

ferroui_base/platform/
i_asset_loader.rs

1use crate::utilities::Uri;
2use std::fmt;
3use std::io::{Read, Seek};
4use std::sync::Arc;
5
6/// A readable, seekable stream over the content of an asset.
7pub trait AssetStream: Read + Seek {}
8
9impl<T: Read + Seek> AssetStream for T {}
10
11/// Identifies the unit assets are embedded in and looked up by: a crate that
12/// registered assets (see [`register_assets`](crate::platform::register_assets)).
13#[derive(Clone, Debug, PartialEq, Eq, Hash)]
14pub struct AssetAssembly {
15    name: Arc<str>,
16}
17
18impl AssetAssembly {
19    /// Identifies the crate with the given name.
20    pub fn new(name: &str) -> Self {
21        Self { name: Arc::from(name) }
22    }
23
24    /// The name of the crate.
25    pub fn name(&self) -> &str {
26        &self.name
27    }
28}
29
30impl fmt::Display for AssetAssembly {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        f.write_str(&self.name)
33    }
34}
35
36/// Loads assets compiled into the application binary.
37///
38/// The interface and its service registration are considered unstable; use
39/// the functions of [`AssetLoader`](crate::platform::AssetLoader) instead.
40pub trait IAssetLoader: 'static {
41    /// Sets the default assembly from which to load embedded-resource
42    /// (`resm:`) assets for which no assembly is specified.
43    fn set_default_assembly(&self, assembly: &AssetAssembly);
44
45    /// Checks if an asset with the specified URI exists.
46    ///
47    /// `base_uri` is a base URI to use if `uri` is relative.
48    fn exists(&self, uri: &Uri, base_uri: Option<&Uri>) -> bool;
49
50    /// Opens the asset with the requested URI.
51    ///
52    /// Fails with [`std::io::ErrorKind::NotFound`] when the asset could not
53    /// be found.
54    fn open(&self, uri: &Uri, base_uri: Option<&Uri>) -> std::io::Result<Box<dyn AssetStream>>;
55
56    /// Opens the asset with the requested URI and returns the asset stream
57    /// along with the assembly containing the asset.
58    fn open_and_get_assembly(
59        &self,
60        uri: &Uri,
61        base_uri: Option<&Uri>,
62    ) -> std::io::Result<(Box<dyn AssetStream>, AssetAssembly)>;
63
64    /// Extracts the assembly information from a URI.
65    fn get_assembly(&self, uri: &Uri, base_uri: Option<&Uri>) -> Option<AssetAssembly>;
66
67    /// Gets all assets of a folder and its subfolders that match the
68    /// specified URI.
69    fn get_assets(&self, uri: &Uri, base_uri: Option<&Uri>) -> Vec<Uri>;
70
71    /// Removes the cached descriptor of the assembly with the given name, so
72    /// that assets it registered afterwards become visible.
73    fn invalidate_assembly_cache(&self, name: &str);
74
75    /// Removes all cached assembly descriptors.
76    fn invalidate_assembly_cache_all(&self);
77}