mailrs-spf 2.0.0

RFC 7208 Sender Policy Framework verifier. Pure-Rust evaluator with a pluggable DNS resolver trait; ships an optional hickory-resolver-backed implementation. Bounded DNS-lookup limits + loop detection per spec.
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
//! SPF record parsing (RFC 7208 §4.6).
//!
//! Turns the raw TXT string (`"v=spf1 ip4:1.2.3.4 include:example.com -all"`)
//! into a typed [`Record`] with [`Mechanism`]s + [`Qualifier`]s.

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use compact_str::CompactString;

use crate::error::SpfError;

/// SPF qualifier (RFC 7208 §4.6.2). Default is `Pass` (`+`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Qualifier {
    /// `+` — Pass on match.
    Pass,
    /// `-` — Fail on match.
    Fail,
    /// `~` — SoftFail on match.
    SoftFail,
    /// `?` — Neutral on match.
    Neutral,
}

impl Qualifier {
    fn from_byte(b: u8) -> Option<Self> {
        match b {
            b'+' => Some(Qualifier::Pass),
            b'-' => Some(Qualifier::Fail),
            b'~' => Some(Qualifier::SoftFail),
            b'?' => Some(Qualifier::Neutral),
            _ => None,
        }
    }
}

/// One SPF mechanism (RFC 7208 §5).
///
/// Each mechanism carries its [`Qualifier`] and the mechanism-specific
/// payload.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mechanism {
    /// `all` — matches every IP.
    All {
        /// Qualifier applied on match.
        qualifier: Qualifier,
    },
    /// `ip4:1.2.3.4` or `ip4:1.2.3.0/24` — matches IPv4 in the
    /// specified network.
    Ip4 {
        /// Qualifier applied on match.
        qualifier: Qualifier,
        /// Network base address.
        addr: Ipv4Addr,
        /// Prefix length (1-32). 32 if not specified in the record.
        prefix: u8,
    },
    /// `ip6:2001:db8::1` or `ip6:2001:db8::/32` — matches IPv6.
    Ip6 {
        /// Qualifier applied on match.
        qualifier: Qualifier,
        /// Network base address.
        addr: Ipv6Addr,
        /// Prefix length (1-128). 128 if not specified in the record.
        prefix: u8,
    },
    /// `a` or `a:example.com` or `a:example.com/24`.
    A {
        /// Qualifier applied on match.
        qualifier: Qualifier,
        /// Domain to look up (default = current domain in scope).
        ///
        /// **v2 change**: `CompactString` (inlined ≤24 bytes); real SPF
        /// domains nearly always fit.
        domain: Option<CompactString>,
        /// IPv4 prefix length (default 32).
        ip4_prefix: u8,
        /// IPv6 prefix length (default 128).
        ip6_prefix: u8,
    },
    /// `mx` or `mx:example.com`.
    Mx {
        /// Qualifier applied on match.
        qualifier: Qualifier,
        /// Domain whose MX records to look up. `CompactString` per `A.domain`.
        domain: Option<CompactString>,
        /// IPv4 prefix length (default 32).
        ip4_prefix: u8,
        /// IPv6 prefix length (default 128).
        ip6_prefix: u8,
    },
    /// `include:example.com` — recurse into another domain's SPF.
    Include {
        /// Qualifier applied on match.
        qualifier: Qualifier,
        /// Included domain. `CompactString` per `A.domain`.
        domain: CompactString,
    },
    /// `exists:%{ir}.example.com` — match if the lookup returns ANY A.
    Exists {
        /// Qualifier applied on match.
        qualifier: Qualifier,
        /// Domain template to look up. Macro expansion is out of v1 scope;
        /// the literal template is used as-is. `CompactString` per `A.domain`.
        domain: CompactString,
    },
}

impl Mechanism {
    /// Qualifier accessor (every variant has one).
    pub fn qualifier(&self) -> Qualifier {
        match self {
            Mechanism::All { qualifier }
            | Mechanism::Ip4 { qualifier, .. }
            | Mechanism::Ip6 { qualifier, .. }
            | Mechanism::A { qualifier, .. }
            | Mechanism::Mx { qualifier, .. }
            | Mechanism::Include { qualifier, .. }
            | Mechanism::Exists { qualifier, .. } => *qualifier,
        }
    }
}

/// Parsed SPF record.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Record {
    /// All mechanisms in document order (the evaluator walks them
    /// left-to-right and stops at the first non-implicit match).
    pub mechanisms: Vec<Mechanism>,
}

