Module outbound

Source
Expand description

§Outbound Ports — Driven Services

Outbound ports define how the domain can use external services. They represent capabilities the application needs but doesn’t implement itself: persistence, notifications, inter-process communication.

§Overview

                         ┌──────────────┐
                         │    DOMAIN    │
                         │    Theme     │
                         │  Validation  │
                         └──────┬───────┘
                                │
     ╔══════════════════════════╧══════════════════════════════╗
     ║                   OUTBOUND PORTS                        ║
     ║                                                         ║
     ║   PersistPort ────► Save themes to disk                 ║
     ║   NotifyPort ─────► Show desktop notifications          ║
     ║   IpcPort ────────► Communicate with QuickShell         ║
     ║                                                         ║
     ╚═════╤════════════════════╤════════════════════╤═════════╝
           │                    │                    │
     ┌─────▼──────┐       ┌─────▼──────┐       ┌─────▼──────┐
     │FileAdapter │       │NotifAdapter│       │DbusAdapter │
     │            │       │            │       │            │
     │ Writes to  │       │ Sends via  │       │ Calls via  │
     │ filesystem │       │  D-Bus     │       │  D-Bus     │
     └─────┬──────┘       └─────┬──────┘       └─────┬──────┘
           │                    │                    │
     ┌─────▼──────┐       ┌─────▼──────┐       ┌─────▼──────┐
     │ Filesystem │       │   Desktop  │       │ QuickShell │
     │            │       │Notification│       │            │
     │ ~/.config/ │       │  Service   │       │   D-Bus    │
     │   fern/    │       │            │       │  Service   │
     └────────────┘       └────────────┘       └────────────┘

§Dependency Inversion

Outbound ports demonstrate the Dependency Inversion Principle:

  • The domain depends on abstractions (port traits)
  • Adapters depend on the domain (they implement port traits)
  • External systems are accessed only through adapters

This means the domain never knows about file systems, D-Bus, or any specific technology. It just calls trait methods.

ⓘ
// Domain code - doesn't know about D-Bus
fn reload_theme(ipc: &impl IpcPort, theme: &Theme) -> Result<()> {
    ipc.send_reload(theme)?;
    Ok(())
}

// Adapter - implements the port using D-Bus
struct DbusIpcAdapter { /* D-Bus connection */ }

impl IpcPort for DbusIpcAdapter {
    fn send_reload(&self, theme: &Theme) -> Result<()> {
        // D-Bus specific code here
    }
}

§Port Responsibilities

PortResponsibilityExternal System
PersistPortSave/load theme filesFilesystem
NotifyPortSend desktop notificationsNotification daemon
IpcPortCommunicate with QuickShellD-Bus

§Testing with Mocks

Because ports are traits, they’re easy to mock for testing:

ⓘ
use std::sync::atomic::{AtomicUsize, Ordering};

struct MockNotifyPort {
    call_count: AtomicUsize,
}

impl NotifyPort for MockNotifyPort {
    fn send(&self, _: Notification) -> Result<()> {
        self.call_count.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}

#[test]
fn sends_notification_on_error() {
    let mock = MockNotifyPort { call_count: AtomicUsize::new(0) };
    // ... test code that should send a notification ...
    assert_eq!(mock.call_count.load(Ordering::SeqCst), 1);
}

Structs§

NullIpcPort
A no-op implementation of IpcPort for testing.
NullNotifyPort
A no-op implementation of NotifyPort for testing.

Traits§

IpcPort
Port for inter-process communication with QuickShell.
NotifyPort
Port for sending desktop notifications.
PersistPort
Port for persisting configuration and themes to storage.