reserve-core 0.5.1

Core lookup, catalog, and rate-limiting engine behind the reserve domain finder
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
use std::fmt;
use std::time::Duration;

use serde::{Deserialize, Serialize};

fn strip_hostile(raw: &str) -> String {
    raw.chars()
        .filter(|c| !c.is_control() && !reorders_text(*c))
        .collect()
}

/// @docgen Remote text reaches a terminal and a JSON file, so control bytes could rewrite the screen or hide a line.
pub(crate) fn scrub(raw: &str) -> String {
    strip_hostile(raw)
        .chars()
        .take(200)
        .collect::<String>()
        .trim()
        .to_owned()
}

/// @docgen `--raw` shows the whole answer, so the 200-character cap that fits a hostname field would cut the answer it exists to show.
/// @docgen A registry record is meant to have lines; stripping the newline the way a hostname field must would flatten it into one.
pub(crate) fn scrub_unbounded(raw: &str) -> String {
    raw.chars()
        .filter(|c| *c == '\n' || (!c.is_control() && !reorders_text(*c)))
        .collect::<String>()
        .trim()
        .to_owned()
}

/// @docgen A bidi override is no control character, yet it reverses the line and can make a taken name read as free.
/// @docgen Bengali, Persian and several Indian scripts use these to shape letters, so a typed name that carries one means it.
pub const fn shapes_letters(c: char) -> bool {
    matches!(c, '\u{200c}' | '\u{200d}')
}

pub const fn reorders_text(c: char) -> bool {
    matches!(c,
        '\u{00ad}'
        | '\u{200b}'..='\u{200f}'
        | '\u{202a}'..='\u{202e}'
        | '\u{2060}'..='\u{2064}'
        | '\u{2066}'..='\u{2069}'
        | '\u{feff}')
}

use crate::tld::Suffix;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Source {
    Registry,
    Text,
    Dns,
}

impl Source {
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::Registry => "registry",
            Self::Text => "text",
            Self::Dns => "dns",
        }
    }

    /// @docgen Only the registry settles it: DNS proves a name is taken but never free, and the text service is pattern matching.
    #[must_use]
    pub const fn is_authoritative(self) -> bool {
        matches!(self, Self::Registry)
    }
}

impl fmt::Display for Source {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.label())
    }
}

/// @docgen Every one of these is reported as unknown and never folded into available.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "reason", rename_all = "kebab-case")]
pub enum Reason {
    NoService,
    RateLimited,
    /// @docgen A registry fault is not pushback, and calling it rate limiting sends the user away to wait instead of reporting an outage.
    ServerError {
        status: u16,
    },
    /// @docgen The registry answered and refused the question, which is understood perfectly and must not read as a garbled reply.
    Declined {
        detail: String,
    },
    Blocked,
    TimedOut,
    Unreachable,
    Malformed {
        detail: String,
    },
    WrongSubject {
        answered_about: String,
    },
    /// @docgen The extension exists but only a closed group may register in it, so availability is not a meaningful question.
    NotRegistrable,
}

impl Reason {
    #[must_use]
    pub const fn label(&self) -> &'static str {
        match self {
            Self::NoService => "no registry service",
            Self::RateLimited => "rate limited",
            Self::ServerError { .. } => "registry error",
            Self::Declined { .. } => "registry declined",
            Self::Blocked => "access refused",
            Self::TimedOut => "timed out",
            Self::Unreachable => "could not connect",
            Self::Malformed { .. } => "answer not understood",
            Self::WrongSubject { .. } => "answered about another name",
            Self::NotRegistrable => "not publicly registrable",
        }
    }

    #[must_use]
    pub const fn is_retryable(&self) -> bool {
        matches!(
            self,
            Self::RateLimited
                | Self::ServerError { .. }
                | Self::Declined { .. }
                | Self::TimedOut
                | Self::Unreachable
                | Self::Blocked
        )
    }
}

impl fmt::Display for Reason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Malformed { detail } | Self::Declined { detail } => {
                write!(f, "{}: {detail}", self.label())
            }
            Self::WrongSubject { answered_about } => {
                write!(f, "{}: {answered_about}", self.label())
            }
            other => f.write_str(other.label()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "kebab-case")]
pub enum Status {
    Available,
    Taken,
    Unknown(Reason),
}

impl Status {
    #[must_use]
    pub const fn is_available(&self) -> bool {
        matches!(self, Self::Available)
    }

    #[must_use]
    pub const fn is_unknown(&self) -> bool {
        matches!(self, Self::Unknown(_))
    }