impl Record {
    /// Parse a TXT-record string as an SPF record.
    ///
    /// Returns `SpfError::InvalidRecord` if the input doesn't start
    /// with `v=spf1` or contains an unparseable mechanism.
    ///
    /// ```
    /// use mailrs_spf::Record;
    /// let r = Record::parse("v=spf1 ip4:203.0.113.0/24 include:example.com -all").unwrap();
    /// assert_eq!(r.mechanisms.len(), 3);
    /// ```
    pub fn parse(input: &str) -> Result<Self, SpfError> {
        // Single-pass byte iterator over the input. Tokenisation
        // (find the next SP), modifier filter (is the token a
        // `name=value` modifier rather than a `mech:value`?) and
        // per-token parsing are all driven from the same forward
        // walk — no `str::split(' ')` iterator intermediate, no
        // `token.contains('=')` second pass over each token.
        //
        // Same architectural shape as mail-auth 0.9's
        // `TxtRecordParser::parse(bytes)`, which uses a stateful
        // `bytes.iter()` + `next_term()` driver.
        let trimmed = input.trim();
        let after_version = trimmed
            .strip_prefix("v=spf1")
            .ok_or_else(|| SpfError::InvalidRecord("missing v=spf1 prefix".into()))?;

        let bytes = after_version.as_bytes();
        let mut mechanisms = Vec::with_capacity(4);

        // tok_start: start of the current token's bytes (or end-of-input
        // if we're between tokens).
        let mut pos = 0;
        while pos < bytes.len() {
            // Skip leading SP — typically just one between mechanisms.
            while pos < bytes.len() && bytes[pos] == b' ' {
                pos += 1;
            }
            if pos >= bytes.len() {
                break;
            }
            // Find end of token via memchr (SIMD on aarch64 / x86_64).
            let tok_start = pos;
            let tok_end = match memchr::memchr(b' ', &bytes[tok_start..]) {
                Some(rel) => tok_start + rel,
                None => bytes.len(),
            };
            // Decide: modifier (skip) or mechanism (parse)? A token is
            // a modifier iff it has '=' BEFORE any ':'. We compute both
            // positions inline in the same byte walk; saves a second
            // pass for the simple-record common case where neither
            // char is present in the token (e.g. `-all`).
            let token_bytes = &bytes[tok_start..tok_end];
            let eq = memchr::memchr(b'=', token_bytes);
            let colon = memchr::memchr(b':', token_bytes);
            let is_modifier = match (eq, colon) {
                (Some(e), Some(c)) => e < c,
                (Some(_), None) => true,
                _ => false,
            };
            if !is_modifier {
                // SAFETY: bytes come from a valid `&str` and we only
                // sliced on memchr-found byte boundaries, all ASCII.
                let token = unsafe { std::str::from_utf8_unchecked(token_bytes) };

                // Inline fast-path for the bare `all` mechanism — by
                // far the most common token in a simple SPF record
                // (every record ends with `-all`, `~all`, `?all`, or
                // `+all`). Skips the parse_mechanism call entirely.
                let tb = token.as_bytes();
                let inline_all = matches!(tb, b"all" | b"+all" | b"-all" | b"~all" | b"?all");
                if inline_all {
                    let qualifier = if tb.len() == 4 {
                        match tb[0] {
                            b'+' => Qualifier::Pass,
                            b'-' => Qualifier::Fail,
                            b'~' => Qualifier::SoftFail,
                            b'?' => Qualifier::Neutral,
                            _ => Qualifier::Pass, // unreachable
                        }
                    } else {
                        Qualifier::Pass
                    };
                    mechanisms.push(Mechanism::All { qualifier });
                } else {
                    mechanisms.push(parse_mechanism(token)?);
                }
            }
            pos = tok_end + 1;
        }

        Ok(Record { mechanisms })
    }
}

