digit-cli 0.3.0

A finger protocol client (RFC 1288 / RFC 742)
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
729
730
731
732
733
734
735
736
737
738
739
740
# digit v0.3.0 Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Add response size capping, raw output mode, shell completions, and comprehensive docs.rs documentation.

**Architecture:** Four independent features modifying existing files. `finger()` is refactored to call a new `finger_raw()` that returns `Vec<u8>` and accepts `max_response_size`. `finger()` wraps it with lossy UTF-8 decode. CLI gains `--max-size`, `--raw` flags, and a `completions` subcommand. Documentation added incrementally.

**Tech Stack:** Rust 2021, clap (derive), clap_complete, thiserror, std::io::Read::take.

---

### Task 1: Refactor finger() to finger_raw() -- Tests (Red)

**Files:**
- Modify: `src/protocol.rs`
- Modify: `tests/integration.rs`

This task adds `finger_raw()`, changes `finger()` to call it, and updates all callers to pass `max_response_size`. The refactor must happen atomically since the `finger()` signature changes.

- [ ] **Step 1: Add `finger_raw` function signature with `todo!()` and update `finger` signature**

In `src/protocol.rs`, add `finger_raw` before the existing `finger` function:

```rust
/// Execute a finger query over TCP, returning raw bytes.
///
/// Like [`finger`], but returns the raw response bytes without UTF-8 decoding.
/// The response is capped at `max_response_size` bytes.
pub fn finger_raw(
    query: &Query,
    timeout: Duration,
    max_response_size: u64,
) -> Result<Vec<u8>, FingerError> {
    todo!()
}
```

Change the existing `finger` function signature from:

```rust
pub fn finger(query: &Query, timeout: Duration) -> Result<String, FingerError> {
```

to:

```rust
/// Execute a finger query over TCP, returning the response as a string.
///
/// Calls [`finger_raw`] and decodes the response with [`String::from_utf8_lossy`].
/// Invalid UTF-8 bytes are replaced with U+FFFD.
pub fn finger(query: &Query, timeout: Duration, max_response_size: u64) -> Result<String, FingerError> {
```

Do NOT change the body of `finger` yet -- just the signature.

- [ ] **Step 2: Update all integration test calls to pass max_response_size**

In `tests/integration.rs`, update the import line:

```rust
use digit::protocol::{finger, finger_raw};
```

Update every `finger(&q, ...)` call to pass a large max size. There are 5 calls plus the timeout test. Change each:

```rust
    let result = finger(&q, Duration::from_secs(5));
```

to:

```rust
    let result = finger(&q, Duration::from_secs(5), 1_048_576);
```

The calls to update are in: `read_timeout_reports_timed_out` (line 28, uses `Duration::from_secs(1)`), `end_to_end_user_query` (line 92), `end_to_end_list_users` (line 107), `end_to_end_verbose_query` (line 122), `connection_refused_returns_error` (line 137, uses `Duration::from_secs(2)`), `utf8_lossy_handles_invalid_bytes` (line 158).

- [ ] **Step 3: Add integration tests for size cap and raw mode**

Add these tests to `tests/integration.rs`:

```rust
#[test]
fn response_capped_at_max_size() {
    // Server sends 1000 bytes, but we cap at 100.
    let big_response = "X".repeat(1000);
    let (port, handle) = mock_finger_server(&big_response, |_| {});

    let q = Query::parse(Some(&format!("user@127.0.0.1")), false, port).expect("valid query");
    let result = finger(&q, Duration::from_secs(5), 100).expect("finger should succeed");

    assert_eq!(result.len(), 100);
    assert!(result.chars().all(|c| c == 'X'));

    handle.join().expect("server thread");
}

#[test]
fn finger_raw_returns_bytes() {
    let (port, handle) = mock_finger_server("Hello\r\n", |_| {});

    let q = Query::parse(Some(&format!("user@127.0.0.1")), false, port).expect("valid query");
    let result = finger_raw(&q, Duration::from_secs(5), 1_048_576).expect("finger_raw should succeed");

    assert_eq!(result, b"Hello\r\n");

    handle.join().expect("server thread");
}

#[test]
fn finger_raw_preserves_invalid_utf8() {
    let response_bytes: Vec<u8> = vec![72, 101, 108, 108, 111, 0xFF, 0xFE, 10];
    let response_str = unsafe { String::from_utf8_unchecked(response_bytes.clone()) };

    let (port, handle) = mock_finger_server(&response_str, |_| {});

    let q = Query::parse(Some(&format!("user@127.0.0.1")), false, port).expect("valid query");
    let result = finger_raw(&q, Duration::from_secs(5), 1_048_576).expect("finger_raw should succeed");

    // Raw mode preserves the original bytes without replacement.
    assert_eq!(result, response_bytes);

    handle.join().expect("server thread");
}
```

