pub struct StylesheetRegistry { /* private fields */ }Expand description
Registry for stylesheet/theme resolution from multiple sources.
The registry maintains a unified view of themes from:
- Inline YAML strings (highest priority)
- Multiple filesystem directories
- Embedded content (for release builds)
§Resolution Order
When looking up a theme name:
- Check inline themes first
- Check file-based themes in registration order
- Return error if not found
§Hot Reloading
In development mode (debug builds), file-based themes are re-read and re-parsed on each access, enabling rapid iteration without restarts.
§Example
let mut registry = StylesheetRegistry::new();
// Add inline theme (highest priority)
registry.add_inline("custom", r#"
header:
fg: cyan
bold: true
"#)?;
// Add from directory
registry.add_dir("./themes")?;
// Get a theme
let theme = registry.get("darcula")?;Implementations§
Source§impl StylesheetRegistry
impl StylesheetRegistry
Sourcepub fn add_inline(
&mut self,
name: impl Into<String>,
yaml: &str,
) -> Result<(), StylesheetError>
pub fn add_inline( &mut self, name: impl Into<String>, yaml: &str, ) -> Result<(), StylesheetError>
Adds an inline theme from a YAML string.
Inline themes have the highest priority and will shadow any file-based themes with the same name.
§Arguments
name- The theme name for resolutionyaml- The YAML content defining the theme
§Errors
Returns an error if the YAML content cannot be parsed.
§Example
registry.add_inline("custom", r#"
header:
fg: cyan
bold: true
muted:
dim: true
"#)?;Sourcepub fn add_theme(&mut self, name: impl Into<String>, theme: Theme)
pub fn add_theme(&mut self, name: impl Into<String>, theme: Theme)
Adds a pre-parsed theme directly.
This is useful when you have a Theme instance already constructed
programmatically and want to register it in the registry.
§Arguments
name- The theme name for resolutiontheme- The pre-built theme instance
Sourcepub fn add_dir<P: AsRef<Path>>(
&mut self,
path: P,
) -> Result<(), StylesheetError>
pub fn add_dir<P: AsRef<Path>>( &mut self, path: P, ) -> Result<(), StylesheetError>
Adds a stylesheet directory to search for files.
Themes in the directory are resolved by their filename without
extension. For example, with directory ./themes:
"darcula"→./themes/darcula.yaml"monokai"→./themes/monokai.yaml
§Errors
Returns an error if the directory doesn’t exist.
§Example
registry.add_dir("./themes")?;
let theme = registry.get("darcula")?;Sourcepub fn add_embedded(&mut self, themes: HashMap<String, Theme>)
pub fn add_embedded(&mut self, themes: HashMap<String, Theme>)
Adds pre-embedded themes (for release builds).
Embedded themes are stored directly in memory without filesystem access.
This is typically used with include_str! to bundle themes at compile time.
§Arguments
themes- Map of theme name to parsed Theme
Sourcepub fn add_embedded_theme(&mut self, name: impl Into<String>, theme: Theme)
pub fn add_embedded_theme(&mut self, name: impl Into<String>, theme: Theme)
Adds a pre-embedded theme by name.
This is a convenience method for adding a single embedded theme.
§Arguments
name- The theme name for resolutiontheme- The pre-built theme instance
Sourcepub fn from_embedded_entries(
entries: &[(&str, &str)],
) -> Result<Self, StylesheetError>
pub fn from_embedded_entries( entries: &[(&str, &str)], ) -> Result<Self, StylesheetError>
Creates a registry from embedded stylesheet entries.
This is the primary entry point for compile-time embedded stylesheets,
typically called by the embed_styles! macro.
§Arguments
entries- Slice of(name_with_ext, yaml_content)pairs wherename_with_extis the relative path including extension (e.g.,"themes/dark.yaml")
§Processing
This method applies the same logic as runtime file loading:
- YAML parsing: Each entry’s content is parsed as a theme definition
- Extension stripping:
"themes/dark.yaml"→"themes/dark" - Extension priority: When multiple files share a base name, the
higher-priority extension wins (see
STYLESHEET_EXTENSIONS) - Dual registration: Each theme is accessible by both its base name and its full name with extension
§Errors
Returns an error if any YAML content fails to parse.
§Example
use standout::style::StylesheetRegistry;
// Typically generated by embed_styles! macro
let entries: &[(&str, &str)] = &[
("default.yaml", "header:\n fg: cyan\n bold: true"),
("themes/dark.yaml", "panel:\n fg: white"),
];
let mut registry = StylesheetRegistry::from_embedded_entries(entries).unwrap();
// Access by base name or full name
assert!(registry.get("default").is_ok());
assert!(registry.get("default.yaml").is_ok());
assert!(registry.get("themes/dark").is_ok());Sourcepub fn get(&mut self, name: &str) -> Result<Theme, StylesheetError>
pub fn get(&mut self, name: &str) -> Result<Theme, StylesheetError>
Gets a theme by name.
Looks up the theme in order: inline first, then file-based. In development mode, file-based themes are re-read on each access.
§Arguments
name- The theme name (with or without extension)
§Errors
Returns an error if the theme is not found or cannot be parsed.
§Example
let theme = registry.get("darcula")?;Sourcepub fn names(&self) -> impl Iterator<Item = &str>
pub fn names(&self) -> impl Iterator<Item = &str>
Returns an iterator over all registered theme names.
Sourcepub fn refresh(&mut self) -> Result<(), StylesheetError>
pub fn refresh(&mut self) -> Result<(), StylesheetError>
Refreshes file-based themes from disk.
This re-walks all registered directories and updates the internal cache. Useful in long-running applications that need to pick up theme changes without restarting.
§Errors
Returns an error if any directory cannot be read.
Trait Implementations§
Source§impl Default for StylesheetRegistry
impl Default for StylesheetRegistry
Source§impl From<EmbeddedSource<StylesheetResource>> for StylesheetRegistry
impl From<EmbeddedSource<StylesheetResource>> for StylesheetRegistry
Source§fn from(source: EmbeddedStyles) -> Self
fn from(source: EmbeddedStyles) -> Self
Converts embedded styles into a StylesheetRegistry.
In debug mode, if the source path exists, styles are loaded from disk (enabling hot-reload). Otherwise, embedded content is used.
§Panics
Panics if embedded YAML content fails to parse (should be caught in dev).