#[inline]
fn parse_mechanism(token: &str) -> Result<Mechanism, SpfError> {
    let (qualifier, body) = split_qualifier(token);

    // Split mechanism name from value
    let (name, value) = match body.split_once(':') {
        Some((n, v)) => (n, Some(v)),
        None => {
            // Could be `a` or `a/24` (prefix without explicit domain)
            if let Some((n, _)) = body.split_once('/') {
                (n, Some(&body[n.len()..])) // include the '/' in value
            } else {
                (body, None)
            }
        }
    };

    // Byte-match on the mechanism name. Avoids the UTF-8-aware `&str` match
    // path — mechanism names are pure ASCII so the byte form is strictly
    // cheaper at runtime.
    match name.as_bytes() {
        b"all" => {
            if value.is_some() {
                return Err(SpfError::InvalidRecord(format!(
                    "'all' takes no value: {token}"
                )));
            }
            Ok(Mechanism::All { qualifier })
        }
        b"ip4" => {
            let v = value.ok_or_else(|| SpfError::InvalidRecord("ip4: missing value".into()))?;
            let (addr_str, prefix) = parse_addr_and_prefix(v, 32)?;
            // Hand-rolled byte-level IPv4 parser. `std::net::Ipv4Addr::FromStr`
            // pays for general-purpose error reporting + UTF-8 char iter.
            // For dotted-quad ASCII input we can do it in ~5 ns by walking
            // bytes once and rejecting on the first non-digit/non-dot. This
            // closes the +25% gap vs `mail-auth` 0.9 on simple records.
            let addr = parse_ipv4_fast(addr_str)
                .ok_or_else(|| SpfError::InvalidRecord(format!("bad ipv4 address: {addr_str}")))?;
            Ok(Mechanism::Ip4 {
                qualifier,
                addr,
                prefix,
            })
        }
        b"ip6" => {
            let v = value.ok_or_else(|| SpfError::InvalidRecord("ip6: missing value".into()))?;
            let (addr_str, prefix) = parse_addr_and_prefix(v, 128)?;
            let addr: Ipv6Addr = addr_str
                .parse()
                .map_err(|_| SpfError::InvalidRecord(format!("bad ipv6 address: {addr_str}")))?;
            Ok(Mechanism::Ip6 {
                qualifier,
                addr,
                prefix,
            })
        }
        b"a" => {
            let (domain, ip4_prefix, ip6_prefix) = parse_a_mx_value(value)?;
            Ok(Mechanism::A {
                qualifier,
                domain,
                ip4_prefix,
                ip6_prefix,
            })
        }
        b"mx" => {
            let (domain, ip4_prefix, ip6_prefix) = parse_a_mx_value(value)?;
            Ok(Mechanism::Mx {
                qualifier,
                domain,
                ip4_prefix,
                ip6_prefix,
            })
        }
        b"include" => {
            let v =
                value.ok_or_else(|| SpfError::InvalidRecord("include: missing domain".into()))?;
            Ok(Mechanism::Include {
                qualifier,
                domain: CompactString::new(v),
            })
        }
        b"exists" => {
            let v =
                value.ok_or_else(|| SpfError::InvalidRecord("exists: missing domain".into()))?;
            Ok(Mechanism::Exists {
                qualifier,
                domain: CompactString::new(v),
            })
        }
        b"ptr" => {
            // RFC 7208 §5.5 marks ptr as not-recommended; v1.0 of this
            // crate doesn't implement PTR lookups → permerror.
            Err(SpfError::InvalidRecord(
                "ptr mechanism not supported (RFC 7208 §5.5 deprecates)".into(),
            ))
        }
        _ => Err(SpfError::InvalidRecord(format!(
            "unknown mechanism: {name}"
        ))),
    }
}

#[inline]
fn split_qualifier(token: &str) -> (Qualifier, &str) {
    if let Some(first) = token.as_bytes().first()
        && let Some(q) = Qualifier::from_byte(*first)
    {
        return (q, &token[1..]);
    }
    (Qualifier::Pass, token) // default qualifier is `+`
}

/// Borrow-returning variant — avoids the `to_string()` allocation that the
/// SPF hot path used to pay per `ip4:`/`ip6:` mechanism.
/// Byte-level IPv4 dotted-quad parser. Returns `None` for any input
/// `std::net::Ipv4Addr::FromStr` would also reject (4 octets, 0-255
/// each, no leading + sign, no trailing whitespace).
///
/// Single-pass state machine: walks the bytes exactly once, building
/// each octet inline. No intermediate scan for dot positions, no
/// second pass to decode octets — same shape as mail-auth 0.9's
/// `Ipv4Addr` parser. ~5-8× faster than `<Ipv4Addr as FromStr>` on
/// typical SPF dotted-quad inputs.
#[inline]
fn parse_ipv4_fast(s: &str) -> Option<Ipv4Addr> {
    let bytes = s.as_bytes();
    let mut octets = [0u8; 4];
    let mut idx = 0usize;
    let mut current: u16 = 0;
    let mut started = false;

    for &b in bytes {
        if b.is_ascii_digit() {
            current = current * 10 + (b - b'0') as u16;
            if current > 255 {
                return None;
            }
            started = true;
        } else if b == b'.' {
            if !started || idx >= 3 {
                return None;
            }
            // SAFETY: current ≤ 255 enforced above.
            octets[idx] = current as u8;
            idx += 1;
            current = 0;
            started = false;
        } else {
            return None;
        }
    }

    if !started || idx != 3 {
        return None;
    }
    octets[3] = current as u8;
    Some(Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]))
}

