Skip to main content

goud_engine/assets/loader/
context.rs

1//! Load context provided to asset loaders during loading.
2
3use crate::assets::AssetPath;
4use std::fmt;
5
6/// Context provided to asset loaders during loading.
7///
8/// The context provides:
9/// - The path of the asset being loaded
10/// - Methods to declare dependencies on other assets
11/// - Metadata about the loading process
12///
13/// Loaders call [`add_dependency`](LoadContext::add_dependency) to declare
14/// that the asset being loaded depends on another asset. After loading
15/// completes, the `AssetServer` records these edges in the dependency
16/// graph so that cascade reloads work correctly.
17pub struct LoadContext<'a> {
18    /// The path of the asset being loaded (owned).
19    asset_path: AssetPath<'static>,
20    /// Paths of assets that this asset depends on.
21    dependencies: Vec<String>,
22    /// Marker for lifetime (in the future, this will hold references to AssetServer).
23    _marker: std::marker::PhantomData<&'a ()>,
24}
25
26impl<'a> LoadContext<'a> {
27    /// Creates a new load context for the given asset path.
28    pub fn new(asset_path: AssetPath<'static>) -> Self {
29        Self {
30            asset_path,
31            dependencies: Vec::new(),
32            _marker: std::marker::PhantomData,
33        }
34    }
35
36    /// Returns the path of the asset being loaded.
37    pub fn path(&self) -> &AssetPath<'static> {
38        &self.asset_path
39    }
40
41    /// Returns the asset path as a string.
42    pub fn path_str(&self) -> &str {
43        self.asset_path.as_str()
44    }
45
46    /// Returns the file extension of the asset.
47    pub fn extension(&self) -> Option<&str> {
48        self.asset_path.extension()
49    }
50
51    /// Returns the file name of the asset.
52    pub fn file_name(&self) -> Option<&str> {
53        self.asset_path.file_name()
54    }
55
56    /// Declares that the asset being loaded depends on another asset.
57    ///
58    /// After the load completes, the `AssetServer` will record this
59    /// dependency so that changes to `dependency_path` trigger a
60    /// cascade reload of the current asset.
61    ///
62    /// # Arguments
63    ///
64    /// * `dependency_path` - Path of the asset this asset depends on
65    pub fn add_dependency(&mut self, dependency_path: impl Into<String>) {
66        self.dependencies.push(dependency_path.into());
67    }
68
69    /// Returns the list of dependencies declared during loading.
70    pub fn dependencies(&self) -> &[String] {
71        &self.dependencies
72    }
73
74    /// Consumes the context and returns the collected dependencies.
75    pub(crate) fn into_dependencies(self) -> Vec<String> {
76        self.dependencies
77    }
78}
79
80impl<'a> fmt::Debug for LoadContext<'a> {
81    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82        f.debug_struct("LoadContext")
83            .field("asset_path", &self.asset_path)
84            .field("dependencies", &self.dependencies.len())
85            .finish()
86    }
87}