Skip to main content

goud_engine/assets/asset/
asset_info.rs

1//! Runtime metadata about an asset type.
2
3use super::asset_id::AssetId;
4use super::asset_type::AssetType;
5use super::trait_def::Asset;
6use std::fmt;
7
8/// Runtime metadata about an asset type.
9///
10/// Contains information about an asset type that can be queried at runtime.
11/// Useful for debugging, logging, and asset management UI.
12///
13/// # Example
14///
15/// ```
16/// use goud_engine::assets::{Asset, AssetInfo, AssetType};
17///
18/// struct Texture {
19///     width: u32,
20///     height: u32,
21///     data: Vec<u8>,
22/// }
23///
24/// impl Asset for Texture {
25///     fn asset_type_name() -> &'static str {
26///         "Texture"
27///     }
28///
29///     fn asset_type() -> AssetType {
30///         AssetType::Texture
31///     }
32///
33///     fn extensions() -> &'static [&'static str] {
34///         &["png", "jpg"]
35///     }
36/// }
37///
38/// let info = AssetInfo::of::<Texture>();
39/// assert_eq!(info.name, "Texture");
40/// assert_eq!(info.asset_type, AssetType::Texture);
41/// assert!(info.extensions.contains(&"png"));
42/// ```
43#[derive(Debug, Clone)]
44pub struct AssetInfo {
45    /// Unique identifier for this asset type.
46    pub id: AssetId,
47
48    /// Human-readable type name.
49    pub name: &'static str,
50
51    /// Size of the asset type in bytes.
52    pub size: usize,
53
54    /// Alignment of the asset type in bytes.
55    pub align: usize,
56
57    /// Asset category.
58    pub asset_type: AssetType,
59
60    /// Supported file extensions.
61    pub extensions: &'static [&'static str],
62}
63
64impl AssetInfo {
65    /// Creates `AssetInfo` for a specific asset type.
66    ///
67    /// # Example
68    ///
69    /// ```
70    /// use goud_engine::assets::{Asset, AssetInfo};
71    ///
72    /// struct MyAsset {
73    ///     value: i32,
74    /// }
75    /// impl Asset for MyAsset {}
76    ///
77    /// let info = AssetInfo::of::<MyAsset>();
78    /// assert_eq!(info.size, std::mem::size_of::<MyAsset>());
79    /// ```
80    pub fn of<T: Asset>() -> Self {
81        Self {
82            id: AssetId::of::<T>(),
83            name: T::asset_type_name(),
84            size: std::mem::size_of::<T>(),
85            align: std::mem::align_of::<T>(),
86            asset_type: T::asset_type(),
87            extensions: T::extensions(),
88        }
89    }
90}
91
92impl fmt::Display for AssetInfo {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        write!(
95            f,
96            "AssetInfo {{ name: \"{}\", type: {}, size: {}, extensions: {:?} }}",
97            self.name, self.asset_type, self.size, self.extensions
98        )
99    }
100}