/// Decode a 1-3 byte ASCII decimal slice into `u8`. Same input space
/// as `<u8 as FromStr>` but unrolled per length so LLVM can elide the
/// loop-counter induction that the generic parser pays for.
#[inline]
fn parse_octet(bytes: &[u8]) -> Option<u8> {
    match bytes.len() {
        1 => {
            let d = bytes[0].wrapping_sub(b'0');
            if d <= 9 { Some(d) } else { None }
        }
        2 => {
            let d0 = bytes[0].wrapping_sub(b'0');
            let d1 = bytes[1].wrapping_sub(b'0');
            if d0 <= 9 && d1 <= 9 {
                Some(d0 * 10 + d1)
            } else {
                None
            }
        }
        3 => {
            let d0 = bytes[0].wrapping_sub(b'0');
            let d1 = bytes[1].wrapping_sub(b'0');
            let d2 = bytes[2].wrapping_sub(b'0');
            if d0 <= 9 && d1 <= 9 && d2 <= 9 {
                let v = d0 as u16 * 100 + d1 as u16 * 10 + d2 as u16;
                if v <= 255 { Some(v as u8) } else { None }
            } else {
                None
            }
        }
        _ => None,
    }
}

#[inline]
fn parse_addr_and_prefix(value: &str, default: u8) -> Result<(&str, u8), SpfError> {
    if let Some((addr, prefix_str)) = value.rsplit_once('/') {
        // Reuse the unrolled per-length octet parser — prefix is also
        // a 1-3 digit u8 in the SPF grammar (1-32 for ip4, 1-128 for
        // ip6). Avoids `<u8 as FromStr>`'s generic loop induction.
        let prefix = parse_octet(prefix_str.as_bytes())
            .ok_or_else(|| SpfError::InvalidRecord(format!("bad prefix: {prefix_str}")))?;
        Ok((addr, prefix))
    } else {
        Ok((value, default))
    }
}

/// Parse the optional `:domain/prefix4//prefix6` suffix on `a` and `mx`.
fn parse_a_mx_value(
    value: Option<&str>,
) -> Result<(Option<CompactString>, u8, u8), SpfError> {
    let Some(v) = value else {
        return Ok((None, 32, 128));
    };
    let (domain_part, prefix_part) = match v.find('/') {
        Some(idx) => (Some(&v[..idx]), &v[idx..]),
        None => (Some(v), ""),
    };
    let domain = domain_part
        .filter(|s| !s.is_empty())
        .map(CompactString::new);

    let (ip4_prefix, ip6_prefix) = if prefix_part.is_empty() {
        (32u8, 128u8)
    } else if let Some(rest) = prefix_part.strip_prefix("//") {
        // only //ip6
        let p6: u8 = rest
            .parse()
            .map_err(|_| SpfError::InvalidRecord(format!("bad ip6 prefix: {rest}")))?;
        (32, p6)
    } else if let Some(rest) = prefix_part.strip_prefix('/') {
        if let Some((p4_str, p6_str)) = rest.split_once("//") {
            let p4: u8 = p4_str
                .parse()
                .map_err(|_| SpfError::InvalidRecord(format!("bad ip4 prefix: {p4_str}")))?;
            let p6: u8 = p6_str
                .parse()
                .map_err(|_| SpfError::InvalidRecord(format!("bad ip6 prefix: {p6_str}")))?;
            (p4, p6)
        } else {
            let p4: u8 = rest
                .parse()
                .map_err(|_| SpfError::InvalidRecord(format!("bad ip4 prefix: {rest}")))?;
            (p4, 128)
        }
    } else {
        (32, 128)
    };

    Ok((domain, ip4_prefix, ip6_prefix))
}