    /// @docgen Meaning never rides on color alone, so the symbol carries it too.
    #[must_use]
    pub const fn mark(&self) -> char {
        match self {
            Self::Available => '+',
            Self::Taken => '-',
            Self::Unknown(_) => '?',
        }
    }

    #[must_use]
    pub const fn label(&self) -> &'static str {
        match self {
            Self::Available => "AVAILABLE",
            Self::Taken => "TAKEN",
            Self::Unknown(_) => "UNKNOWN",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Finding {
    pub domain: String,
    pub name: String,
    pub suffix: Suffix,
    pub status: Status,
    pub source: Option<Source>,
    pub elapsed: Duration,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub responder: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub registration: Option<crate::lookup::registration::Registration>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attempts: Option<Vec<Attempt>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub raw: Option<String>,
    /// @docgen `--cache-ttl` is the one path that can serve an answer that is not fresh, so a reused row must never look like a live one.
    #[serde(default)]
    pub cached: bool,
}

impl Finding {
    #[must_use]
    pub fn unknown(name: &str, suffix: &Suffix, reason: Reason, elapsed: Duration) -> Self {
        Self {
            domain: format!("{name}.{suffix}"),
            name: name.to_owned(),
            suffix: suffix.clone(),
            status: Status::Unknown(reason),
            source: None,
            elapsed,
            responder: None,
            registration: None,
            attempts: None,
            raw: None,
            cached: false,
        }
    }

    #[must_use]
    /// @docgen A name whose extension is not in the catalog still gets a row, so it is never dropped from the table.
    pub fn unrecognized(domain: &str) -> Self {
        let suffix = domain.rsplit_once('.').map_or(domain, |(_, tail)| tail);
        Self {
            domain: domain.to_owned(),
            name: domain.to_owned(),
            suffix: Suffix::from_raw(suffix),
            status: Status::Unknown(Reason::NoService),
            source: None,
            elapsed: Duration::ZERO,
            responder: None,
            registration: None,
            attempts: None,
            raw: None,
            cached: false,
        }
    }

    #[must_use]
    pub const fn is_available(&self) -> bool {
        self.status.is_available()
    }
}

/// @docgen Kept only when `--explain` asks for it; an ordinary sweep never builds this trail.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Attempt {
    pub source: Source,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub responder: Option<String>,
    pub outcome: AttemptOutcome,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "outcome", rename_all = "kebab-case")]
pub enum AttemptOutcome {
    Decisive(Status),
    Inconclusive(Reason),
    Skipped,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Tally {
    pub available: usize,
    pub taken: usize,
    pub unknown: usize,
}

impl Tally {
    #[must_use]
    pub fn of(findings: &[Finding]) -> Self {
        let mut tally = Self::default();
        for finding in findings {
            match finding.status {
                Status::Available => tally.available += 1,
                Status::Taken => tally.taken += 1,
                Status::Unknown(_) => tally.unknown += 1,
            }
        }
        tally
    }

    #[must_use]
    pub const fn checked(&self) -> usize {
        self.available + self.taken + self.unknown
    }

    #[must_use]
    pub const fn has_available(&self) -> bool {
        self.available > 0
    }
}

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

    fn suffix() -> Suffix {
        Suffix::parse("com").expect("com parses")
    }

    #[test]
    fn only_the_registry_settles_the_question_on_its_own() {
        assert!(Source::Registry.is_authoritative());
        assert!(!Source::Dns.is_authoritative());
        assert!(!Source::Text.is_authoritative());
    }

    #[test]
    fn every_unanswered_lookup_is_unknown_and_never_available() {
        let reasons = [
            Reason::NoService,
            Reason::RateLimited,
            Reason::ServerError { status: 500 },
            Reason::Blocked,
            Reason::TimedOut,
            Reason::Unreachable,
            Reason::NotRegistrable,
            Reason::Malformed {
                detail: "bad json".to_owned(),
            },
            Reason::WrongSubject {
                answered_about: "other.com".to_owned(),
            },
        ];
        for reason in reasons {
            let status = Status::Unknown(reason);
            assert!(!status.is_available(), "{status:?} must not read as free");
            assert!(status.is_unknown());
            assert_eq!(status.mark(), '?');
        }
    }

    #[test]
    fn a_throttled_lookup_is_retryable_but_a_missing_service_is_not() {
        assert!(Reason::RateLimited.is_retryable());
        assert!(Reason::ServerError { status: 503 }.is_retryable());
        assert!(Reason::TimedOut.is_retryable());
        assert!(Reason::Unreachable.is_retryable());
        assert!(!Reason::NoService.is_retryable());
        assert!(!Reason::NotRegistrable.is_retryable());
        assert!(
            !Reason::Malformed {
                detail: String::new()
            }
            .is_retryable()
        );
    }

