Skip to main content

FileRegistry

Struct FileRegistry 

Source
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 implement Clone for get() 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>

Source

pub fn new(config: FileRegistryConfig<T>) -> Self

Creates a new registry with the given configuration.

The registry starts empty. Call add_dir to register directories, then refresh or access resources to trigger initialization.

Source

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.

Source

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.

Source

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.

Source

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
Source

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.

Source

pub fn names(&self) -> impl Iterator<Item = &str>

Returns an iterator over all registered names.

Source

pub fn len(&self) -> usize

Returns the number of registered resources.

Note: This counts both extensionless and with-extension entries, so it may be higher than the number of unique files.

Source

pub fn is_empty(&self) -> bool

Returns true if no resources are registered.

Source

pub fn clear(&mut self)

Clears all entries from the registry.

Source

pub fn dirs(&self) -> &[PathBuf]

Returns the registered directories.

Auto Trait Implementations§

§

impl<T> Freeze for FileRegistry<T>

§

impl<T> RefUnwindSafe for FileRegistry<T>
where T: RefUnwindSafe,

§

impl<T> Send for FileRegistry<T>
where T: Send,

§

impl<T> Sync for FileRegistry<T>
where T: Sync,

§

impl<T> Unpin for FileRegistry<T>
where T: Unpin,

§

impl<T> UnwindSafe for FileRegistry<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more