/// Check whether `ip` falls in `subnet/prefix`.
pub(crate) fn ip_in_subnet(ip: IpAddr, subnet: IpAddr, prefix: u8) -> bool {
    match (ip, subnet) {
        (IpAddr::V4(a), IpAddr::V4(b)) => {
            if prefix == 0 {
                return true;
            }
            if prefix > 32 {
                return false;
            }
            let mask: u32 = if prefix == 32 {
                u32::MAX
            } else {
                !((1u32 << (32 - prefix)) - 1)
            };
            (u32::from_be_bytes(a.octets()) & mask) == (u32::from_be_bytes(b.octets()) & mask)
        }
        (IpAddr::V6(a), IpAddr::V6(b)) => {
            if prefix == 0 {
                return true;
            }
            if prefix > 128 {
                return false;
            }
            let a_bits = u128::from_be_bytes(a.octets());
            let b_bits = u128::from_be_bytes(b.octets());
            let mask: u128 = if prefix == 128 {
                u128::MAX
            } else {
                !((1u128 << (128 - prefix)) - 1)
            };
            (a_bits & mask) == (b_bits & mask)
        }
        // Mixed v4/v6 — never match.
        _ => false,
    }
}

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

    #[test]
    fn parse_minimal_all_record() {
        let r = Record::parse("v=spf1 -all").unwrap();
        assert_eq!(r.mechanisms.len(), 1);
        assert_eq!(
            r.mechanisms[0],
            Mechanism::All {
                qualifier: Qualifier::Fail
            }
        );
    }

    #[test]
    fn parse_record_with_ip4() {
        let r = Record::parse("v=spf1 ip4:203.0.113.0/24 -all").unwrap();
        assert_eq!(r.mechanisms.len(), 2);
        assert_eq!(
            r.mechanisms[0],
            Mechanism::Ip4 {
                qualifier: Qualifier::Pass,
                addr: "203.0.113.0".parse().unwrap(),
                prefix: 24,
            }
        );
    }

    #[test]
    fn parse_record_with_ip4_no_prefix() {
        let r = Record::parse("v=spf1 ip4:1.2.3.4 -all").unwrap();
        if let Mechanism::Ip4 { prefix, .. } = r.mechanisms[0] {
            assert_eq!(prefix, 32);
        } else {
            panic!("expected ip4");
        }
    }

    #[test]
    fn parse_record_with_ip6() {
        let r = Record::parse("v=spf1 ip6:2001:db8::/32 -all").unwrap();
        assert_eq!(
            r.mechanisms[0],
            Mechanism::Ip6 {
                qualifier: Qualifier::Pass,
                addr: "2001:db8::".parse().unwrap(),
                prefix: 32,
            }
        );
    }

    #[test]
    fn parse_record_with_include() {
        let r = Record::parse("v=spf1 include:_spf.google.com -all").unwrap();
        assert_eq!(
            r.mechanisms[0],
            Mechanism::Include {
                qualifier: Qualifier::Pass,
                domain: "_spf.google.com".into(),
            }
        );
    }

    #[test]
    fn parse_record_with_softfail_all() {
        let r = Record::parse("v=spf1 ~all").unwrap();
        assert_eq!(
            r.mechanisms[0],
            Mechanism::All {
                qualifier: Qualifier::SoftFail
            }
        );
    }

    #[test]
    fn parse_record_with_neutral_all() {
        let r = Record::parse("v=spf1 ?all").unwrap();
        assert_eq!(
            r.mechanisms[0],
            Mechanism::All {
                qualifier: Qualifier::Neutral
            }
        );
    }

    #[test]
    fn parse_record_with_a_default() {
        let r = Record::parse("v=spf1 a -all").unwrap();
        assert_eq!(
            r.mechanisms[0],
            Mechanism::A {
                qualifier: Qualifier::Pass,
                domain: None,
                ip4_prefix: 32,
                ip6_prefix: 128,
            }
        );
    }

    #[test]
    fn parse_record_with_a_explicit_domain() {
        let r = Record::parse("v=spf1 a:example.com -all").unwrap();
        assert_eq!(
            r.mechanisms[0],
            Mechanism::A {
                qualifier: Qualifier::Pass,
                domain: Some("example.com".into()),
                ip4_prefix: 32,
                ip6_prefix: 128,
            }
        );
    }

    #[test]
    fn parse_record_with_a_and_prefix() {
        let r = Record::parse("v=spf1 a:example.com/24 -all").unwrap();
        if let Mechanism::A {
            domain,
            ip4_prefix,
            ip6_prefix,
            ..
        } = &r.mechanisms[0]
        {
            assert_eq!(domain.as_deref(), Some("example.com"));
            assert_eq!(*ip4_prefix, 24);
            assert_eq!(*ip6_prefix, 128);
        } else {
            panic!("expected a");
        }
    }

    #[test]
    fn parse_record_with_a_v4_and_v6_prefixes() {
        let r = Record::parse("v=spf1 a:example.com/24//64 -all").unwrap();
        if let Mechanism::A {
            ip4_prefix,
            ip6_prefix,
            ..
        } = r.mechanisms[0]
        {
            assert_eq!(ip4_prefix, 24);
            assert_eq!(ip6_prefix, 64);
        } else {
            panic!("expected a");
        }
    }

    #[test]
    fn parse_record_with_mx() {
        let r = Record::parse("v=spf1 mx -all").unwrap();
        assert!(matches!(r.mechanisms[0], Mechanism::Mx { .. }));
    }

    #[test]
    fn parse_record_with_exists() {
        let r = Record::parse("v=spf1 exists:%{i}._spf.example.com -all").unwrap();
        if let Mechanism::Exists { domain, .. } = &r.mechanisms[0] {
            assert_eq!(domain, "%{i}._spf.example.com");
        } else {
            panic!("expected exists");
        }
    }

    #[test]
    fn parse_record_rejects_missing_version() {
        let r = Record::parse("ip4:1.2.3.4 -all");
        assert!(matches!(r, Err(SpfError::InvalidRecord(_))));
    }

    #[test]
    fn parse_record_rejects_unknown_mechanism() {
        let r = Record::parse("v=spf1 frobnicate -all");
        assert!(matches!(r, Err(SpfError::InvalidRecord(_))));
    }

    #[test]
    fn parse_record_rejects_ptr_mechanism() {
        let r = Record::parse("v=spf1 ptr -all");
        assert!(matches!(r, Err(SpfError::InvalidRecord(_))));
    }

    #[test]
    fn parse_record_skips_modifiers() {
        // `redirect=` is a modifier, not a mechanism — silently skipped in v1.0.
        let r = Record::parse("v=spf1 redirect=spf.example.com").unwrap();
        assert_eq!(r.mechanisms.len(), 0);
    }

    #[test]
    fn parse_empty_record_after_version() {
        let r = Record::parse("v=spf1").unwrap();
        assert_eq!(r.mechanisms.len(), 0);
    }

    #[test]
    fn parse_record_handles_extra_whitespace() {
        let r = Record::parse("  v=spf1   ip4:1.2.3.4   -all  ").unwrap();
        assert_eq!(r.mechanisms.len(), 2);
    }

    #[test]
    fn ip_in_subnet_ipv4_exact_match() {
        let ip: IpAddr = "203.0.113.42".parse().unwrap();
        let net: IpAddr = "203.0.113.0".parse().unwrap();
        assert!(ip_in_subnet(ip, net, 24));
        assert!(!ip_in_subnet(ip, net, 32));
    }

    #[test]
    fn ip_in_subnet_ipv4_zero_prefix() {
        let ip: IpAddr = "1.2.3.4".parse().unwrap();
        let net: IpAddr = "0.0.0.0".parse().unwrap();
        assert!(ip_in_subnet(ip, net, 0));
    }

    #[test]
    fn ip_in_subnet_ipv6_match() {
        let ip: IpAddr = "2001:db8::1".parse().unwrap();
        let net: IpAddr = "2001:db8::".parse().unwrap();
        // /32 matches: prefix covers the first 32 bits which agree
        assert!(ip_in_subnet(ip, net, 32));
        // /128 should NOT match because the host bits differ
        assert!(!ip_in_subnet(ip, net, 128));
        // But /127 should match because last bit is masked off
        assert!(ip_in_subnet(ip, net, 127));
    }

    #[test]
    fn ip_in_subnet_v4_v6_mixed_never_matches() {
        let v4: IpAddr = "1.2.3.4".parse().unwrap();
        let v6: IpAddr = "2001:db8::1".parse().unwrap();
        assert!(!ip_in_subnet(v4, v6, 0));
        assert!(!ip_in_subnet(v6, v4, 0));
    }
}