- [ ] **Step 4: Update main.rs to pass max_response_size**

In `src/main.rs`, update the `finger` call to pass `1_048_576` as a temporary hardcoded value:

```rust
    match finger(&query, timeout, 1_048_576) {
```

- [ ] **Step 5: Verify new tests fail, old tests compile**

Run: `cargo test`
Expected: Existing tests pass (finger() compiles with new parameter). The 3 new tests fail: `response_capped_at_max_size` fails because finger() doesn't cap yet, `finger_raw_returns_bytes` and `finger_raw_preserves_invalid_utf8` fail because `finger_raw` has `todo!()`.

- [ ] **Step 6: Commit**

```bash
git add src/protocol.rs src/main.rs tests/integration.rs
git commit -m "test: add finger_raw and response size cap tests (red)"
```

---

### Task 2: Implement finger_raw() and Response Size Cap (Green)

**Files:**
- Modify: `src/protocol.rs`

- [ ] **Step 1: Move the finger() body into finger_raw()**

Replace the `todo!()` in `finger_raw` with the full body of the current `finger()` function, but with two changes:

1. Replace the read section. Change:

```rust
    let mut buf = Vec::new();
    stream.read_to_end(&mut buf).map_err(|e| {
```

to:

```rust
    let mut buf = Vec::new();
    stream.take(max_response_size).read_to_end(&mut buf).map_err(|e| {
```

2. Return `Vec<u8>` instead of `String`. Change the final line from:

```rust
    Ok(String::from_utf8_lossy(&buf).into_owned())
```

to:

```rust
    Ok(buf)
```

- [ ] **Step 2: Replace finger() body with delegation to finger_raw()**

Replace the entire body of `finger()` with:

```rust
pub fn finger(query: &Query, timeout: Duration, max_response_size: u64) -> Result<String, FingerError> {
    let bytes = finger_raw(query, timeout, max_response_size)?;
    Ok(String::from_utf8_lossy(&bytes).into_owned())
}
```

- [ ] **Step 3: Run all tests**

Run: `cargo test`
Expected: All tests pass, including the 3 new ones (size cap, raw bytes, raw preserves invalid UTF-8).

- [ ] **Step 4: Commit**

```bash
git add src/protocol.rs
git commit -m "feat: add finger_raw() and response size capping (green)"
```

---

### Task 3: Add --max-size and --raw CLI Flags

**Files:**
- Modify: `src/main.rs`

- [ ] **Step 1: Add the CLI flags and update main logic**

In `src/main.rs`, add to the imports:

```rust
use std::io::Write as _;
use digit::protocol::{finger, finger_raw};
```

Remove `use digit::protocol::finger;` (replaced by the new import).

Add two fields to the `Cli` struct:

```rust
    /// Maximum response size in bytes
    #[arg(long, default_value_t = 1_048_576)]
    max_size: u64,

    /// Write raw response bytes to stdout without UTF-8 decoding
    #[arg(long)]
    raw: bool,
```

Replace the finger call and response handling section (the `match finger(...)` block) with:

```rust
    if cli.raw {
        match finger_raw(&query, timeout, cli.max_size) {
            Ok(bytes) => {
                if bytes.len() as u64 >= cli.max_size {
                    eprintln!(
                        "digit: warning: response truncated at {} bytes (use --max-size to increase)",
                        cli.max_size
                    );
                }
                std::io::stdout()
                    .write_all(&bytes)
                    .expect("failed to write to stdout");
                ExitCode::SUCCESS
            }
            Err(e) => {
                eprintln!("digit: {}", e);
                ExitCode::FAILURE
            }
        }
    } else {
        match finger(&query, timeout, cli.max_size) {
            Ok(response) => {
                if response.len() as u64 >= cli.max_size {
                    eprintln!(
                        "digit: warning: response truncated at {} bytes (use --max-size to increase)",
                        cli.max_size
                    );
                }
                print!("{}", response);
                ExitCode::SUCCESS
            }
            Err(e) => {
                eprintln!("digit: {}", e);
                ExitCode::FAILURE
            }
        }
    }
```

