Skip to main content

FieldType

Trait FieldType 

Source
pub trait FieldType<'a>: Sized {
    type Combination: Combination;

    const INVALID: InvalidValue = InvalidValue::Error;

    // Required method
    fn parse(value: &'a [u8]) -> Result<Self, FieldValueError>;
}
Expand description

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)
    }
}

Provided Associated Constants§

Source

const INVALID: InvalidValue = InvalidValue::Error

What an invalid value means, unless a declaration overrides it with invalid = "...". A required declaration always treats an invalid value as an error.

Required Associated Types§

Source

type Combination: Combination

One of the markers in combination: Singleton, List, or Separate.

Required Methods§

Source

fn parse(value: &'a [u8]) -> Result<Self, FieldValueError>

Parse one instance, or one element of a list, with surrounding spaces and tabs already removed.

§Errors

A FieldValueError describing why the value is invalid, without repeating it.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§