wallflow 0.5.2

Elegant wallpaper management with smooth transitions, powered by awww
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# Wallpaper.rs Refactoring Plan

## Current State Analysis

### Problems with Current `src/wallpaper.rs`

1. **Mixed Responsibilities**: Single file handles:

   - HTTP client management
   - JSON parsing for specific APIs
   - File I/O operations
   - Wallpaper application logic
   - System integration (KDE, pywal)

2. **Hardcoded Implementations**:

   - Wallhaven and Picsum logic baked into specific functions
   - No abstraction for adding new sources
   - Duplicate HTTP client creation

3. **Unused Configuration**:

   - `config.cleanup.*` - Not implemented
   - `config.advanced.*` - Parallel downloads, retries, timeouts ignored
   - `config.logging.*` - Custom logging not used

4. **Limited Error Handling**:
   - Generic error types
   - No retry logic
   - No rate limiting

## Proposed Module Structure

```
src/
├── wallpaper/               # Main wallpaper module
│   ├── mod.rs              # Public API
│   ├── apply.rs            # Wallpaper application logic
│   └── manager.rs          # High-level orchestration
├── downloaders/            # Downloader system
│   ├── mod.rs              # Public API & registry
│   ├── traits.rs           # Core traits
│   ├── registry.rs         # Downloader registry
│   ├── apod.rs             # NASA APOD downloader
│   ├── bing.rs             # Bing Daily downloader
│   ├── earthview.rs        # Google Earth View
│   ├── reddit.rs           # Reddit downloader
│   └── wallhaven.rs        # Existing Wallhaven (refactored)
├── http/                   # HTTP utilities
│   ├── mod.rs              # Public API
│   ├── client.rs           # Shared HTTP client
│   ├── cache.rs            # Response caching
│   └── retry.rs            # Retry logic with backoff
├── filesystem/             # File management
│   ├── mod.rs              # Public API
│   ├── cleanup.rs          # Implement config.cleanup logic
│   └── local.rs            # Local file scanning (extracted)
└── integration/            # System integration
    ├── mod.rs              # Public API
    ├── pywal.rs            # Color scheme integration
    └── desktop.rs          # Desktop environment integration
```

## Implementation Plan

### Phase 1: Core Abstractions (3-4 days)

#### Step 1.1: Create Downloader Trait

**File**: `src/downloaders/traits.rs`

```rust
use anyhow::Result;
use async_trait::async_trait;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct DownloadRequest {
    pub category: Option<String>,
    pub resolution: Option<(u32, u32)>,
    pub quality: Option<String>,
}

#[derive(Debug)]
pub struct DownloadedWallpaper {
    pub file_path: PathBuf,
    pub source_url: Option<String>,
    pub title: Option<String>,
    pub description: Option<String>,
    pub metadata: WallpaperMetadata,
}

#[derive(Debug)]
pub struct WallpaperMetadata {
    pub dimensions: Option<(u32, u32)>,
    pub file_size: Option<u64>,
    pub format: Option<String>,
    pub source: String,
    pub downloaded_at: chrono::DateTime<chrono::Utc>,
}

#[async_trait]
pub trait WallpaperDownloader {
    /// Download a wallpaper based on the request
    async fn download(&self, request: &DownloadRequest) -> Result<DownloadedWallpaper>;

    /// Get the source name for this downloader
    fn source_name(&self) -> &'static str;

    /// Check if this source requires authentication
    fn requires_auth(&self) -> bool { false }

    /// Validate configuration for this downloader
    fn validate_config(&self, config: &crate::config::Config) -> Result<()> { Ok(()) }
}
```

#### Step 1.2: HTTP Client Utilities

**File**: `src/http/client.rs`

```rust
use anyhow::{Context, Result};
use reqwest::{Client, Response};
use std::time::Duration;

pub struct HttpClient {
    client: Client,
    timeout: Duration,
    retry_attempts: u32,
}

impl HttpClient {
    pub fn new(timeout_secs: u32, retry_attempts: u32) -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(timeout_secs as u64))
            .user_agent("wallflow/1.0")
            .build()
            .expect("Failed to create HTTP client");

        Self {
            client,
            timeout: Duration::from_secs(timeout_secs as u64),
            retry_attempts,
        }
    }

    pub async fn get_json<T>(&self, url: &str) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        // Implementation with retry logic
    }

    pub async fn download_file(&self, url: &str, path: &Path) -> Result<()> {
        // Implementation with progress tracking
    }
}
```

