digit-cli 0.2.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
# digit 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:** Build a Rust CLI finger client implementing RFC 1288 and RFC 742 with clear code, documentation, and thorough tests.

**Architecture:** Library + binary split. `src/query.rs` handles input parsing into a `Query` struct. `src/protocol.rs` builds the wire-format string and performs TCP I/O. `src/main.rs` is a thin CLI entry point using clap. Error handling uses `thiserror`.

**Tech Stack:** Rust 2021 edition, clap (derive), thiserror, std::net for TCP.

---

### Task 1: Project Scaffolding

**Files:**
- Create: `Cargo.toml`
- Create: `LICENSE`
- Create: `src/main.rs` (placeholder)
- Create: `src/lib.rs` (placeholder)

- [ ] **Step 1: Initialize the Cargo project**

Run: `cargo init --name digit`

This creates `Cargo.toml` and `src/main.rs`.

- [ ] **Step 2: Set up Cargo.toml with dependencies and metadata**

Edit `Cargo.toml` to contain:

```toml
[package]
name = "digit"
version = "0.1.0"
edition = "2021"
description = "A finger protocol client (RFC 1288 / RFC 742)"
license = "MIT"

[dependencies]
clap = { version = "4", features = ["derive"] }
thiserror = "2"
```

- [ ] **Step 3: Create the MIT LICENSE file**

Create `LICENSE` with the MIT license text, copyright 2026.

- [ ] **Step 4: Create src/lib.rs with module declarations**

Create `src/lib.rs`:

```rust
pub mod protocol;
pub mod query;
```

- [ ] **Step 5: Create placeholder module files**

Create `src/query.rs`:

```rust
// Finger query parsing.
```

Create `src/protocol.rs`:

```rust
// Finger protocol wire format and TCP communication.
```

- [ ] **Step 6: Replace src/main.rs placeholder**

Replace `src/main.rs` with:

```rust
fn main() {
    println!("digit - finger client");
}
```

- [ ] **Step 7: Verify the project compiles**

Run: `cargo build`
Expected: Compiles successfully with no errors.

- [ ] **Step 8: Commit**

```bash
git add Cargo.toml Cargo.lock LICENSE src/
git commit -m "feat: scaffold project with dependencies and module structure"
```

---

### Task 2: Query Parsing -- Struct and Tests

**Files:**
- Create: `src/query.rs`
- Test: `src/query.rs` (inline `#[cfg(test)]` module)

- [ ] **Step 1: Write failing tests for Query parsing**

Write the full contents of `src/query.rs`:

```rust
/// A parsed finger query.
///
/// Represents the structured result of parsing a finger query string
/// like `"user@host"`, `"@host"`, or `"user@host1@host2"`.
#[derive(Debug, Clone, PartialEq)]
pub struct Query {
    /// The user to query. `None` means list all users.
    pub user: Option<String>,
    /// The host(s) to query. The last host is the connection target.
    /// Multiple hosts indicate a forwarding chain (RFC 1288).
    pub hosts: Vec<String>,
    /// Whether to request verbose output (sends `/W` prefix).
    pub long: bool,
    /// TCP port to connect on. Default is 79.
    pub port: u16,
}

/// The default finger protocol port.
pub const DEFAULT_PORT: u16 = 79;

impl Query {
    /// Parse a query string into a `Query`.
    ///
    /// # Examples
    ///
    /// - `"user@host"` -> user=Some("user"), hosts=["host"]
    /// - `"@host"` -> user=None, hosts=["host"]
    /// - `"user@host1@host2"` -> user=Some("user"), hosts=["host1", "host2"]
    /// - `"user"` -> user=Some("user"), hosts=["localhost"]
    /// - `""` or `None` -> user=None, hosts=["localhost"]
    pub fn parse(input: Option<&str>, long: bool, port: u16) -> Query {
        todo!()
    }

    /// Returns the host to connect to (the last host in the chain).
    pub fn target_host(&self) -> &str {
        todo!()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_user_at_host() {
        let q = Query::parse(Some("user@host"), false, 79);
        assert_eq!(q.user, Some("user".to_string()));
        assert_eq!(q.hosts, vec!["host".to_string()]);
        assert!(!q.long);
        assert_eq!(q.port, 79);
    }

    #[test]
    fn parse_at_host_lists_users() {
        let q = Query::parse(Some("@host"), false, 79);
        assert_eq!(q.user, None);
        assert_eq!(q.hosts, vec!["host".to_string()]);
    }

    #[test]
    fn parse_user_only_defaults_to_localhost() {
        let q = Query::parse(Some("user"), false, 79);
        assert_eq!(q.user, Some("user".to_string()));
        assert_eq!(q.hosts, vec!["localhost".to_string()]);
    }

    #[test]
    fn parse_empty_string_defaults_to_localhost() {
        let q = Query::parse(Some(""), false, 79);
        assert_eq!(q.user, None);
        assert_eq!(q.hosts, vec!["localhost".to_string()]);
    }

    #[test]
    fn parse_none_defaults_to_localhost() {
        let q = Query::parse(None, false, 79);
        assert_eq!(q.user, None);
        assert_eq!(q.hosts, vec!["localhost".to_string()]);
    }

    #[test]
    fn parse_forwarding_chain() {
        let q = Query::parse(Some("user@host1@host2"), false, 79);
        assert_eq!(q.user, Some("user".to_string()));
        assert_eq!(q.hosts, vec!["host1".to_string(), "host2".to_string()]);
    }

    #[test]
    fn parse_forwarding_chain_no_user() {
        let q = Query::parse(Some("@host1@host2"), false, 79);
        assert_eq!(q.user, None);
        assert_eq!(q.hosts, vec!["host1".to_string(), "host2".to_string()]);
    }

    #[test]
    fn parse_long_flag_preserved() {
        let q = Query::parse(Some("user@host"), true, 79);
        assert!(q.long);
    }

    #[test]
    fn parse_custom_port_preserved() {
        let q = Query::parse(Some("user@host"), false, 7979);
        assert_eq!(q.port, 7979);
    }

    #[test]
    fn target_host_returns_last_host() {
        let q = Query::parse(Some("user@host1@host2"), false, 79);
        assert_eq!(q.target_host(), "host2");
    }

    #[test]
    fn target_host_single_host() {
        let q = Query::parse(Some("user@host"), false, 79);
        assert_eq!(q.target_host(), "host");
    }

    #[test]
    fn parse_three_host_chain() {
        let q = Query::parse(Some("user@a@b@c"), false, 79);
        assert_eq!(q.user, Some("user".to_string()));
        assert_eq!(
            q.hosts,
            vec!["a".to_string(), "b".to_string(), "c".to_string()]
        );
        assert_eq!(q.target_host(), "c");
    }
}
```

- [ ] **Step 2: Run tests to verify they fail**

Run: `cargo test --lib query`
Expected: FAIL -- all tests fail with "not yet implemented" panics.

- [ ] **Step 3: Commit the failing tests**

```bash
git add src/query.rs
git commit -m "test: add query parsing tests (red)"
```

---

### Task 3: Query Parsing -- Implementation

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

- [ ] **Step 1: Implement Query::parse and Query::target_host**

Replace the two `todo!()` bodies in `src/query.rs`:

Replace `Query::parse`:

```rust
    pub fn parse(input: Option<&str>, long: bool, port: u16) -> Query {
        let input = input.unwrap_or("");

        if input.is_empty() {
            return Query {
                user: None,
                hosts: vec!["localhost".to_string()],
                long,
                port,
            };
        }

        // Split on '@'. First part is the user (if non-empty), rest are hosts.
        let parts: Vec<&str> = input.splitn(2, '@').collect();

        if parts.len() == 1 {
            // No '@' found -- user only, default to localhost.
            return Query {
                user: Some(parts[0].to_string()),
                hosts: vec!["localhost".to_string()],
                long,
                port,
            };
        }

        // Has at least one '@'.
        let user = if parts[0].is_empty() {
            None
        } else {
            Some(parts[0].to_string())
        };

        let hosts: Vec<String> = parts[1].split('@').map(|s| s.to_string()).collect();

        Query {
            user,
            hosts,
            long,
            port,
        }
    }
```

