Skip to main content

adler_core/
check.rs

1//! Verdict types produced when a site is probed.
2
3use std::collections::BTreeMap;
4use std::fmt;
5
6use serde::{Deserialize, Serialize};
7
8/// Outcome of a single site probe.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum MatchKind {
12    /// The account exists on this site.
13    Found,
14    /// The account does not exist on this site.
15    NotFound,
16    /// The response was inconclusive (network error, unexpected status,
17    /// ambiguous content). Reported separately so the user can review them
18    /// rather than silently dropping signal.
19    Uncertain,
20}
21
22impl MatchKind {
23    /// True if the verdict represents a positive (existing) account.
24    pub const fn is_found(self) -> bool {
25        matches!(self, Self::Found)
26    }
27}
28
29/// Why a probe was inconclusive.
30///
31/// `Uncertain` outcomes carry a typed reason rather than a free-form string,
32/// so logic that reacts to specific cases (e.g. retry on a transient ban)
33/// matches an enum variant instead of a fragile string. The [`fmt::Display`]
34/// rendering is what the CLI prints; serialization is the externally-tagged
35/// default (unit variants → a `snake_case` string, detail-carrying variants →
36/// `{ "network": "…" }`).
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38#[serde(rename_all = "snake_case")]
39pub enum UncertainReason {
40    /// HTTP 429, or 503 with a `Retry-After` header.
41    RateLimited,
42    /// A Cloudflare interstitial / "checking your browser" page.
43    CloudflareChallenge,
44    /// A captcha gate.
45    Captcha,
46    /// The path is disallowed by the host's `robots.txt` (`--respect-robots`).
47    RobotsDisallowed,
48    /// The scan deadline elapsed before this site finished.
49    Deadline,
50    /// The executor's scheduler was closed (does not happen in practice).
51    SchedulerClosed,
52    /// A transport/network error while issuing the request.
53    Network(String),
54    /// An error reading the response body.
55    BodyRead(String),
56    /// A `bot-protected` site needed the browser backend but the per-scan
57    /// `--browser-budget` cap was already spent on earlier sites.
58    BrowserBudget,
59    /// The username doesn't satisfy the site's `regex_check`
60    /// (e.g. too short, contains forbidden characters). Reported
61    /// without issuing any HTTP request — saves both network and the
62    /// false-positive class where the site 404s on illegal usernames
63    /// in ways our signal can't tell apart from a missing account.
64    UsernameNotAllowed,
65    /// The browser backend itself failed (timeout, navigation error,
66    /// session drop, …) for a `bot-protected` site.
67    BrowserFailed(String),
68    /// Any other reason (e.g. a `doctor` pre-flight skip).
69    Other(String),
70}
71
72impl fmt::Display for UncertainReason {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        match self {
75            Self::RateLimited => f.write_str("rate_limited"),
76            Self::CloudflareChallenge => f.write_str("cloudflare_challenge"),
77            Self::Captcha => f.write_str("captcha"),
78            Self::RobotsDisallowed => f.write_str("robots_disallowed"),
79            Self::Deadline => f.write_str("deadline reached"),
80            Self::SchedulerClosed => f.write_str("scheduler closed"),
81            Self::Network(detail) => write!(f, "request: {detail}"),
82            Self::BodyRead(detail) => write!(f, "body read: {detail}"),
83            Self::BrowserBudget => f.write_str("browser_budget_exceeded"),
84            Self::UsernameNotAllowed => f.write_str("username_not_allowed"),
85            Self::BrowserFailed(detail) => write!(f, "browser: {detail}"),
86            Self::Other(detail) => f.write_str(detail),
87        }
88    }
89}
90
91/// Result of probing a single site for a username.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct CheckOutcome {
94    /// Site name (matches `Site::name`).
95    pub site: String,
96    /// Concrete URL that was requested.
97    pub url: String,
98    /// Verdict produced by the site's detection strategy.
99    pub kind: MatchKind,
100    /// Why the outcome is `Uncertain`, if it is. `None` for `Found` /
101    /// `NotFound`.
102    #[serde(default, skip_serializing_if = "Option::is_none")]
103    pub reason: Option<UncertainReason>,
104    /// Wall-clock duration of the probe.
105    pub elapsed_ms: u64,
106    /// Fields extracted from a `Found` profile when `--enrich` is active
107    /// (e.g. `name`, `bio`, `avatar`). Empty unless enrichment ran and the
108    /// site has extractor rules. Ordered by field name.
109    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
110    pub enrichment: BTreeMap<String, String>,
111    /// Human-readable descriptions of the signals that produced the verdict —
112    /// e.g. `"HTTP 404 (status_not_found)"`. Empty for `Uncertain` (no signal
113    /// fired). Surfaced by `--explain`; always present in JSON output.
114    #[serde(default, skip_serializing_if = "Vec::is_empty")]
115    pub evidence: Vec<String>,
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn match_kind_serialises_snake_case() {
124        assert_eq!(
125            serde_json::to_string(&MatchKind::Found).unwrap(),
126            "\"found\""
127        );
128        assert_eq!(
129            serde_json::to_string(&MatchKind::NotFound).unwrap(),
130            "\"not_found\""
131        );
132        assert_eq!(
133            serde_json::to_string(&MatchKind::Uncertain).unwrap(),
134            "\"uncertain\""
135        );
136    }
137
138    #[test]
139    fn match_kind_is_found() {
140        assert!(MatchKind::Found.is_found());
141        assert!(!MatchKind::NotFound.is_found());
142        assert!(!MatchKind::Uncertain.is_found());
143    }
144
145    #[test]
146    fn outcome_skips_absent_reason() {
147        let outcome = CheckOutcome {
148            site: "GitHub".into(),
149            url: "https://github.com/alice".into(),
150            kind: MatchKind::Found,
151            reason: None,
152            elapsed_ms: 42,
153            enrichment: BTreeMap::new(),
154            evidence: Vec::new(),
155        };
156        let json = serde_json::to_string(&outcome).unwrap();
157        assert!(
158            !json.contains("reason"),
159            "reason field must be omitted when None"
160        );
161        assert!(
162            !json.contains("enrichment"),
163            "enrichment must be omitted when empty"
164        );
165        assert!(json.contains("\"kind\":\"found\""));
166        assert!(json.contains("\"elapsed_ms\":42"));
167    }
168
169    #[test]
170    fn unit_reason_serialises_as_snake_case_string() {
171        let outcome = CheckOutcome {
172            site: "GitHub".into(),
173            url: "https://github.com/alice".into(),
174            kind: MatchKind::Uncertain,
175            reason: Some(UncertainReason::RateLimited),
176            elapsed_ms: 5_000,
177            enrichment: BTreeMap::new(),
178            evidence: Vec::new(),
179        };
180        let json = serde_json::to_string(&outcome).unwrap();
181        assert!(json.contains("\"reason\":\"rate_limited\""), "{json}");
182    }
183
184    #[test]
185    fn detail_reason_serialises_as_tagged_object() {
186        let json = serde_json::to_string(&UncertainReason::Network("refused".into())).unwrap();
187        assert_eq!(json, "{\"network\":\"refused\"}");
188    }
189
190    #[test]
191    fn reason_display_matches_legacy_note_text() {
192        assert_eq!(UncertainReason::RateLimited.to_string(), "rate_limited");
193        assert_eq!(UncertainReason::Deadline.to_string(), "deadline reached");
194        assert_eq!(
195            UncertainReason::Network("boom".into()).to_string(),
196            "request: boom"
197        );
198    }
199}