Note: The truncation check uses `>=` because `take()` reads at most `max_response_size` bytes. If the response is exactly that length, it was likely truncated. For the `finger()` path (lossy decode), the string length in bytes might differ from the raw length due to multi-byte replacement characters, but the check is still a reasonable heuristic since the raw bytes are capped first.

- [ ] **Step 2: Verify it compiles and help text is correct**

Run: `cargo build`
Expected: Compiles successfully.

Run: `cargo run -- --help`
Expected: Shows `--max-size` with `[default: 1048576]` and `--raw` flags.

- [ ] **Step 3: Run all tests**

Run: `cargo test`
Expected: All tests pass.

- [ ] **Step 4: Commit**

```bash
git add src/main.rs
git commit -m "feat: add --max-size and --raw CLI flags"
```

---

### Task 4: Shell Completions Subcommand

**Files:**
- Modify: `Cargo.toml`
- Modify: `src/main.rs`

- [ ] **Step 1: Add clap_complete dependency**

In `Cargo.toml`, add to `[dependencies]`:

```toml
clap_complete = "4"
```

- [ ] **Step 2: Add the Command enum and subcommand support**

In `src/main.rs`, add to the imports:

```rust
use clap::Subcommand;
use clap_complete::aot::generate;
```

Add the `Command` enum before the `Cli` struct:

```rust
#[derive(Subcommand, Debug)]
enum Command {
    /// Generate shell completions for the given shell
    Completions {
        /// Shell to generate completions for (bash, zsh, fish, powershell, elvish)
        shell: clap_complete::Shell,
    },
}
```

Add a subcommand field to the `Cli` struct:

```rust
    #[command(subcommand)]
    command: Option<Command>,
```

- [ ] **Step 3: Handle the completions subcommand in main**

In the `main` function, add subcommand handling after `let cli = Cli::parse();` and before the `Query::parse` call:

```rust
    if let Some(Command::Completions { shell }) = cli.command {
        let mut cmd = Cli::command();
        generate(shell, &mut cmd, "digit", &mut std::io::stdout());
        return ExitCode::SUCCESS;
    }
```

Also add `use clap::CommandFactory;` to the imports at the top.

- [ ] **Step 4: Verify it compiles and generates completions**

Run: `cargo build`
Expected: Compiles successfully.

Run: `cargo run -- completions bash | head -5`
Expected: Outputs bash completion script starting with shell function definitions.

Run: `cargo run -- completions zsh | head -5`
Expected: Outputs zsh completion script.

- [ ] **Step 5: Verify normal finger queries still work**

Run: `cargo run -- --help`
Expected: Shows normal usage plus the `completions` subcommand.

Run: `cargo test`
Expected: All tests pass.

- [ ] **Step 6: Commit**

```bash
git add Cargo.toml Cargo.lock src/main.rs
git commit -m "feat: add shell completions subcommand via clap_complete"
```

---

### Task 5: Documentation -- Crate and Module Level

**Files:**
- Modify: `src/lib.rs`
- Modify: `src/query.rs`
- Modify: `src/protocol.rs`

- [ ] **Step 1: Add crate-level documentation to lib.rs**

Replace the contents of `src/lib.rs` with:

```rust
//! A finger protocol client library implementing
//! [RFC 1288](https://datatracker.ietf.org/doc/html/rfc1288) and
//! [RFC 742](https://datatracker.ietf.org/doc/html/rfc742).
//!
//! This crate provides:
//! - [`query::Query`] -- parsing finger query strings into structured queries
//! - [`protocol::build_query_string`] -- constructing RFC 1288 wire-format query strings
//! - [`protocol::finger`] -- executing a finger query over TCP (returns a UTF-8 string)
//! - [`protocol::finger_raw`] -- executing a finger query over TCP (returns raw bytes)
//!
//! # Example
//!
//! ```no_run
//! use std::time::Duration;
//! use digit::query::Query;
//! use digit::protocol::finger;
//!
//! let query = Query::parse(Some("user@example.com"), false, 79).unwrap();
//! let response = finger(&query, Duration::from_secs(10), 1_048_576).unwrap();
//! println!("{}", response);
//! ```

