sip-header 1.0.0-beta.2

SIP header field parsers: Via, Warning, Auth, Accept, Contact, Call-Info, History-Info, Geolocation, Security, and full IANA header catalog
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
//! The shared shape and parse path of every comma-list value type.

use crate::diagnostic::{Field, ParseWarning, Parsed, WarningCode};
use crate::error::ParseError;
use crate::scrub::{scrub, Scrubbed};
use crate::span::{Located, Span};
use crate::{QuoteStart, RowEntry};

/// Constructor, accessors, iteration and Display for a
/// `struct $Type(Vec<$Entry>)`.
///
/// `non_empty` types refuse an empty list, which their grammar forbids;
/// `may_be_empty` types build from any entries.
macro_rules! list_type {
    ($Type:ident, $Entry:ty, non_empty) => {
        impl $Type {
            /// Build from entries; errors when `entries` is empty, which
            /// this header's grammar forbids.
            pub fn new(entries: Vec<$Entry>) -> Result<Self, $crate::error::ParseError> {
                if entries.is_empty() {
                    return Err($crate::error::ParseError::empty(
                        $crate::diagnostic::Field::Value,
                    ));
                }
                Ok(Self(entries))
            }

            /// Remove and return the entry at `index`, `None` when there is
            /// none; errors on the last entry, which the grammar needs.
            pub fn remove(
                &mut self,
                index: usize,
            ) -> Result<Option<$Entry>, $crate::error::ParseError> {
                $crate::list::remove_needed(&mut self.0, index)
            }

            /// Keep only the entries for which `keep` returns `true`, in
            /// order; errors, the list unchanged, when none would be kept.
            pub fn retain(
                &mut self,
                keep: impl FnMut(&$Entry) -> bool,
            ) -> Result<(), $crate::error::ParseError> {
                $crate::list::retain_needed(&mut self.0, keep)
            }
        }

        #[cfg(feature = "serde")]
        impl<'de> serde::Deserialize<'de> for $Type {
            fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                let entries = $crate::serde_parts::shaped::<Vec<$Entry>, _>(
                    deserializer,
                    stringify!($Type),
                    "a sequence",
                )?;
                Self::new(entries).map_err(<D::Error as serde::de::Error>::custom)
            }
        }

        list_type!(@common $Type, $Entry);
    };
    ($Type:ident, $Entry:ty, may_be_empty) => {
        impl $Type {
            /// Build from entries; this header's grammar admits the empty list.
            pub fn new(entries: Vec<$Entry>) -> Self {
                Self(entries)
            }

            /// Remove and return the entry at `index`, `None` when there is
            /// none.
            pub fn remove(&mut self, index: usize) -> Option<$Entry> {
                (index < self.0.len()).then(|| self.0.remove(index))
            }

            /// Keep only the entries for which `keep` returns `true`, in order.
            pub fn retain(&mut self, keep: impl FnMut(&$Entry) -> bool) {
                self.0
                    .retain(keep);
            }
        }

        #[cfg(feature = "serde")]
        impl<'de> serde::Deserialize<'de> for $Type {
            fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                $crate::serde_parts::shaped::<Vec<$Entry>, _>(
                    deserializer,
                    stringify!($Type),
                    "a sequence",
                )
                .map(Self::new)
            }
        }

        list_type!(@common $Type, $Entry);
    };
    (@common $Type:ident, $Entry:ty) => {
        #[cfg(feature = "serde")]
        impl $crate::list::Entries for $Type {
            type Entry = $Entry;

            fn into_entry_vec(self) -> Vec<$Entry> {
                self.0
            }
        }

        #[cfg(feature = "serde")]
        impl serde::Serialize for $Type {
            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                serializer.collect_seq(&self.0)
            }
        }

        impl $Type {
            /// The entries as a slice.
            pub fn entries(&self) -> &[$Entry] {
                &self.0
            }

            /// Consume self and return the entries as a `Vec`.
            pub fn into_entries(self) -> Vec<$Entry> {
                self.0
            }

            /// The entries, in order.
            pub fn iter(&self) -> std::slice::Iter<'_, $Entry> {
                self.0
                    .iter()
            }

            /// The entries, in order, each changed only through its own
            /// checked methods.
            pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, $Entry> {
                self.0
                    .iter_mut()
            }

            /// The entry at `index`, changed only through its own checked
            /// methods; `None` when there is none.
            pub fn get_mut(&mut self, index: usize) -> Option<&mut $Entry> {
                self.0
                    .get_mut(index)
            }

            /// Append an entry.
            pub fn push(&mut self, entry: $Entry) {
                self.0
                    .push(entry);
            }

            /// Number of entries.
            pub fn len(&self) -> usize {
                self.0
                    .len()
            }

            /// Returns `true` if there are no entries.
            pub fn is_empty(&self) -> bool {
                self.0
                    .is_empty()
            }
        }

        impl std::fmt::Display for $Type {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                $crate::fmt_joined(f, &self.0, ", ")
            }
        }

        impl IntoIterator for $Type {
            type Item = $Entry;
            type IntoIter = std::vec::IntoIter<$Entry>;

            fn into_iter(self) -> Self::IntoIter {
                self.0
                    .into_iter()
            }
        }

        impl<'a> IntoIterator for &'a $Type {
            type Item = &'a $Entry;
            type IntoIter = std::slice::Iter<'a, $Entry>;

            fn into_iter(self) -> Self::IntoIter {
                self.0
                    .iter()
            }
        }
    };
}

