pub struct FileRegistry<T> { /* private fields */ }Expand description
Generic registry for file-based resources.
Manages loading and accessing resources from multiple directories with consistent behavior for extension priority, collision detection, and dev/release modes.
§Type Parameter
T: The content type. Must implementCloneforget()to return owned values.
§Example
let config = FileRegistryConfig {
extensions: &[".yaml", ".yml"],
transform: |content| serde_yaml::from_str(content).map_err(|e| LoadError::Transform {
name: String::new(),
message: e.to_string(),
}),
};
let mut registry = FileRegistry::new(config);
registry.add_dir("./styles")?;
let definitions = registry.get("darcula")?;Implementations§
Source§impl<T: Clone> FileRegistry<T>
impl<T: Clone> FileRegistry<T>
Sourcepub fn new(config: FileRegistryConfig<T>) -> Self
pub fn new(config: FileRegistryConfig<T>) -> Self
Sourcepub fn add_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<(), LoadError>
pub fn add_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<(), LoadError>
Adds a directory to search for files.
Directories are searched in registration order. If files with the same name exist in multiple directories, a collision error is raised.
§Lazy Initialization
The directory is validated but not walked immediately. Walking happens on
first access or explicit refresh call.
§Errors
Returns LoadError::DirectoryNotFound if the path doesn’t exist or
isn’t a directory.
Sourcepub fn add_embedded(&mut self, name: &str, content: T)
pub fn add_embedded(&mut self, name: &str, content: T)
Adds pre-embedded content (for release builds).
Embedded resources are stored directly in memory, avoiding filesystem access at runtime. Useful for deployment scenarios.
§Note
Embedded resources shadow file-based resources with the same name.
Sourcepub fn refresh(&mut self) -> Result<(), LoadError>
pub fn refresh(&mut self) -> Result<(), LoadError>
Initializes/refreshes the registry from registered directories.
This walks all registered directories, discovers files, and builds the resolution map. Call this to:
- Pick up newly added files (in dev mode)
- Force re-initialization after adding directories
§Panics
Panics if a collision is detected (same name from different directories). This is intentional—collisions are configuration errors that must be fixed.
§Errors
Returns an error if directory walking fails.
Sourcepub fn get(&mut self, name: &str) -> Result<T, LoadError>
pub fn get(&mut self, name: &str) -> Result<T, LoadError>
Gets a resource by name, applying the transform if reading from disk.
In dev mode (when using LoadedEntry::File): re-reads file and transforms
on each call, enabling hot reload.
In release mode (when using LoadedEntry::Embedded): returns embedded
content directly.
§Errors
LoadError::NotFoundif the name doesn’t existLoadError::Ioif the file can’t be readLoadError::Transformif the transform function fails
Sourcepub fn get_entry(&self, name: &str) -> Option<&LoadedEntry<T>>
pub fn get_entry(&self, name: &str) -> Option<&LoadedEntry<T>>
Returns a reference to the entry if it exists.
Unlike get, this doesn’t trigger initialization or file reading.
Useful for checking if a name exists without side effects.
Sourcepub fn names(&self) -> impl Iterator<Item = &str>
pub fn names(&self) -> impl Iterator<Item = &str>
Returns an iterator over all registered names.