pub mod protocol;
pub mod query;
```

- [ ] **Step 2: Add module-level documentation to query.rs**

Add at the very top of `src/query.rs`, before the `Query` struct:

```rust
//! Finger query string parsing.
//!
//! Parses user input (e.g. `"user@host"`, `"@host"`, `"user@host1@host2"`)
//! into a structured [`Query`] for use with [`crate::protocol::finger`].

```

- [ ] **Step 3: Add a rustdoc example to Query::parse**

Update the doc comment on `Query::parse` in `src/query.rs`. Replace the existing doc comment (lines 30-38) with:

```rust
    /// Parse a query string into a [`Query`].
    ///
    /// Returns an error if any hostname segment is empty (e.g. `"user@"`, `"user@@host"`).
    ///
    /// # Examples
    ///
    /// ```
    /// use digit::query::Query;
    ///
    /// // Standard user query
    /// let q = Query::parse(Some("user@example.com"), false, 79).unwrap();
    /// assert_eq!(q.user, Some("user".to_string()));
    /// assert_eq!(q.target_host(), "example.com");
    ///
    /// // Empty hostname is rejected
    /// assert!(Query::parse(Some("user@"), false, 79).is_err());
    /// ```
```

- [ ] **Step 4: Add module-level documentation to protocol.rs**

Add at the very top of `src/protocol.rs`, before the imports:

```rust
//! Finger protocol wire format and TCP communication.
//!
//! Provides functions to build RFC 1288 query strings and execute finger
//! queries over TCP with configurable timeouts and response size limits.

```

- [ ] **Step 5: Add rustdoc example to build_query_string**

Update the doc comment on `build_query_string` in `src/protocol.rs`. Replace the existing doc comment with:

```rust
/// Build the wire-format query string to send over the TCP connection.
///
/// Per RFC 1288:
/// - Verbose queries prepend `/W ` (with trailing space).
/// - Forwarding appends `@host1@host2...` for all hosts except the last
///   (the last host is the connection target, not part of the query string).
/// - The query is terminated with `\r\n`.
///
/// # Example
///
/// ```
/// use digit::query::Query;
/// use digit::protocol::build_query_string;
///
/// let q = Query::parse(Some("user@host"), true, 79).unwrap();
/// assert_eq!(build_query_string(&q), "/W user\r\n");
/// ```
```

- [ ] **Step 6: Add rustdoc example to finger**

Update the doc comment on `finger` in `src/protocol.rs`:

```rust
/// Execute a finger query over TCP, returning the response as a string.
///
/// Calls [`finger_raw`] and decodes the response with [`String::from_utf8_lossy`].
/// Invalid UTF-8 bytes are replaced with U+FFFD. The response is capped at
/// `max_response_size` bytes.
///
/// # Example
///
/// ```no_run
/// use std::time::Duration;
/// use digit::query::Query;
/// use digit::protocol::finger;
///
/// let q = Query::parse(Some("user@example.com"), false, 79).unwrap();
/// let response = finger(&q, Duration::from_secs(10), 1_048_576).unwrap();
/// println!("{}", response);
/// ```
```

- [ ] **Step 7: Add rustdoc example to finger_raw**

Update the doc comment on `finger_raw` in `src/protocol.rs`:

```rust
/// Execute a finger query over TCP, returning raw bytes.
///
/// Like [`finger`], but returns the raw response bytes without UTF-8 decoding.
/// The response is capped at `max_response_size` bytes. Useful for piping
/// output to other tools or when the response contains non-UTF-8 data.
///
/// # Example
///
/// ```no_run
/// use std::time::Duration;
/// use digit::query::Query;
/// use digit::protocol::finger_raw;
///
/// let q = Query::parse(Some("user@example.com"), false, 79).unwrap();
/// let bytes = finger_raw(&q, Duration::from_secs(10), 1_048_576).unwrap();
/// std::io::Write::write_all(&mut std::io::stdout(), &bytes).unwrap();
/// ```
```

- [ ] **Step 8: Run doc tests**

Run: `cargo test --doc`
Expected: The compilable doc examples (`Query::parse`, `build_query_string`) pass. The `no_run` examples compile but don't execute.

- [ ] **Step 9: Run full test suite**

Run: `cargo test`
Expected: All tests pass.

- [ ] **Step 10: Commit**

```bash
git add src/lib.rs src/query.rs src/protocol.rs
git commit -m "docs: add crate, module, and function-level rustdoc documentation"
```

---

### Task 6: Update README

**Files:**
- Modify: `README.md`

- [ ] **Step 1: Update the README**

Replace the full contents of `README.md` with:

````markdown
# digit

[![crates.io](https://img.shields.io/crates/v/digit-cli.svg)](https://crates.io/crates/digit-cli)
[![docs.rs](https://docs.rs/digit-cli/badge.svg)](https://docs.rs/digit-cli)

A finger protocol client implementing [RFC 1288](https://datatracker.ietf.org/doc/html/rfc1288) and [RFC 742](https://datatracker.ietf.org/doc/html/rfc742), written in Rust.

To try this on active finger servers (as of 2026), try the domains `graph.no` and `tilde.town`

## Installation

```
cargo install digit-cli
```

Or from source:

```
cargo install --path .
```

## Usage

```
digit [OPTIONS] [QUERY]
digit completions <SHELL>
```

### Examples

```bash
# Query a user at a host
digit user@example.com