#### Step 1.3: Downloader Registry

**File**: `src/downloaders/registry.rs`

```rust
use super::traits::WallpaperDownloader;
use anyhow::{anyhow, Result};
use std::collections::HashMap;
use std::sync::Arc;

pub struct DownloaderRegistry {
    downloaders: HashMap<String, Arc<dyn WallpaperDownloader + Send + Sync>>,
}

impl DownloaderRegistry {
    pub fn new() -> Self {
        let mut registry = Self {
            downloaders: HashMap::new(),
        };

        // Register built-in downloaders
        registry.register_builtin_downloaders();
        registry
    }

    fn register_builtin_downloaders(&mut self) {
        // Register each downloader
    }

    pub fn get_downloader(&self, source: &str) -> Result<Arc<dyn WallpaperDownloader + Send + Sync>> {
        self.downloaders.get(source)
            .cloned()
            .ok_or_else(|| anyhow!("Unknown wallpaper source: {}", source))
    }

    pub fn list_sources(&self) -> Vec<String> {
        self.downloaders.keys().cloned().collect()
    }
}
```

### Phase 2: Migrate Existing Functionality (2-3 days)

#### Step 2.1: Extract Wallpaper Application Logic

**File**: `src/wallpaper/apply.rs`

```rust
// Move apply_wallpaper, set_wallpaper_awww, set_wallpaper_kde from wallpaper.rs
// Clean interface focusing just on applying downloaded wallpapers
```

#### Step 2.2: Implement Cleanup Functionality

**File**: `src/filesystem/cleanup.rs`

```rust
use anyhow::Result;
use std::path::Path;
use crate::config::CleanupConfig;

pub struct CleanupManager<'a> {
    config: &'a CleanupConfig,
    download_dir: &'a Path,
}

impl<'a> CleanupManager<'a> {
    pub fn new(config: &'a CleanupConfig, download_dir: &'a Path) -> Self {
        Self { config, download_dir }
    }

    pub async fn cleanup_if_needed(&self) -> Result<u32> {
        if !self.config.auto_cleanup {
            return Ok(0);
        }

        let removed = self.remove_old_wallpapers().await?;
        if removed > 0 {
            info!("🧹 Cleaned up {} old wallpapers", removed);
        }

        Ok(removed)
    }

    async fn remove_old_wallpapers(&self) -> Result<u32> {
        // Implementation: keep only the most recent config.keep_count files
    }
}
```

#### Step 2.3: Refactor Existing Downloaders

Move Wallhaven and Picsum into the new structure:

**File**: `src/downloaders/wallhaven.rs`

```rust
use super::traits::*;
use crate::http::HttpClient;

pub struct WallhavenDownloader {
    client: HttpClient,
}

#[async_trait]
impl WallpaperDownloader for WallhavenDownloader {
    async fn download(&self, request: &DownloadRequest) -> Result<DownloadedWallpaper> {
        // Refactored from existing set_wallhaven function
    }

    fn source_name(&self) -> &'static str {
        "wallhaven"
    }
}
```

### Phase 3: New Downloaders (5-7 days)

#### Step 3.1: NASA APOD (1 day)

**File**: `src/downloaders/apod.rs`

```rust
pub struct ApodRequest {
    client: HttpClient,
}

#[derive(serde::Deserialize)]
struct ApodResponse {
    date: String,
    title: String,
    explanation: String,
    hdurl: Option<String>,
    url: String,
    media_type: String,
}

#[async_trait]
impl WallpaperDownloader for ApodRequest {
    async fn download(&self, request: &DownloadRequest) -> Result<DownloadedWallpaper> {
        // NASA APOD API: https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY
        // Educational: JSON parsing, optional fields handling
    }
}
```

