Trait PersistPort

Source
pub trait PersistPort: Send + Sync {
    // Required methods
    fn save_theme(&self, theme: &Theme, path: impl AsRef<Path>) -> Result<()>;
    fn load_theme(&self, path: impl AsRef<Path>) -> Result<Theme>;
    fn config_dir(&self) -> Option<PathBuf>;
    fn ensure_config_dir(&self) -> Result<PathBuf>;
    fn exists(&self, path: impl AsRef<Path>) -> bool;
}
Expand description

Port for persisting configuration and themes to storage.

PersistPort abstracts over different storage backends, enabling the domain to save and load themes without knowing the underlying storage mechanism.

§Implementations

  • Filesystem — Writes JSON files to ~/.config/fern/
  • XDG — Follows XDG Base Directory Specification
  • Memory — In-memory storage for testing

§File Structure

The recommended file structure for persisted themes:

~/.config/fern/
├── config.toml      # User configuration (source of truth)
├── config.json      # Generated JSON for QuickShell
└── themes/
    ├── dark.json    # Built-in dark theme
    ├── light.json   # Built-in light theme
    └── custom.json  # User-created themes

§Example

ⓘ
use fernctl::ports::outbound::PersistPort;
use fernctl::adapters::FileSystemAdapter;

let adapter = FileSystemAdapter::new();

// Save a theme
adapter.save_theme(&theme, "~/.config/fern/config.json")?;

// Load a theme
let loaded = adapter.load_theme("~/.config/fern/config.json")?;

Required Methods§

Source

fn save_theme(&self, theme: &Theme, path: impl AsRef<Path>) -> Result<()>

Saves a theme to the specified path.

The theme is serialized to JSON format for QuickShell compatibility.

§Arguments
  • theme — The theme to save
  • path — The file path to write to
§Errors

Returns [FernError::Io] if the file cannot be written.

§Example
ⓘ
adapter.save_theme(&theme, "config.json")?;
Source

fn load_theme(&self, path: impl AsRef<Path>) -> Result<Theme>

Loads a theme from the specified path.

§Arguments
  • path — The file path to read from
§Returns

The loaded Theme.

§Errors
  • [FernError::Io] if the file cannot be read
  • [ConfigError] if the file content is invalid
Source

fn config_dir(&self) -> Option<PathBuf>

Returns the default configuration directory.

This follows platform conventions:

  • Linux: ~/.config/fern/
  • macOS: ~/Library/Application Support/fern/
  • Windows: %APPDATA%\fern\
§Returns

The path to the configuration directory, or None if it cannot be determined.

Source

fn ensure_config_dir(&self) -> Result<PathBuf>

Ensures the configuration directory exists.

Creates the directory and any parent directories if they don’t exist.

§Errors

Returns [FernError::Io] if the directory cannot be created.

Source

fn exists(&self, path: impl AsRef<Path>) -> bool

Checks if a configuration file exists.

§Arguments
  • path — The file path to check
§Returns

true if the file exists and is readable.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§