# List all users at a host
digit @example.com

# Query a user with verbose/long output
digit -l user@example.com

# Use a non-standard port
digit -p 7979 user@example.com

# Set a connection timeout (in seconds)
digit -t 5 user@example.com

# Write raw bytes to stdout (useful for piping)
digit --raw user@example.com | hexdump -C

# Limit response size to 64 KiB
digit --max-size 65536 user@example.com

# Query a user at localhost
digit user

# List users at localhost
digit
```

### Forwarding queries

The finger protocol supports forwarding queries through a chain of hosts using the `@host1@host2` syntax (RFC 1288, section 2.5.1). For example:

```bash
digit user@host1@host2
```

This connects to `host2` and asks it to forward the query to `host1`. Note that many modern finger servers disable forwarding for security reasons, so this feature depends on server support.

### Options

| Option | Description |
|---|---|
| `-l, --long` | Request verbose/long output (sends `/W` prefix) |
| `-p, --port <PORT>` | Port to connect on (default: 79) |
| `-t, --timeout <SECS>` | Connection timeout in seconds (default: 10) |
| `--max-size <BYTES>` | Maximum response size in bytes (default: 1048576) |
| `--raw` | Write raw response bytes to stdout without UTF-8 decoding |
| `-h, --help` | Print help |
| `-V, --version` | Print version |

### Shell completions

Generate shell completions with the `completions` subcommand:

```bash
# Bash
digit completions bash > ~/.bash_completion.d/digit

# Zsh
digit completions zsh > ~/.zfunc/_digit

# Fish
digit completions fish > ~/.config/fish/completions/digit.fish

# PowerShell
digit completions powershell > digit.ps1

# Elvish
digit completions elvish > digit.elv
```

## Library

`digit` can also be used as a Rust library. See the [API documentation](https://docs.rs/digit-cli) for details.

## License

MIT
````

- [ ] **Step 2: Commit**

```bash
git add README.md
git commit -m "docs: update README with new flags, completions, and docs.rs link"
```

---

### Task 7: Version Bump and Final Checks

**Files:**
- Modify: `Cargo.toml`

- [ ] **Step 1: Bump version to 0.3.0**

In `Cargo.toml`, change:

```toml
version = "0.2.0"
```

to:

```toml
version = "0.3.0"
```

- [ ] **Step 2: Run full test suite**

Run: `cargo test`
Expected: All tests pass.

- [ ] **Step 3: Run clippy**

Run: `cargo clippy -- -D warnings`
Expected: No warnings.

- [ ] **Step 4: Check formatting**

Run: `cargo fmt -- --check`
Expected: No formatting issues. If any, run `cargo fmt` to fix.

- [ ] **Step 5: Verify doc tests**

Run: `cargo test --doc`
Expected: All doc tests pass.

- [ ] **Step 6: Commit**

```bash
git add Cargo.toml Cargo.lock
git commit -m "chore: bump version to 0.3.0"
```