Skip to main content

StylesheetRegistry

Struct StylesheetRegistry 

Source
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:

  1. Check inline themes first
  2. Check file-based themes in registration order
  3. 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

Source

pub fn new() -> Self

Creates an empty stylesheet registry.

Source

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 resolution
  • yaml - 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
"#)?;
Source

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 resolution
  • theme - The pre-built theme instance
Source

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")?;
Source

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
Source

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 resolution
  • theme - The pre-built theme instance
Source

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 where name_with_ext is the relative path including extension (e.g., "themes/dark.yaml")
§Processing

This method applies the same logic as runtime file loading:

  1. YAML parsing: Each entry’s content is parsed as a theme definition
  2. Extension stripping: "themes/dark.yaml""themes/dark"
  3. Extension priority: When multiple files share a base name, the higher-priority extension wins (see STYLESHEET_EXTENSIONS)
  4. 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());
Source

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")?;
Source

pub fn contains(&self, name: &str) -> bool

Checks if a theme exists in the registry.

§Arguments
  • name - The theme name to check
Source

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

Returns an iterator over all registered theme names.

Source

pub fn len(&self) -> usize

Returns the number of registered themes.

Source

pub fn is_empty(&self) -> bool

Returns true if no themes are registered.

Source

pub fn clear(&mut self)

Clears all registered themes.

Source

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

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<EmbeddedSource<StylesheetResource>> for StylesheetRegistry

Source§

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).

Auto Trait Implementations§

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> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
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