#### Step 3.2: Bing Daily (1 day)

**File**: `src/downloaders/bing.rs`

```rust
#[derive(serde::Deserialize)]
struct BingResponse {
    images: Vec<BingImage>,
}

#[derive(serde::Deserialize)]
struct BingImage {
    url: String,
    title: String,
    copyright: String,
}

// Educational: XML/JSON hybrid APIs, URL construction
```

#### Step 3.3: Google Earth View (1 day)

**File**: `src/downloaders/earthview.rs`

```rust
// Educational: Geographic data, large image handling
```

#### Step 3.4: Reddit (2 days)

**File**: `src/downloaders/reddit.rs`

```rust
// Educational: Social media APIs, content filtering, JSON traversal
```

#### Step 3.5: MediaRSS (2 days)

**File**: `src/downloaders/mediarss.rs`

```rust
// Educational: RSS/XML parsing, feed validation
```

### Phase 4: Integration & Polish (2-3 days)

#### Step 4.1: Update Main Wallpaper Module

**File**: `src/wallpaper/mod.rs`

```rust
use crate::config::Config;
use crate::downloaders::DownloaderRegistry;
use anyhow::Result;

pub async fn set_wallpaper_by_source(config: &Config) -> Result<()> {
    let registry = DownloaderRegistry::new();
    let downloader = registry.get_downloader(&config.sources.default)?;

    let request = build_download_request(config)?;
    let wallpaper = downloader.download(&request).await?;

    // Apply wallpaper
    apply::apply_wallpaper(&wallpaper.file_path, config).await?;

    // Cleanup old downloads
    let cleanup_manager = crate::filesystem::cleanup::CleanupManager::new(
        &config.cleanup,
        Path::new(&config.paths.downloads)
    );
    cleanup_manager.cleanup_if_needed().await?;

    Ok(())
}
```

#### Step 4.2: Enhanced Configuration Usage

Implement all the unused config options:

```rust
// Use config.advanced.* in HTTP client
// Use config.cleanup.* in cleanup manager
// Use config.logging.* for structured logging
```

#### Step 4.3: Update CLI Commands

Add commands for testing individual downloaders:

```bash
cargo run -- test-downloader apod
cargo run -- test-downloader bing
cargo run -- list-sources
```

## Educational Benefits

### Rust Learning Outcomes

1. **Trait System**: Abstract interfaces for downloaders
2. **Async Programming**: Concurrent downloads, timeouts
3. **Error Handling**: Custom error types, graceful degradation
4. **Module Organization**: Large project structure
5. **Configuration Management**: Type-safe config validation
6. **HTTP Clients**: Real-world API integration patterns
7. **File I/O**: Streaming downloads, cleanup logic
8. **Testing**: Mock HTTP responses, integration tests

### Design Patterns

1. **Registry Pattern**: Pluggable downloader system
2. **Builder Pattern**: HTTP client configuration
3. **Strategy Pattern**: Different download strategies per source
4. **Factory Pattern**: Creating downloaders based on source type

## Migration Strategy

### Backward Compatibility

- Keep existing CLI commands working
- Maintain current config file format
- Gradual migration of functionality

### Testing Strategy

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use mockito::Server;

    #[tokio::test]
    async fn test_apod_downloader() {
        // Mock NASA API response
        // Test successful download
        // Test error handling
    }
}
```

### Risk Mitigation

- Implement one downloader at a time
- Keep existing wallpaper.rs as fallback during migration
- Extensive testing of each component
- Clear rollback plan if issues arise

## Success Criteria

### Technical Metrics

- [ ] All existing functionality preserved
- [ ] At least 5 new wallpaper sources implemented
- [ ] 90%+ test coverage for new modules
- [ ] Clean separation of concerns
- [ ] All unused config options implemented

### Educational Metrics

- [ ] Clear, documented module boundaries
- [ ] Comprehensive examples for each pattern
- [ ] Step-by-step implementation guides
- [ ] Error handling best practices demonstrated

This refactoring transforms wallflow from a simple wallpaper setter into a comprehensive, educational codebase demonstrating modern Rust patterns while maintaining the project's learning-focused mission.