    #[test]
    fn the_status_mark_carries_meaning_without_color() {
        assert_eq!(Status::Available.mark(), '+');
        assert_eq!(Status::Taken.mark(), '-');
        assert_eq!(Status::Unknown(Reason::TimedOut).mark(), '?');
        assert_ne!(Status::Available.label(), Status::Taken.label());
    }

    #[test]
    fn an_unknown_finding_reports_the_reason_in_its_text() {
        let finding = Finding::unknown(
            "example",
            &suffix(),
            Reason::RateLimited,
            Duration::from_millis(20),
        );
        assert_eq!(finding.domain, "example.com");
        assert!(!finding.is_available());
        match finding.status {
            Status::Unknown(reason) => assert_eq!(reason.to_string(), "rate limited"),
            other => panic!("expected unknown, got {other:?}"),
        }
    }

    #[test]
    fn an_unreadable_answer_keeps_its_detail_in_the_message() {
        let reason = Reason::Malformed {
            detail: "truncated body".to_owned(),
        };
        assert!(reason.to_string().contains("truncated body"));
    }

    #[test]
    fn the_tally_counts_each_class_and_never_double_counts() {
        let findings = vec![
            Finding {
                domain: "a.com".to_owned(),
                name: "a".to_owned(),
                suffix: suffix(),
                status: Status::Available,
                source: Some(Source::Registry),
                elapsed: Duration::ZERO,
                responder: None,
                registration: None,
                attempts: None,
                raw: None,
                cached: false,
            },
            Finding {
                domain: "b.com".to_owned(),
                name: "b".to_owned(),
                suffix: suffix(),
                status: Status::Taken,
                source: Some(Source::Registry),
                elapsed: Duration::ZERO,
                responder: None,
                registration: None,
                attempts: None,
                raw: None,
                cached: false,
            },
            Finding::unknown("c", &suffix(), Reason::TimedOut, Duration::ZERO),
        ];

        let tally = Tally::of(&findings);
        assert_eq!(tally.available, 1);
        assert_eq!(tally.taken, 1);
        assert_eq!(tally.unknown, 1);
        assert_eq!(tally.checked(), 3);
        assert!(tally.has_available());
    }

    #[test]
    fn a_run_with_no_free_names_reports_nothing_found() {
        let tally = Tally {
            available: 0,
            taken: 5,
            unknown: 2,
        };
        assert!(!tally.has_available());
        assert_eq!(tally.checked(), 7);
    }

    #[test]
    fn an_empty_run_tallies_to_zero() {
        let tally = Tally::of(&[]);
        assert_eq!(tally, Tally::default());
        assert_eq!(tally.checked(), 0);
        assert!(!tally.has_available());
    }

    #[test]
    fn a_bidi_override_cannot_reverse_a_line_the_way_a_control_byte_would() {
        let hostile = "evil\u{202e}moc.elpmaxe\u{202c}";
        let clean = scrub(hostile);
        assert!(!clean.contains('\u{202e}'), "an override must not survive");
        assert!(!clean.contains('\u{202c}'));
        assert!(clean.contains("evil"), "the readable text still shows");

        for mark in ['\u{200b}', '\u{200f}', '\u{2066}', '\u{feff}', '\u{00ad}'] {
            let raw = format!("a{mark}b");
            assert_eq!(scrub(&raw), "ab", "{mark:?} must not survive");
        }
    }

    #[test]
    fn the_unbounded_scrub_keeps_the_lines_a_record_is_meant_to_have() {
        let record = "Domain Name: EXAMPLE.COM\nRegistrar: COM LAUDE\n\nStatus: active";
        assert_eq!(scrub_unbounded(record), record);
    }

    #[test]
    fn the_unbounded_scrub_still_strips_control_bytes_and_bidi_overrides() {
        let hostile = "line one\nevil\u{202e}owt enil\u{202c}\nline three";
        let clean = scrub_unbounded(hostile);
        assert!(!clean.contains('\u{202e}'));
        assert!(!clean.contains('\u{202c}'));
        assert!(clean.contains("line one\n"));
        assert!(clean.contains("line three"));

        let with_escape = "before\x1bafter";
        assert_eq!(scrub_unbounded(with_escape), "beforeafter");
    }

    #[test]
    fn the_unbounded_scrub_never_cuts_a_long_record_the_way_the_bounded_one_does() {
        let long = "x".repeat(400);
        assert_eq!(scrub_unbounded(&long).len(), 400);
        assert_eq!(scrub(&long).len(), 200);
    }
}