use crate::__private::{IncomingField, IncomingName};
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct FieldRef<'a> {
position: usize,
name: &'a str,
value: &'a [u8],
}
impl<'a> FieldRef<'a> {
pub const fn new(position: usize, name: &'a str, value: &'a [u8]) -> Self {
Self {
position,
name,
value,
}
}
pub const fn position(&self) -> usize {
self.position
}
pub const fn name(&self) -> &'a str {
self.name
}
pub const fn value(&self) -> &'a [u8] {
self.value
}
}
impl fmt::Debug for FieldRef<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
debug_field(formatter, "FieldRef", self.position, self.name, self.value)
}
}
pub(crate) fn debug_field(
formatter: &mut fmt::Formatter<'_>,
type_name: &str,
position: usize,
name: &str,
value: &[u8],
) -> fmt::Result {
formatter
.debug_struct(type_name)
.field("position", &position)
.field("name", &name)
.field("value", &format_args!("<{} bytes>", value.len()))
.finish()
}
pub trait IntoField<'a, S> {
#[doc(hidden)]
fn into_field(self, index: usize) -> IncomingField<'a>;
}
impl<'a, S> IntoField<'a, S> for (&'a str, &'a [u8]) {
fn into_field(self, index: usize) -> IncomingField<'a> {
IncomingField {
position: index,
name: IncomingName::Text(self.0),
value: self.1,
}
}
}
impl<'a, S> IntoField<'a, S> for (&'a str, &'a str) {
fn into_field(self, index: usize) -> IncomingField<'a> {
IncomingField {
position: index,
name: IncomingName::Text(self.0),
value: self.1.as_bytes(),
}
}
}
impl<'a, S> IntoField<'a, S> for &(&'a str, &'a [u8]) {
fn into_field(self, index: usize) -> IncomingField<'a> {
IncomingField {
position: index,
name: IncomingName::Text(self.0),
value: self.1,
}
}
}
impl<'a, S> IntoField<'a, S> for FieldRef<'a> {
fn into_field(self, _index: usize) -> IncomingField<'a> {
IncomingField {
position: self.position,
name: IncomingName::Text(self.name),
value: self.value,
}
}
}
impl<'a, S> IntoField<'a, S> for &FieldRef<'a> {
fn into_field(self, index: usize) -> IncomingField<'a> {
<FieldRef<'a> as IntoField<'a, S>>::into_field(*self, index)
}
}