Replace `Query::target_host`:

```rust
    pub fn target_host(&self) -> &str {
        self.hosts.last().expect("hosts must not be empty")
    }
```

- [ ] **Step 2: Run tests to verify they pass**

Run: `cargo test --lib query`
Expected: All 13 tests pass.

- [ ] **Step 3: Commit**

```bash
git add src/query.rs
git commit -m "feat: implement query parsing (green)"
```

---

### Task 4: Protocol -- Wire Format and Tests

**Files:**
- Create: `src/protocol.rs`
- Test: `src/protocol.rs` (inline `#[cfg(test)]` module)

- [ ] **Step 1: Write the error type, wire format builder, and failing tests**

Write the full contents of `src/protocol.rs`:

```rust
use std::io;

use crate::query::Query;

/// Errors that can occur during a finger protocol exchange.
#[derive(Debug, thiserror::Error)]
pub enum FingerError {
    /// Failed to resolve the hostname.
    #[error("could not resolve host '{host}': {source}")]
    DnsResolution {
        host: String,
        #[source]
        source: io::Error,
    },

    /// Failed to connect to the remote host.
    #[error("could not connect to {host}:{port}: {source}")]
    ConnectionFailed {
        host: String,
        port: u16,
        #[source]
        source: io::Error,
    },

    /// Connection timed out.
    #[error("connection to {host}:{port} timed out")]
    Timeout { host: String, port: u16 },

    /// Failed to send the query.
    #[error("failed to send query: {source}")]
    SendFailed {
        #[source]
        source: io::Error,
    },

    /// Failed to read the response.
    #[error("failed to read response: {source}")]
    ReadFailed {
        #[source]
        source: io::Error,
    },
}

/// 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`.
pub fn build_query_string(query: &Query) -> String {
    todo!()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::query::Query;

    #[test]
    fn query_string_user_at_host() {
        let q = Query::parse(Some("user@host"), false, 79);
        assert_eq!(build_query_string(&q), "user\r\n");
    }

    #[test]
    fn query_string_list_users() {
        let q = Query::parse(Some("@host"), false, 79);
        assert_eq!(build_query_string(&q), "\r\n");
    }

    #[test]
    fn query_string_verbose_user() {
        let q = Query::parse(Some("user@host"), true, 79);
        assert_eq!(build_query_string(&q), "/W user\r\n");
    }

    #[test]
    fn query_string_verbose_list() {
        let q = Query::parse(Some("@host"), true, 79);
        assert_eq!(build_query_string(&q), "/W \r\n");
    }

    #[test]
    fn query_string_forwarding() {
        let q = Query::parse(Some("user@host1@host2"), false, 79);
        assert_eq!(build_query_string(&q), "user@host1\r\n");
    }

    #[test]
    fn query_string_forwarding_verbose() {
        let q = Query::parse(Some("user@host1@host2"), true, 79);
        assert_eq!(build_query_string(&q), "/W user@host1\r\n");
    }

    #[test]
    fn query_string_forwarding_no_user() {
        let q = Query::parse(Some("@host1@host2"), false, 79);
        assert_eq!(build_query_string(&q), "@host1\r\n");
    }

    #[test]
    fn query_string_three_host_chain() {
        let q = Query::parse(Some("user@a@b@c"), false, 79);
        assert_eq!(build_query_string(&q), "user@a@b\r\n");
    }

    #[test]
    fn query_string_localhost_user() {
        let q = Query::parse(Some("user"), false, 79);
        assert_eq!(build_query_string(&q), "user\r\n");
    }

    #[test]
    fn query_string_localhost_list() {
        let q = Query::parse(None, false, 79);
        assert_eq!(build_query_string(&q), "\r\n");
    }
}
```

- [ ] **Step 2: Run tests to verify they fail**

Run: `cargo test --lib protocol`
Expected: FAIL -- all tests fail with "not yet implemented" panics.

- [ ] **Step 3: Commit the failing tests**

```bash
git add src/protocol.rs
git commit -m "test: add wire format query string tests (red)"
```

---

### Task 5: Protocol -- Wire Format Implementation

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

- [ ] **Step 1: Implement build_query_string**

Replace the `todo!()` body in `build_query_string`:

```rust
pub fn build_query_string(query: &Query) -> String {
    let mut result = String::new();

    // Verbose prefix per RFC 1288.
    if query.long {
        result.push_str("/W ");
    }

    // User portion.
    if let Some(ref user) = query.user {
        result.push_str(user);
    }

    // Forwarding: include all hosts except the last (the connection target).
    // These become @host1@host2... in the query string.
    if query.hosts.len() > 1 {
        for host in &query.hosts[..query.hosts.len() - 1] {
            result.push('@');
            result.push_str(host);
        }
    }

    result.push_str("\r\n");
    result
}
```

- [ ] **Step 2: Run tests to verify they pass**

Run: `cargo test --lib protocol`
Expected: All 10 tests pass.

- [ ] **Step 3: Commit**

```bash
git add src/protocol.rs
git commit -m "feat: implement wire format query string builder (green)"
```

---

### Task 6: Protocol -- TCP Communication

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

- [ ] **Step 1: Implement the finger function**

Add the following imports to the top of `src/protocol.rs` (alongside existing imports):

```rust
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;
```

Add the `finger` function after `build_query_string`:

```rust
/// Execute a finger query over TCP.
///
/// Connects to the target host, sends the query string, reads the full
/// response until the server closes the connection, and returns the
/// response as a string. Invalid UTF-8 bytes are replaced with U+FFFD.
pub fn finger(query: &Query, timeout: Duration) -> Result<String, FingerError> {
    let host = query.target_host();
    let addr = format!("{}:{}", host, query.port);

    // Resolve and connect with timeout.
    let mut stream = TcpStream::connect_timeout(
        &addr.parse().map_err(|_| {
            // If the address doesn't parse as a SocketAddr, try DNS resolution.
            // This path handles hostnames (not raw IPs).
            return FingerError::DnsResolution {
                host: host.to_string(),
                source: io::Error::new(io::ErrorKind::InvalidInput, "invalid address"),
            };
        })?,
        timeout,
    )
    .map_err(|e| {
        if e.kind() == io::ErrorKind::TimedOut {
            FingerError::Timeout {
                host: host.to_string(),
                port: query.port,
            }
        } else {
            FingerError::ConnectionFailed {
                host: host.to_string(),
                port: query.port,
                source: e,
            }
        }
    })?;

    // Set read/write timeouts on the connected socket.
    stream.set_read_timeout(Some(timeout)).ok();
    stream.set_write_timeout(Some(timeout)).ok();

    // Send the query.
    let query_string = build_query_string(query);
    stream
        .write_all(query_string.as_bytes())
        .map_err(|e| FingerError::SendFailed { source: e })?;

    // Read the full response.
    let mut buf = Vec::new();
    stream
        .read_to_end(&mut buf)
        .map_err(|e| FingerError::ReadFailed { source: e })?;

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

- [ ] **Step 2: Verify it compiles**

Run: `cargo build`
Expected: Compiles successfully.

- [ ] **Step 3: Commit**

```bash
git add src/protocol.rs
git commit -m "feat: implement TCP finger protocol exchange"
```

---

### Task 7: Protocol -- DNS Resolution Fix

The `finger` function in Task 6 uses `TcpStream::connect_timeout` which requires a `SocketAddr`. This doesn't support hostnames directly. We need to resolve hostnames first using `std::net::ToSocketAddrs`.

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

- [ ] **Step 1: Rewrite the connection logic to support hostnames**

Replace the `finger` function body with:

```rust
pub fn finger(query: &Query, timeout: Duration) -> Result<String, FingerError> {
    let host = query.target_host();
    let addr_str = format!("{}:{}", host, query.port);

    // Resolve hostname to socket addresses.
    use std::net::ToSocketAddrs;
    let addr = addr_str
        .to_socket_addrs()
        .map_err(|e| FingerError::DnsResolution {
            host: host.to_string(),
            source: e,
        })?
        .next()
        .ok_or_else(|| FingerError::DnsResolution {
            host: host.to_string(),
            source: io::Error::new(io::ErrorKind::NotFound, "no addresses found"),
        })?;

    // Connect with timeout.
    let mut stream = TcpStream::connect_timeout(&addr, timeout).map_err(|e| {
        if e.kind() == io::ErrorKind::TimedOut {
            FingerError::Timeout {
                host: host.to_string(),
                port: query.port,
            }
        } else {
            FingerError::ConnectionFailed {
                host: host.to_string(),
                port: query.port,
                source: e,
            }
        }
    })?;

    // Set read/write timeouts on the connected socket.
    stream.set_read_timeout(Some(timeout)).ok();
    stream.set_write_timeout(Some(timeout)).ok();

    // Send the query.
    let query_string = build_query_string(query);
    stream
        .write_all(query_string.as_bytes())
        .map_err(|e| FingerError::SendFailed { source: e })?;

    // Read the full response.
    let mut buf = Vec::new();
    stream
        .read_to_end(&mut buf)
        .map_err(|e| FingerError::ReadFailed { source: e })?;

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

- [ ] **Step 2: Verify it compiles**

Run: `cargo build`
Expected: Compiles successfully.

- [ ] **Step 3: Commit**

```bash
git add src/protocol.rs
git commit -m "fix: use ToSocketAddrs for proper hostname resolution"
```

---

### Task 8: CLI Entry Point

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

- [ ] **Step 1: Implement the CLI with clap**

Replace the contents of `src/main.rs`:

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

use clap::Parser;

use digit::protocol::{finger, FingerError};
use digit::query::{Query, DEFAULT_PORT};

/// digit - a finger protocol client (RFC 1288 / RFC 742)
///
/// Query user information from finger servers. Supports standard queries,
/// user listings, and forwarding chains.
///
/// Note: Forwarding queries (user@host1@host2) depend on server support.
/// Many modern finger servers disable forwarding for security reasons.
#[derive(Parser, Debug)]
#[command(version, about)]
struct Cli {
    /// Finger query (e.g. "user@host", "@host", "user@host1@host2")
    query: Option<String>,

    /// Request verbose/long output (sends /W)
    #[arg(short, long)]
    long: bool,

    /// Port to connect on
    #[arg(short, long, default_value_t = DEFAULT_PORT)]
    port: u16,

    /// Connection timeout in seconds
    #[arg(short, long, default_value_t = 10)]
    timeout: u64,
}

fn main() -> ExitCode {
    let cli = Cli::parse();

    let query = Query::parse(cli.query.as_deref(), cli.long, cli.port);
    let timeout = Duration::from_secs(cli.timeout);

    match finger(&query, timeout) {
        Ok(response) => {
            print!("{}", response);
            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("digit: {}", e);
            ExitCode::FAILURE
        }
    }
}
```

- [ ] **Step 2: Verify it compiles**

Run: `cargo build`
Expected: Compiles successfully.

- [ ] **Step 3: Verify help text**

Run: `cargo run -- --help`
Expected: Shows usage with query, --long, --port, --timeout options. The about text should mention forwarding and RFC numbers.

- [ ] **Step 4: Commit**

```bash
git add src/main.rs
git commit -m "feat: implement CLI entry point with clap"
```

---

### Task 9: Integration Tests

**Files:**
- Create: `tests/integration.rs`

- [ ] **Step 1: Write integration tests with a mock TCP server**

Create `tests/integration.rs`:

```rust
use std::io::{Read, Write};
use std::net::TcpListener;
use std::thread;
use std::time::Duration;

use digit::protocol::{finger, FingerError};
use digit::query::Query;

/// Start a mock finger server that accepts one connection, reads the query,
/// and responds with `response`. Returns the port it's listening on and
/// a join handle. The `on_query` callback receives the raw query bytes.
fn mock_finger_server(
    response: &str,
    on_query: impl FnOnce(String) + Send + 'static,
) -> (u16, thread::JoinHandle<()>) {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind to ephemeral port");
    let port = listener.local_addr().unwrap().port();
    let response = response.to_string();

    let handle = thread::spawn(move || {
        let (mut stream, _) = listener.accept().expect("accept connection");
        stream
            .set_read_timeout(Some(Duration::from_secs(2)))
            .ok();

        let mut buf = Vec::new();
        let mut tmp = [0u8; 1024];
        // Read until we see \r\n (end of finger query).
        loop {
            match stream.read(&mut tmp) {
                Ok(0) => break,
                Ok(n) => {
                    buf.extend_from_slice(&tmp[..n]);
                    if buf.windows(2).any(|w| w == b"\r\n") {
                        break;
                    }
                }
                Err(_) => break,
            }
        }

        let query_str = String::from_utf8_lossy(&buf).into_owned();
        on_query(query_str);

        stream.write_all(response.as_bytes()).expect("write response");
        // Server closes connection to signal end of response.
    });

    (port, handle)
}

#[test]
fn end_to_end_user_query() {
    let (port, handle) = mock_finger_server("Login: user\r\nName: Test User\r\n", |query| {
        assert_eq!(query, "user\r\n");
    });

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

    assert!(result.contains("Login: user"));
    assert!(result.contains("Test User"));

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

#[test]
fn end_to_end_list_users() {
    let (port, handle) = mock_finger_server("user1\r\nuser2\r\n", |query| {
        assert_eq!(query, "\r\n");
    });

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

    assert!(result.contains("user1"));
    assert!(result.contains("user2"));

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

#[test]
fn end_to_end_verbose_query() {
    let (port, handle) = mock_finger_server("Verbose info\r\n", |query| {
        assert_eq!(query, "/W user\r\n");
    });

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

    assert!(result.contains("Verbose info"));

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

#[test]
fn connection_refused_returns_error() {
    // Connect to a port that nothing is listening on.
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind");
    let port = listener.local_addr().unwrap().port();
    drop(listener); // Close it so the port is free but nothing is listening.

    let q = Query::parse(Some(&format!("user@127.0.0.1")), false, port);
    let result = finger(&q, Duration::from_secs(2));

    assert!(result.is_err());
    let err = result.unwrap_err();
    let msg = format!("{}", err);
    assert!(
        msg.contains("could not connect"),
        "unexpected error: {}",
        msg
    );
}

#[test]
fn utf8_lossy_handles_invalid_bytes() {
    // Server sends bytes that aren't valid UTF-8.
    let response_bytes: Vec<u8> = vec![72, 101, 108, 108, 111, 0xFF, 0xFE, 10];
    let response_str = unsafe { String::from_utf8_unchecked(response_bytes) };

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

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

    // The valid "Hello" portion should be present.
    assert!(result.contains("Hello"));
    // Invalid bytes should be replaced with the replacement character.
    assert!(result.contains('\u{FFFD}'));

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

- [ ] **Step 2: Run the integration tests**

Run: `cargo test --test integration`
Expected: All 5 tests pass.

- [ ] **Step 3: Commit**

```bash
git add tests/integration.rs
git commit -m "test: add integration tests with mock TCP finger server"
```

---

### Task 10: Run Full Test Suite and Lint

**Files:**
- No new files.

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

Run: `cargo test`
Expected: All unit tests (query + protocol) and integration tests pass.

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

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

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

Run: `cargo fmt -- --check`
Expected: No formatting issues.

- [ ] **Step 4: Fix any issues found**

If clippy or fmt flag anything, fix them and re-run.

- [ ] **Step 5: Commit any fixes**

```bash
git add -A
git commit -m "chore: fix clippy warnings and formatting"
```

(Skip this step if there were no issues.)

---

### Task 11: Documentation

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

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

Create `README.md`:

````markdown
# digit

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.

## Installation

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

## Usage

```
digit [OPTIONS] [QUERY]
```

### 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

# 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) |
| `-h, --help` | Print help |
| `-V, --version` | Print version |

## License

MIT
````

- [ ] **Step 2: Commit**

```bash
git add README.md
git commit -m "docs: add README with usage examples"
```