sip-header 1.0.0-beta.1

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
//! The error every header-value parser returns.

use std::fmt;

use crate::diagnostic::{write_location, Field, ParseWarning, Parsed};
use crate::span::{Relocation, Span};
use sip_header_catalog::RowError;

impl<T> Parsed<T> {
    /// The value, or the first warning as [`ParseError::NonConformant`].
    pub fn into_strict(self) -> Result<T, ParseError> {
        match self
            .warnings
            .first()
        {
            Some(w) => Err(ParseError::NonConformant(*w)),
            None => Ok(self.value),
        }
    }
}

/// Error returned by every header-value parser in this crate.
///
/// Lenient parsing fails only when the input yields no usable value;
/// strict parsing also returns the first grammar breach as
/// [`NonConformant`](ParseError::NonConformant). Display names the layer
/// that failed and never quotes the input; a lower layer's error comes
/// through [`source`](std::error::Error::source).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseError {
    /// The input's structure leaves no value to return.
    Malformed(Fault),
    /// A URI the header carries could not be parsed at all.
    Uri(UriFault),
    /// The lookup store could not frame the header's rows.
    Row(RowError),
    /// A strict parse met a grammar breach.
    NonConformant(ParseWarning),
}

impl ParseError {
    pub(crate) fn malformed(field: Field, code: FaultCode, position: Option<usize>) -> Self {
        ParseError::Malformed(Fault {
            position,
            ..Fault::new(field, code)
        })
    }

    /// `field` is empty where the grammar requires content.
    pub(crate) fn empty(field: Field) -> Self {
        ParseError::malformed(field, FaultCode::Empty, None)
    }

    /// sip-uri refused the `len` bytes at `position`.
    pub(crate) fn uri(source: sip_uri::ParseError, position: usize, len: usize) -> Self {
        ParseError::Uri(UriFault {
            position: Some(position),
            len,
            row: None,
            entry: None,
            source,
        })
    }

    /// The text this error points at, in the row it names: a
    /// [`Malformed`](Self::Malformed) fault from its position to its end, a
    /// [`Uri`](Self::Uri) the URI refused, a
    /// [`NonConformant`](Self::NonConformant) warning its
    /// [`span`](ParseWarning::span), else its point as an empty range.
    /// `None` without a position, and for a row error.
    pub fn span(&self) -> Option<Span> {
        match self {
            ParseError::Malformed(fault) => fault
                .position
                .map(|start| {
                    let end = fault
                        .end
                        .unwrap_or(start)
                        .max(start);
                    Span::in_row(fault.row, start..end)
                }),
            ParseError::Uri(fault) => fault.span(),
            ParseError::NonConformant(w) => w
                .span()
                .or_else(|| {
                    w.position
                        .map(|p| Span::in_row(w.row, p..p))
                }),
            ParseError::Row(_) => None,
        }
    }

    /// Detach the error from any list entry.
    pub(crate) fn without_entry(self) -> Self {
        match self {
            ParseError::Malformed(fault) => ParseError::Malformed(Fault {
                entry: None,
                ..fault
            }),
            ParseError::Uri(fault) => ParseError::Uri(UriFault {
                entry: None,
                ..fault
            }),
            ParseError::NonConformant(w) => {
                ParseError::NonConformant(ParseWarning { entry: None, ..w })
            }
            ParseError::Row(e) => ParseError::Row(e),
        }
    }

    /// Point an error that names no position at `at`.
    pub(crate) fn placed(self, at: usize) -> Self {
        match self {
            ParseError::Malformed(fault)
                if fault
                    .position
                    .is_none() =>
            {
                ParseError::Malformed(fault.at(at))
            }
            ParseError::NonConformant(w)
                if w.position
                    .is_none() =>
            {
                ParseError::NonConformant(w.at(at))
            }
            e => e,
        }
    }

    /// Drop the position and row, for input that was decoded before parsing.
    pub(crate) fn without_position(self) -> Self {
        match self {
            ParseError::Malformed(fault) => ParseError::Malformed(Fault {
                position: None,
                end: None,
                row: None,
                ..fault
            }),
            ParseError::Uri(fault) => ParseError::Uri(UriFault {
                position: None,
                row: None,
                ..fault
            }),
            ParseError::NonConformant(w) => ParseError::NonConformant(ParseWarning {
                position: None,
                row: None,
                span: None,
                ..w
            }),
            ParseError::Row(e) => ParseError::Row(e),
        }
    }

    /// Move the error to where `to` places the text it was found in.
    pub(crate) fn relocate(self, to: &Relocation<'_>) -> Self {
        match self {
            ParseError::Malformed(fault) => ParseError::Malformed(Fault {
                position: fault
                    .position
                    .and_then(|p| to.start(p)),
                end: fault
                    .end
                    .and_then(|p| to.end(p)),
                row: to
                    .row()
                    .or(fault.row),
                ..fault
            }),
            ParseError::Uri(fault) => {
                let span = fault
                    .span()
                    .and_then(|s| s.relocate(to));
                ParseError::Uri(UriFault {
                    position: span.map(|s| {
                        s.range()
                            .start
                    }),
                    len: span.map_or(0, |s| {
                        s.range()
                            .len()
                    }),
                    row: to
                        .row()
                        .or(fault.row),
                    ..fault
                })
            }
            ParseError::NonConformant(w) => ParseError::NonConformant(w.relocate(to)),
            ParseError::Row(e) => ParseError::Row(e),
        }
    }

    /// Place this error in row `row`.
    pub(crate) fn in_row(self, row: usize) -> Self {
        self.relocate(&Relocation::shift(Some(0), Some(row)))
    }

    /// Attribute this error to list entry `index`.
    pub(crate) fn in_entry(self, index: usize) -> Self {
        match self {
            ParseError::Malformed(fault) => ParseError::Malformed(fault.in_entry(index)),
            ParseError::Uri(fault) => ParseError::Uri(fault.in_entry(index)),
            ParseError::NonConformant(w) => ParseError::NonConformant(w.in_entry(index)),
            ParseError::Row(e) => ParseError::Row(e),
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseError::Malformed(fault) => write!(f, "malformed header value: {fault}"),
            ParseError::Uri(fault) => fault.fmt(f),
            ParseError::Row(_) => f.write_str("row error"),
            ParseError::NonConformant(w) => write!(f, "non-conformant header value: {w}"),
        }
    }
}

impl From<RowError> for ParseError {
    fn from(e: RowError) -> Self {
        ParseError::Row(e)
    }
}

impl std::error::Error for ParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            // Skips the fault, whose Display this error already printed.
            ParseError::Uri(fault) => std::error::Error::source(fault),
            ParseError::Row(e) => Some(e),
            ParseError::Malformed(_) | ParseError::NonConformant(_) => None,
        }
    }
}

/// A URI the header carries that sip-uri could not parse.
///
/// sip-uri's error is the [`source`](std::error::Error::source).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UriFault {
    position: Option<usize>,
    /// Bytes of the URI from `position`.
    len: usize,
    row: Option<usize>,
    entry: Option<usize>,
    source: sip_uri::ParseError,
}

impl UriFault {
    pub(crate) fn in_entry(self, index: usize) -> Self {
        UriFault {
            entry: Some(index),
            ..self
        }
    }

    /// Byte offset of the URI in its row: the string handed to `parse`, or
    /// the row or entry named by [`row`](Self::row).
    pub fn position(&self) -> Option<usize> {
        self.position
    }

    /// Index of the row holding the URI, among the rows or entries a value
    /// was built from; `None` for a value parsed from one string.
    pub fn row(&self) -> Option<usize> {
        self.row
    }

    /// Index of the list entry holding the URI, for list-valued headers.
    pub fn entry(&self) -> Option<usize> {
        self.entry
    }

    /// sip-uri's error for the URI, the one
    /// [`source`](std::error::Error::source) returns.
    pub fn cause(&self) -> &sip_uri::ParseError {
        &self.source
    }

    /// The URI's text in its row; `None` without a position.
    pub fn span(&self) -> Option<Span> {
        self.position
            .map(|start| Span::in_row(self.row, start..start + self.len))
    }
}

impl fmt::Display for UriFault {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("invalid URI")?;
        write_location(f, self.position, self.row, self.entry)
    }
}

impl std::error::Error for UriFault {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.source)
    }
}

/// Why a header value yields nothing to return.
///
/// Names where the failure is, never what text it was.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Fault {
    /// Part of the header value that failed.
    pub field: Field,
    /// What is wrong with it.
    pub code: FaultCode,
    /// Byte offset into the row that failed, when one points at the failure:
    /// the string handed to `parse`, or the row or entry named by
    /// [`row`](Self::row).
    pub position: Option<usize>,
    /// Byte offset in the same row where the failing text ends, when the
    /// fault covers more than a point.
    pub end: Option<usize>,
    /// Index of the row that failed, among the rows or entries a value was
    /// built from; `None` for a value parsed from one string.
    pub row: Option<usize>,
    /// Index of the list entry that failed, for list-valued headers.
    pub entry: Option<usize>,
}

impl Fault {
    /// A fault in `field`, with no position or entry.
    ///
    /// For a layer outside this crate that decodes header values:
    ///
    /// ```
    /// use sip_header::{Fault, FaultCode, Field, ParseError};
    ///
    /// let e = ParseError::Malformed(
    ///     Fault::new(Field::Value, FaultCode::NotUtf8).at(12).in_entry(2),
    /// );
    /// assert_eq!(e.to_string(), "malformed header value: value: not-utf8 at byte 12 in entry 2");
    /// ```
    pub fn new(field: Field, code: FaultCode) -> Self {
        Fault {
            field,
            code,
            position: None,
            end: None,
            row: None,
            entry: None,
        }
    }

    /// Point the fault at byte `position`.
    pub fn at(mut self, position: usize) -> Self {
        self.position = Some(position);
        self
    }

    /// End the failing text at byte `end`.
    pub fn to(mut self, end: usize) -> Self {
        self.end = Some(end);
        self
    }

    /// Place the fault in row `row`.
    pub fn in_row(mut self, row: usize) -> Self {
        self.row = Some(row);
        self
    }

    /// Attribute the fault to list entry `index`.
    pub fn in_entry(mut self, index: usize) -> Self {
        self.entry = Some(index);
        self
    }
}

impl fmt::Display for Fault {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.field, self.code)?;
        write_location(f, self.position, self.row, self.entry)
    }
}

/// What a [`Fault`] found.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FaultCode {
    /// A part that is empty where the grammar requires content.
    Empty,
    /// A part the grammar requires is absent.
    Missing,
    /// A part that may appear once appears again.
    Duplicate,
    /// A quote or bracket that never closes.
    Unterminated,
    /// A character the part cannot hold.
    InvalidChar,
    /// A number that does not parse or fit.
    InvalidNumber,
    /// Text that admits more than one reading, none of them safe to pick.
    Ambiguous,
    /// A part where the grammar does not allow it.
    Misplaced,
    /// Percent-decoded octets that are not UTF-8.
    NotUtf8,
    /// A value whose wire form parses as a different value.
    Unrepresentable,
    /// A header the requested type does not hold, as
    /// [`TypedHeader::HEADERS`](crate::TypedHeader::HEADERS) lists them.
    WrongHeader,
}

impl FaultCode {
    /// Stable kebab-case name, for logs and machine consumers.
    pub fn as_str(self) -> &'static str {
        match self {
            FaultCode::Empty => "empty",
            FaultCode::Missing => "missing",
            FaultCode::Duplicate => "duplicate",
            FaultCode::Unterminated => "unterminated",
            FaultCode::InvalidChar => "invalid-char",
            FaultCode::InvalidNumber => "invalid-number",
            FaultCode::Ambiguous => "ambiguous",
            FaultCode::Misplaced => "misplaced",
            FaultCode::NotUtf8 => "not-utf8",
            FaultCode::Unrepresentable => "unrepresentable",
            FaultCode::WrongHeader => "wrong-header",
        }
    }
}

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

#[cfg(test)]
mod tests {
    use std::error::Error;

    use super::*;
    use crate::diagnostic::WarningCode;

    #[test]
    fn into_strict_returns_first_warning() {
        let w = ParseWarning::new(Field::Addr, WarningCode::TrailingContent).at(1);
        let p = Parsed::new((), vec![w]);
        assert_eq!(p.into_strict(), Err(ParseError::NonConformant(w)));
        assert_eq!(Parsed::new(5, Vec::new()).into_strict(), Ok(5));
    }

    #[test]
    fn uri_error_keeps_source() {
        let e = ParseError::uri(sip_uri::ParseError::SchemeMismatch, 4, 3).in_entry(1);
        let ParseError::Uri(fault) = &e else {
            panic!("not Uri");
        };
        assert_eq!((fault.position(), fault.entry()), (Some(4), Some(1)));
        assert_eq!(fault.cause(), &sip_uri::ParseError::SchemeMismatch);
        let cause = Some(sip_uri::ParseError::SchemeMismatch.to_string());
        assert_eq!(
            fault
                .source()
                .map(ToString::to_string),
            cause
        );
        assert_eq!(
            e.source()
                .map(ToString::to_string),
            cause
        );
        assert_eq!(e.to_string(), "invalid URI at byte 4 in entry 1");
        assert_eq!(fault.to_string(), e.to_string());
        assert_eq!(
            ParseError::uri(sip_uri::ParseError::SchemeMismatch, 4, 3)
                .without_position()
                .to_string(),
            "invalid URI"
        );
    }

    #[test]
    fn row_error_display_names_the_layer() {
        let row = RowError::malformed().in_row(3);
        let e = ParseError::from(row.clone());
        assert_eq!(e.to_string(), "row error");
        assert_eq!(
            e.source()
                .map(ToString::to_string),
            Some(row.to_string())
        );
    }

    #[test]
    fn empty_is_a_fault() {
        let e = ParseError::empty(Field::CallId).in_entry(2);
        assert_eq!(
            e,
            ParseError::Malformed(Fault::new(Field::CallId, FaultCode::Empty).in_entry(2))
        );
        assert_eq!(
            e.to_string(),
            "malformed header value: call-id: empty in entry 2"
        );
        assert!(e
            .source()
            .is_none());
    }

    #[test]
    fn fault_display_names_field_and_code() {
        let e = ParseError::malformed(Field::Tag, FaultCode::Duplicate, Some(9));
        assert_eq!(
            e.to_string(),
            "malformed header value: tag: duplicate at byte 9"
        );
    }
}