Skip to content

Refactor CliContext to use Arc-ed trait object for app strategy #25

Description

@brooksmtownsend

Description

The CliContext struct currently uses conditional compilation to store either Xdg or Windows strategy types directly. This can be simplified by using an Arc-ed trait object, which would eliminate the need for conditional compilation and reduce code duplication.

Location

crates/wash/src/cli/mod.rs:259 in the CliContext struct definition

Current Behavior

#[derive(Debug, Clone)]
pub struct CliContext {
    // TODO(GFI): Just store an Arc-ed trait object
    #[cfg(unix)]
    app_strategy: Xdg,
    #[cfg(windows)]
    app_strategy: Windows,
    // ... other fields
}

This requires separate Deref implementations for each platform and conditional compilation throughout the codebase.

Proposed Solution

Replace the platform-specific fields with a trait object:

// Define a common trait for directory strategies
trait DirectoryStrategy: Debug + Send + Sync {
    fn config_dir(&self) -> PathBuf;
    fn data_dir(&self) -> PathBuf;
    fn cache_dir(&self) -> PathBuf;
    // ... other common methods
}

// Implement the trait for existing types
impl DirectoryStrategy for Xdg { /* ... */ }
impl DirectoryStrategy for Windows { /* ... */ }

#[derive(Debug, Clone)]
pub struct CliContext {
    app_strategy: Arc<dyn DirectoryStrategy>,
    // ... other fields
}

Benefits

  • Eliminates conditional compilation: Single implementation regardless of platform
  • Cleaner code: No need for duplicate Deref implementations
  • Better testability: Easier to mock directory strategies for testing
  • Extensibility: Could support custom directory strategies in the future

Implementation Notes

  • The directories crate types (Xdg, Windows) would need wrapper types to implement the trait
  • Consider using Box<dyn DirectoryStrategy> if Arc isn't needed for this specific case
  • Ensure Debug and Clone implementations work correctly with the trait object

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions