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
// Released under the MIT License.
// Copyright, 2026, by Samuel Williams.
use crateFieldValueError;
use crateCombination;
/// What an invalid value means for a section.
/// One kind of field: how its instances combine, how one value parses, and what an invalid value means.
///
/// The lifetime is that of the field values, so a field type can borrow from them. A field type that owns its value implements `FieldType<'_>`:
///
/// ```
/// use base_message::{FieldType, FieldValueError, ascii_text, combination::Singleton};
///
/// struct ContentLength(u64);
///
/// impl FieldType<'_> for ContentLength {
/// type Combination = Singleton;
///
/// fn parse(value: &[u8]) -> Result<Self, FieldValueError> {
/// let text = ascii_text(value)?;
/// if text.is_empty() || !text.bytes().all(|byte| byte.is_ascii_digit()) {
/// return Err(FieldValueError::new("not a decimal length"));
/// }
/// text.parse().map(Self).map_err(|_| FieldValueError::new("length too large"))
/// }
/// }
///
/// /// A field type that borrows its value, rather than copying it.
/// struct Host<'a>(&'a str);
///
/// impl<'a> FieldType<'a> for Host<'a> {
/// type Combination = Singleton;
///
/// fn parse(value: &'a [u8]) -> Result<Self, FieldValueError> {
/// ascii_text(value).map(Self)
/// }
/// }
/// ```