/// Remove `entries[index]`, refusing the last entry of a list whose
/// grammar needs one.
pub(crate) fn remove_needed<T>(
    entries: &mut Vec<T>,
    index: usize,
) -> Result<Option<T>, ParseError> {
    if index >= entries.len() {
        return Ok(None);
    }
    if entries.len() == 1 {
        return Err(ParseError::empty(Field::Value));
    }
    Ok(Some(entries.remove(index)))
}

/// Keep the entries `keep` accepts, refusing, the entries unchanged, to
/// keep none of a list whose grammar needs one.
pub(crate) fn retain_needed<T>(
    entries: &mut Vec<T>,
    mut keep: impl FnMut(&T) -> bool,
) -> Result<(), ParseError> {
    let kept: Vec<bool> = entries
        .iter()
        .map(&mut keep)
        .collect();
    if !kept.contains(&true) {
        return Err(ParseError::empty(Field::Value));
    }
    let mut kept = kept.into_iter();
    entries.retain(|_| {
        kept.next()
            .unwrap_or(true)
    });
    Ok(())
}

/// The warning for an entry dropped on `fault`, at the fault, covering `entry`.
fn skipped(fault: &ParseError, entry: Span) -> ParseWarning {
    let field = match fault {
        ParseError::Malformed(f) => f.field,
        ParseError::NonConformant(w) => w.field,
        ParseError::Uri(_) | ParseError::Row(_) => Field::Entry,
    };
    let at = fault
        .span()
        .map_or(
            entry
                .range()
                .start,
            |s| {
                s.range()
                    .start
            },
        );
    ParseWarning::new(field, WarningCode::SkippedEntry)
        .at(at)
        .covering(entry)
}

/// A header value of the form `entry *(COMMA entry)`.
///
/// Implementors supply the per-entry parser and their empty-list rule;
/// [`list_parse!`] turns that into the parse traits.
pub(crate) trait CommaList: Sized {
    type Entry: Located;

    /// Whether a lone blank entry is the empty list, for grammars of the
    /// form `[ entry *(COMMA entry) ]`; every other blank entry is
    /// [`EmptyEntry`](crate::WarningCode::EmptyEntry).
    const BLANK_ENTRIES_ARE_EMPTY: bool = false;

    /// Where this grammar lets a `quoted-string` start, which decides the
    /// commas a quote hides.
    const QUOTE_START: QuoteStart = QuoteStart::Param;

    /// Parse one entry, positions relative to `entry`; an `Err` drops it
    /// under [`skipped`].
    fn parse_entry(
        entry: &str,
        warnings: &mut Vec<ParseWarning>,
    ) -> Result<Self::Entry, ParseError>;

    /// Build the list from the entries kept.
    fn from_parsed(entries: Vec<Self::Entry>) -> Result<Self, ParseError>;

    /// Build the list, reporting breaches that span entries; `entries`
    /// pairs each kept entry with its wire entry index, the blank and
    /// dropped entries between them included.
    fn from_parsed_reporting(
        entries: Vec<(usize, Self::Entry)>,
        _warnings: &mut Vec<ParseWarning>,
    ) -> Result<Self, ParseError> {
        Self::from_parsed(
            entries
                .into_iter()
                .map(|(_, entry)| entry)
                .collect(),
        )
    }

    /// Split `raw` at top-level commas and parse every entry.
    fn list_from_str(raw: &str) -> Result<Parsed<Self>, ParseError> {
        let whole = scrub(raw);
        if whole
            .text
            .trim()
            .is_empty()
        {
            return Self::from_parsed(Vec::new()).map(|v| Parsed::new(v, whole.warnings));
        }
        Self::list_from_marked(crate::split_row(raw, None, Self::QUOTE_START))
    }

    /// Parse entries already split, each a row of its own, attributing
    /// errors and warnings to their entry index.
    fn list_from_entries<'a>(
        entries: impl IntoIterator<Item = &'a str>,
    ) -> Result<Parsed<Self>, ParseError> {
        Self::list_from_marked(
            entries
                .into_iter()
                .enumerate()
                .map(|(i, e)| RowEntry::whole(i, e)),
        )
    }

    /// Split every row at top-level commas and parse every entry, entry
    /// indexes counted across rows.
    fn list_from_rows<'a>(
        rows: impl IntoIterator<Item = &'a str>,
    ) -> Result<Parsed<Self>, ParseError> {
        Self::list_from_marked(crate::row_entries(rows, Self::QUOTE_START))
    }

    /// Parse every entry, positioned in the row it was split from.
    fn list_from_marked<'a>(
        entries: impl IntoIterator<Item = RowEntry<'a>>,
    ) -> Result<Parsed<Self>, ParseError> {
        let ReadEntries {
            kept,
            mut warnings,
            first_fault,
            rows,
        } = Self::read_marked(entries)?;
        let none_kept = kept.is_empty();
        let value =
            Self::from_parsed_reporting(kept, &mut warnings).map_err(|e| match first_fault {
                Some(fault) if none_kept => fault,
                _ => e,
            })?;
        for w in &mut warnings {
            if w.row
                .is_none()
            {
                w.row = w
                    .entry
                    .and_then(|e| rows.get(e))
                    .copied()
                    .flatten();
            }
        }
        Ok(Parsed::new(value, warnings))
    }

    /// Parse every entry, positioned in the row it was split from; errs only
    /// with a store's row error.
    fn read_marked<'a>(
        entries: impl IntoIterator<Item = RowEntry<'a>>,
    ) -> Result<ReadEntries<Self::Entry>, ParseError> {
        let entries: Vec<(RowEntry<'a>, Scrubbed<'a>)> = entries
            .into_iter()
            .map(|e| (e, scrub(e.text)))
            .collect();
        let is_blank = |e: &Scrubbed<'_>| {
            e.text
                .trim()
                .is_empty()
        };
        let empty_list = Self::BLANK_ENTRIES_ARE_EMPTY
            && matches!(entries.as_slice(), [(e, s)] if e.comma.is_none() && is_blank(s));
        let rows: Vec<Option<usize>> = entries
            .iter()
            .map(|(e, _)| e.row)
            .collect();
        let mut warnings = Vec::new();
        let mut kept = Vec::with_capacity(entries.len());
        let mut first_fault = None;
        for (i, (entry, mut scrubbed)) in entries
            .into_iter()
            .enumerate()
        {
            let in_row = entry.relocation();
            let comma = entry.final_comma();
            let own: Vec<ParseWarning> = std::mem::take(&mut scrubbed.warnings)
                .into_iter()
                .map(|w| w.relocate(&in_row))
                .collect();
            if is_blank(&scrubbed) {
                let empty =
                    (!empty_list).then(|| crate::empty_entry(Field::Entry, 0).relocate(&in_row));
                warnings.extend(
                    own.into_iter()
                        .chain(empty)
                        .chain(comma)
                        .map(|w| w.in_entry(i)),
                );
                continue;
            }
            let back = scrubbed
                .relocation()
                .then_shift(entry.base, entry.row);
            let mut found = Vec::new();
            let whole = Span::within(
                &scrubbed.text,
                scrubbed
                    .text
                    .trim(),
            );
            let value = match Self::parse_entry(&scrubbed.text, &mut found) {
                Ok(value) => Some(value),
                Err(ParseError::Row(e)) => return Err(ParseError::Row(e)),
                Err(e) => {
                    let e = e.placed(
                        whole
                            .range()
                            .start,
                    );
                    found.push(skipped(&e, whole));
                    first_fault.get_or_insert_with(|| {
                        e.relocate(&back)
                            .in_entry(i)
                    });
                    None
                }
            };
            let found = found
                .into_iter()
                .map(|w| w.relocate(&back));
            warnings.extend(
                own.into_iter()
                    .chain(found)
                    .chain(comma)
                    .map(|w| w.in_entry(i)),
            );
            kept.extend(value.map(|mut v| {
                v.relocate_spans(&back);
                (i, v)
            }));
        }
        Ok(ReadEntries {
            kept,
            warnings,
            first_fault,
            rows,
        })
    }
}

/// What reading a list's entries left: each kept entry with its index, the
/// warnings, the first dropped entry's fault, and each entry's row.
pub(crate) struct ReadEntries<E> {
    kept: Vec<(usize, E)>,
    warnings: Vec<ParseWarning>,
    first_fault: Option<ParseError>,
    rows: Vec<Option<usize>>,
}

/// The first entry of `raw` parsed alone, as the list `L` reads it, outside
/// any list; text after its comma is dropped under
/// [`WarningCode::TrailingContent`].
pub(crate) fn entry_from_str<L: CommaList>(raw: &str) -> Result<Parsed<L::Entry>, ParseError> {
    let mut split = crate::split_row(raw, None, L::QUOTE_START);
    let first = split.next();
    let rest = split
        .next()
        .and(first)
        .and_then(|e| {
            e.base
                .map(|b| {
                    b + e
                        .text
                        .len()
                })
        });
    let read = L::read_marked(first)?;
    let Some((_, value)) = read
        .kept
        .into_iter()
        .next()
    else {
        return Err(read
            .first_fault
            .map_or_else(
                || ParseError::empty(Field::Value),
                ParseError::without_entry,
            ));
    };
    let mut warnings = read.warnings;
    for w in &mut warnings {
        w.entry = None;
    }
    if let Some(at) = rest {
        warnings.push(
            ParseWarning::new(Field::Value, WarningCode::TrailingContent)
                .at(at)
                .covering(Span::new(
                    at..raw
                        .trim_end()
                        .len(),
                )),
        );
    }
    Ok(Parsed::new(value, warnings))
}

/// HeaderParse for a list's entry type, parsing one entry as
/// [`entry_from_str`] does.
macro_rules! entry_parse {
    ($Entry:ty, $List:ty) => {
        impl $crate::traits::HeaderParse for $Entry {
            fn parse_with_warnings(
                raw: &str,
            ) -> Result<$crate::diagnostic::Parsed<Self>, $crate::error::ParseError> {
                $crate::list::entry_from_str::<$List>(raw)
            }
        }
    };
}

/// A list type's entries, for checking one entry through the list's parser.
#[cfg(feature = "serde")]
pub(crate) trait Entries {
    type Entry;

    fn into_entry_vec(self) -> Vec<Self::Entry>;
}

/// `entry` when parsing its wire form as a list gives back that one entry.
#[cfg(feature = "serde")]
pub(crate) fn entry_reads_back<L>(
    entry: <L as CommaList>::Entry,
) -> Result<<L as CommaList>::Entry, ParseError>
where
    L: CommaList + Entries<Entry = <L as CommaList>::Entry>,
    <L as CommaList>::Entry: std::fmt::Display + PartialEq,
{
    crate::check::reads_back(entry, |wire| {
        let mut entries = L::list_from_str(wire)?
            .value
            .into_entry_vec();
        match (entries.pop(), entries.is_empty()) {
            (Some(one), true) => Ok(one),
            _ => Err(ParseError::malformed(
                crate::diagnostic::Field::Value,
                crate::error::FaultCode::Unrepresentable,
                None,
            )),
        }
    })
}

/// HeaderParse and ListParse for a type implementing [`CommaList`].
macro_rules! list_parse {
    ($Type:ident) => {
        impl $crate::traits::sealed::Sealed for $Type {}

        impl $crate::traits::HeaderParse for $Type {
            fn parse_with_warnings(
                raw: &str,
            ) -> Result<$crate::diagnostic::Parsed<Self>, $crate::error::ParseError> {
                <Self as $crate::list::CommaList>::list_from_str(raw)
            }
        }

        impl $crate::traits::ListParse for $Type {
            fn from_entries_with_warnings<'a>(
                entries: impl IntoIterator<Item = &'a str>,
            ) -> Result<$crate::diagnostic::Parsed<Self>, $crate::error::ParseError> {
                <Self as $crate::list::CommaList>::list_from_entries(entries)
            }

            fn from_rows_with_warnings<'a>(
                rows: impl IntoIterator<Item = &'a str>,
            ) -> Result<$crate::diagnostic::Parsed<Self>, $crate::error::ParseError> {
                <Self as $crate::list::CommaList>::list_from_rows(rows)
            }
        }
    };
}

/// Warning summaries and the strict/lenient contract, shared by list tests.
#[cfg(test)]
pub(crate) mod testing {
    use std::fmt::Debug;

    use sip_uri::WarningKind;

    use crate::diagnostic::{Field, ParseWarning, WarningCode};
    use crate::error::ParseError;
    use crate::HeaderParse;

    /// `(field, code, kind, position, entry)` of one warning.
    pub(crate) type Seen = (
        Field,
        WarningCode,
        WarningKind,
        Option<usize>,
        Option<usize>,
    );

    fn seen_in(warnings: &[ParseWarning]) -> Vec<Seen> {
        warnings
            .iter()
            .map(|w| (w.field, w.code, w.kind, w.position, w.entry))
            .collect()
    }

    /// Warnings lenient parsing of `raw` raises.
    pub(crate) fn seen<T: HeaderParse>(raw: &str) -> Vec<Seen> {
        seen_in(
            &T::parse_with_warnings(raw)
                .unwrap()
                .warnings,
        )
    }

    /// Lenient value and warnings, after checking strict parsing refuses.
    pub(crate) fn lenient<T>(raw: &str) -> (T, Vec<Seen>)
    where
        T: HeaderParse + Debug + PartialEq,
    {
        assert!(
            matches!(T::parse_strict(raw), Err(ParseError::NonConformant(_))),
            "{raw}"
        );
        let parsed = T::parse_with_warnings(raw).unwrap();
        assert_eq!(T::parse(raw).as_ref(), Ok(&parsed.value));
        let seen = seen_in(&parsed.warnings);
        (parsed.value, seen)
    }
}