alfrusco 0.2.0

Utilities for building Alfred workflows with Rust.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# alfrusco

[![Crates.io](https://img.shields.io/crates/v/alfrusco.svg)](https://crates.io/crates/alfrusco)
[![Documentation](https://docs.rs/alfrusco/badge.svg)](https://docs.rs/alfrusco)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A powerful, ergonomic Rust library for building [Alfred](https://www.alfredapp.com/) workflows with ease. Alfrusco
handles the complexity of Alfred's JSON protocol, provides rich item building capabilities, and includes advanced
features like background jobs, clipboard operations, and comprehensive logging.

## Features

- **Simple & Ergonomic API** - Intuitive builder patterns for creating Alfred items
- **Async Support** - Full async/await support for modern Rust applications
- **Background Jobs** - Run long-running tasks without blocking Alfred's UI
- **Clipboard Integration** - Built-in support for rich text and Markdown clipboard operations
- **Smart Filtering** - Automatic fuzzy search and sorting of results
- **Workflow Management** - Easy access to workflow directories and configuration
- **Comprehensive Logging** - Structured logging with file and console output
- **URL Items** - Specialized support for URL-based workflow items
- **Environment Handling** - Robust configuration management for Alfred environments
- **Testing Support** - Built-in testing utilities and mocking capabilities

## 📦 Installation

Add alfrusco to your `Cargo.toml`:

```toml
[dependencies]
alfrusco = "0.2"

# For async workflows
tokio = { version = "1", features = ["full"] }

# For command-line argument parsing (recommended)
clap = { version = "4", features = ["derive", "env"] }
```

## 🚀 Quick Start

### Basic Synchronous Workflow

```rust
use alfrusco::{execute, Item, Runnable, Workflow};
use alfrusco::config::AlfredEnvProvider;
use clap::Parser;

#[derive(Parser)]
struct MyWorkflow {
    query: Vec<String>,
}

impl Runnable for MyWorkflow {
    type Error = alfrusco::Error;

    fn run(self, workflow: &mut Workflow) -> Result<(), Self::Error> {
        let query = self.query.join(" ");

        workflow.append_item(
            Item::new(format!("Hello, {}!", query))
                .subtitle("This is a basic Alfred workflow")
                .arg(&query)
                .valid(true)
        );

        Ok(())
    }
}

fn main() {
    // Initialize logging (optional but recommended)
    let _ = alfrusco::init_logging(&AlfredEnvProvider);

    // Parse command line arguments and execute workflow
    let command = MyWorkflow::parse();
    execute(&AlfredEnvProvider, command, &mut std::io::stdout());
}
```

### Async Workflow with HTTP Requests

```rust
use alfrusco::{execute_async, AsyncRunnable, Item, Workflow, WorkflowError};
use alfrusco::config::AlfredEnvProvider;
use clap::Parser;
use serde::Deserialize;

#[derive(Parser)]
struct ApiWorkflow {
    query: Vec<String>,
}

#[derive(Deserialize)]
struct ApiResponse {
    results: Vec<ApiResult>,
}

#[derive(Deserialize)]
struct ApiResult {
    title: String,
    description: String,
    url: String,
}

#[async_trait::async_trait]
impl AsyncRunnable for ApiWorkflow {
    type Error = Box<dyn WorkflowError>;

    async fn run_async(self, workflow: &mut Workflow) -> Result<(), Self::Error> {
        let query = self.query.join(" ");
        workflow.set_filter_keyword(query.clone());

        let url = format!("https://api.example.com/search?q={}", query);
        let response: ApiResponse = reqwest::get(&url)
            .await?
            .json()
            .await?;

        let items: Vec<Item> = response.results
            .into_iter()
            .map(|result| {
                Item::new(&result.title)
                    .subtitle(&result.description)
                    .arg(&result.url)
                    .quicklook_url(&result.url)
                    .valid(true)
            })
            .collect();

        workflow.append_items(items);
        Ok(())
    }
}

#[tokio::main]
async fn main() {
    let _ = alfrusco::init_logging(&AlfredEnvProvider);
    let command = ApiWorkflow::parse();
    execute_async(&AlfredEnvProvider, command, &mut std::io::stdout()).await;
}
```

## 🏗️ Core Concepts

### Items

Items are the building blocks of Alfred workflows. Each item represents a choice in the Alfred selection UI:

```rust
use alfrusco::Item;

let item = Item::new("My Title")
.subtitle("Additional information")
.arg("argument-passed-to-action")
.uid("unique-identifier")
.valid(true)
.icon_from_image("/path/to/icon.png")
.copy_text("Text copied with ⌘C")
.large_type_text("Text shown in large type with ⌘L")
.quicklook_url("https://example.com")
.var("CUSTOM_VAR", "value")
.autocomplete("text for tab completion");
```

### Workflow Configuration

Alfrusco automatically handles Alfred's environment variables through configuration providers:

```rust
use alfrusco::config::{AlfredEnvProvider, TestingProvider};

// For production (reads from Alfred environment variables)
let provider = AlfredEnvProvider;

// For testing (uses temporary directories)
let temp_dir = tempfile::tempdir().unwrap();
let provider = TestingProvider(temp_dir.path().to_path_buf());
```

### Error Handling

Implement custom error types that work seamlessly with Alfred:

```rust
use alfrusco::{WorkflowError, Item};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum MyWorkflowError {
    #[error("Network request failed: {0}")]
    Network(#[from] reqwest::Error),
    #[error("Invalid input: {0}")]
    InvalidInput(String),
}

impl WorkflowError for MyWorkflowError {}

// Errors automatically become Alfred items
impl Runnable for MyWorkflow {
    type Error = MyWorkflowError;

    fn run(self, workflow: &mut Workflow) -> Result<(), Self::Error> {
        // If this returns an error, Alfred will show it as an item
        Err(MyWorkflowError::InvalidInput("Missing required field".to_string()))
    }
}
```

## 🔧 Advanced Features

### Background Jobs

Run long-running tasks without blocking Alfred's UI. This example fetches GitHub release data in the background and
caches it to disk, showing cached results immediately while refreshing stale data:

```rust
use std::process::Command;
use std::time::Duration;

impl Runnable for MyWorkflow {
    type Error = alfrusco::Error;

    fn run(self, workflow: &mut Workflow) -> Result<(), Self::Error> {
        let cache_file = workflow.cache_dir().join("releases.json");

        // Set up a command to fetch data and save to cache
        let mut cmd = Command::new("sh");
        cmd.arg("-c")
            .arg(format!(
                "curl -s https://api.github.com/repos/rust-lang/rust/releases/latest > {}",
                cache_file.display()
            ));

        // Run the command in the background, refresh every 30 seconds
        workflow.run_in_background(
            "github-releases",
            Duration::from_secs(30),
            cmd
        );

        // Check if we have cached data to display
        if cache_file.exists() {
            if let Ok(data) = std::fs::read_to_string(&cache_file) {
                if let Ok(release) = serde_json::from_str::<serde_json::Value>(&data) {
                    if let Some(tag) = release["tag_name"].as_str() {
                        workflow.append_item(
                            Item::new(format!("Latest Rust: {}", tag))
                                .subtitle("Click to view release notes")
                                .arg(release["html_url"].as_str().unwrap_or(""))
                                .valid(true)
                        );
                    }
                }
            }
        }

        // run_in_background automatically shows a status item when the job is stale

        Ok(())
    }
}
```

**Enhanced Background Job Features:**

- **Smart Status Tracking**: Jobs show detailed status messages like "Last succeeded 2 minutes ago (14:32:15), running
  for 3s" or "Last failed 5 minutes ago (14:29:42), running for 1s"
- **Automatic Retry Logic**: Failed jobs are automatically retried even if they ran recently, ensuring eventual success
- **Context-Aware Icons**: Visual indicators show job status at a glance:
    - ✅ Success jobs show completion icon
    - ❌ Failed jobs show error icon
    - 🔄 Retry attempts show sync icon
    - 🕐 First-time runs show clock icon
- **Secure Shell Escaping**: Arguments with spaces and special characters are properly escaped for security
- **Robust Last-Run Tracking**: All job executions are tracked regardless of success/failure for accurate status
  reporting

**Background Job Status Messages:**

When a background job is running, Alfred will display informative status items:

```
Background Job 'github-releases'
Last succeeded 2 minutes ago (14:32:15), running for 3s
```

This gives users clear visibility into:

- When the job last ran successfully or failed
- The exact time of the last execution
- How long the current execution has been running
- Visual context through appropriate icons

### URL Items with Rich Clipboard Support

Create URL items with automatic clipboard integration:

```rust
use alfrusco::URLItem;

let url_item = URLItem::new("Rust Documentation", "https://doc.rust-lang.org/")
    .subtitle("The Rust Programming Language Documentation")
    .short_title("Rust Docs")  // Used in Cmd+Shift modifier
    .long_title("The Rust Programming Language Official Documentation")  // Used in Cmd+Ctrl modifier
    .icon_for_filetype("public.html")
    .copy_text("doc.rust-lang.org");

// Convert to regular Item (happens automatically when added to workflow)
let item: Item = url_item.into();
```

URL items automatically include modifiers for copying links:

- **⌘ (Cmd)**: Copy as Markdown link `[title](url)`
- **⌥ (Alt)**: Copy as rich text link (HTML)
- **⌘⇧ (Cmd+Shift)**: Copy as Markdown with short title
- **⌥⇧ (Alt+Shift)**: Copy as rich text with short title
- **⌘⌃ (Cmd+Ctrl)**: Copy as Markdown with long title
- **⌥⌃ (Alt+Ctrl)**: Copy as rich text with long title

### Smart Filtering and Sorting

Enable automatic fuzzy search and sorting of results:

```rust
impl Runnable for SearchWorkflow {
    type Error = alfrusco::Error;

    fn run(self, workflow: &mut Workflow) -> Result<(), Self::Error> {
        let query = self.query.join(" ");

        // Enable filtering - results will be automatically filtered and sorted
        workflow.set_filter_keyword(query);

        // Add items - they'll be filtered based on the query
        workflow.append_items(vec![
            Item::new("Apple").subtitle("Fruit"),
            Item::new("Banana").subtitle("Yellow fruit"),
            Item::new("Carrot").subtitle("Orange vegetable"),
        ]);

        Ok(())
    }
}
```

### Workflow Directories

Access workflow-specific data and cache directories:

```rust
impl Runnable for MyWorkflow {
    type Error = alfrusco::Error;

    fn run(self, workflow: &mut Workflow) -> Result<(), Self::Error> {
        // Access workflow data directory (persistent storage)
        let data_dir = workflow.data_dir();
        let config_file = data_dir.join("config.json");

        // Access workflow cache directory (temporary storage)
        let cache_dir = workflow.cache_dir();
        let temp_file = cache_dir.join("temp_data.json");

        // Use directories for file operations
        std::fs::write(config_file, "{\"setting\": \"value\"}")?;

        Ok(())
    }
}
```

### Response Caching and Rerun

Control Alfred's caching behavior and automatic refresh:

```rust
use std::time::Duration;

impl Runnable for MyWorkflow {
    type Error = alfrusco::Error;

    fn run(self, workflow: &mut Workflow) -> Result<(), Self::Error> {
        // Cache results for 5 minutes, allow loose reload
        workflow.cache(Duration::from_secs(300), true);

        // Automatically rerun every 30 seconds
        workflow.rerun(Duration::from_secs(30));

        // Skip Alfred's knowledge base integration
        workflow.skip_knowledge(true);

        workflow.append_item(Item::new("Cached result"));
        Ok(())
    }
}
```

## 🧪 Testing

Alfrusco provides comprehensive testing support with shared utilities and organized test structure:

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use alfrusco::config::TestingProvider;
    use tempfile::tempdir;

    #[test]
    fn test_my_workflow() {
        let workflow = MyWorkflow {
            query: vec!["test".to_string()],
        };

        // Use TestingProvider for isolated testing
        let temp_dir = tempdir().unwrap();
        let provider = TestingProvider(temp_dir.path().to_path_buf());

        let mut buffer = Vec::new();
        alfrusco::execute(&provider, workflow, &mut buffer);

        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("Hello, test!"));
    }

    #[tokio::test]
    async fn test_async_workflow() {
        let workflow = AsyncWorkflow {
            query: vec!["async".to_string()],
        };

        let temp_dir = tempdir().unwrap();
        let provider = TestingProvider(temp_dir.path().to_path_buf());

        let mut buffer = Vec::new();
        alfrusco::execute_async(&provider, workflow, &mut buffer).await;

        let output = String::from_utf8(buffer).unwrap();
        assert!(output.contains("async"));
    }
}
```

### ### Test Organization

Alfrusco maintains a comprehensive test suite with **112 tests** across organized test files:

- **`background_job_integration_tests.rs`** - Complete background job lifecycle testing (6 tests)
- **`clipboard_tests.rs`** - Clipboard functionality testing (4 tests)
- **`config_tests.rs`** - Configuration and environment testing (8 tests)
- **`error_injection_tests.rs`** - Error handling and edge cases (2 tests)
- **`error_tests.rs`** - Error type behavior (7 tests)
- **`logging_tests.rs`** - Logging functionality (1 test)
- **`runnable_tests.rs`** - Trait implementation testing (4 tests)
- **`tests/common/mod.rs`** - Shared test utilities and helpers

### Shared Test Utilities

The `tests/common/mod.rs` module provides reusable testing utilities that eliminate code duplication and ensure
consistent test setup across the entire test suite. This includes helper functions for creating test workflows, managing
temporary directories, and common test operations.

```

## 📚 Examples

The `examples/` directory contains complete, runnable examples. Since these examples use `AlfredEnvProvider`, they require Alfred environment variables to be set. We provide a convenient script to run them with mock environment variables:

### Running Examples

**Option 1: Using the run script (recommended)**
```bash
# Basic static output
./run-example.sh static_output

# Success workflow with custom message  
./run-example.sh success --message "Custom message"

# Async API example
./run-example.sh random_user search_term

# URL items demonstration
./run-example.sh url_items

# Background job example
./run-example.sh sleep --duration-in-seconds 10

# Error handling example
./run-example.sh error --file-path nonexistent.txt
```

**Option 2: Using Make targets**

```bash
# List all available examples
make examples-help

# Run specific examples
make example-static_output
make example-success
make example-url_items
```

**Option 3: Manual environment setup**

```bash
# Set required Alfred environment variables
export alfred_workflow_bundleid="com.example.test"
export alfred_workflow_cache="/tmp/cache"
export alfred_workflow_data="/tmp/data"
export alfred_version="5.0"
export alfred_version_build="2058"
export alfred_workflow_name="Test Workflow"

# Then run normally
cargo run --example static_output
```

### Example Descriptions

- **static_output** - Basic workflow that returns static items without user input
- **success** - Simple workflow demonstrating command-line argument parsing
- **random_user** - Async workflow that fetches data from an external API with fuzzy filtering
- **url_items** - Demonstrates URL items with automatic clipboard integration and modifiers
- **sleep** - Shows background job execution with status monitoring
- **error** - Demonstrates custom error types and error item generation
- **async_success** - Basic async workflow example
- **async_error** - Async workflow with error handling examples

## 📖 API Reference

### Core Types

#### `Item`

The primary building block for Alfred workflow results.

**Key Methods:**

- `new(title)` - Create a new item with a title
- `subtitle(text)` - Set subtitle text
- `arg(value)` / `args(values)` - Set arguments passed to actions
- `valid(bool)` - Set whether the item is actionable
- `uid(id)` - Set unique identifier for Alfred's learning
- `icon_from_image(path)` / `icon_for_filetype(type)` - Set item icons
- `copy_text(text)` / `large_type_text(text)` - Set text operations
- `quicklook_url(url)` - Enable Quick Look preview
- `var(key, value)` - Set workflow variables
- `autocomplete(text)` - Set tab completion text
- `modifier(modifier)` - Add keyboard modifier actions

#### `URLItem`

Specialized item type for URLs with automatic clipboard integration.

**Key Methods:**

- `new(title, url)` - Create a URL item
- `subtitle(text)` - Override default URL subtitle
- `short_title(text)` / `long_title(text)` - Set alternative titles for modifiers
- `display_title(text)` - Override displayed title while preserving link title
- `copy_text(text)` - Set custom copy text
- `icon_from_image(path)` / `icon_for_filetype(type)` - Set icons

#### `Workflow`

Main workflow execution context.

**Key Methods:**

- `append_item(item)` / `append_items(items)` - Add items to results
- `prepend_item(item)` / `prepend_items(items)` - Add items to beginning
- `set_filter_keyword(query)` - Enable fuzzy filtering
- `data_dir()` / `cache_dir()` - Access workflow directories
- `run_in_background(name, max_age, command)` - Execute background jobs

#### `Response`

Controls Alfred's response behavior.

**Key Methods:**

- `cache(duration, loose_reload)` - Set caching behavior
- `rerun(interval)` - Set automatic refresh interval
- `skip_knowledge(bool)` - Control Alfred's knowledge integration

### Traits

#### `Runnable`

For synchronous workflows.

```rust
trait Runnable {
    type Error: WorkflowError;
    fn run(self, workflow: &mut Workflow) -> Result<(), Self::Error>;
}
```

#### `AsyncRunnable`

For asynchronous workflows.

```rust
#[async_trait]
trait AsyncRunnable {
    type Error: WorkflowError;
    async fn run_async(self, workflow: &mut Workflow) -> Result<(), Self::Error>;
}
```

#### `WorkflowError`

For custom error types that integrate with Alfred.

```rust
trait WorkflowError: std::error::Error {
    fn error_item(&self) -> Item { /* default implementation */ }
}
```

### Configuration

#### `AlfredEnvProvider`

Production configuration provider that reads from Alfred environment variables.

#### `TestingProvider`

Testing configuration provider that uses temporary directories.

### Execution Functions

- `execute(provider, runnable, writer)` - Execute synchronous workflow
- `execute_async(provider, runnable, writer)` - Execute asynchronous workflow
- `init_logging(provider)` - Initialize structured logging## 🛠️ Development

### Building from Source

```bash
git clone https://github.com/adlio/alfrusco.git
cd alfrusco
cargo build
```

### Running Tests

```bash
# Run all tests
cargo test

# Run tests with nextest (recommended)
cargo nextest run

# Run tests serially (for debugging flaky tests)
make test-serial

# Run with coverage
cargo tarpaulin --out html
```

### Running Examples

Examples require Alfred environment variables. Use the provided script:

```bash
# Basic static output
./run-example.sh static_output

# Success workflow with custom message
./run-example.sh success --message "Hello World"

# Async API example
./run-example.sh random_user john

# URL items demonstration
./run-example.sh url_items

# Background job example
./run-example.sh sleep --duration-in-seconds 5

# Error handling example
./run-example.sh error --file-path /nonexistent/file.txt

# Or use Make targets
make example-static_output
make examples-help  # See all available examples
```

## 🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to
discuss what you would like to change.

### Development Setup

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes
4. Add tests for your changes
5. Ensure all tests pass (`cargo nextest run`)
6. Run clippy (`cargo clippy`)
7. Format your code (`cargo fmt`)
8. Commit your changes (`git commit -m 'Add some amazing feature'`)
9. Push to the branch (`git push origin feature/amazing-feature`)
10. Open a Pull Request

### Code Style

- Follow Rust standard formatting (`cargo fmt`)
- Ensure clippy passes without warnings (`cargo clippy`)
- Add documentation for public APIs
- Include tests for new functionality
- Update examples if adding new features

## 📄 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 📞 Support

- 📖 [Documentation]https://docs.rs/alfrusco
- 🐛 [Issue Tracker]https://github.com/adlio/alfrusco/issues
- 💬 [Discussions]https://github.com/adlio/alfrusco/discussions

---

Made with ❤️ for the Alfred and Rust communities.