rust-dnsbox/lib/dnsbox-base/src/common_types/uri.rs

37 lines
1.0 KiB
Rust

use bytes::Bytes;
use errors::*;
use ser::packet::{DnsPacketData, remaining_bytes};
use ser::text::*;
use std::fmt;
use std::io::Cursor;
/// A single quoted non-empty URL.
///
/// Actually shouldn't allow escapes (i.e. no backslash in the content);
/// but to make sure we can export and import any data we allow standard
/// escape mechanisms and even accept unquoted input.
///
/// No whitespace allowed, last field.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct UriText(Bytes);
impl DnsPacketData for UriText {
fn deserialize(data: &mut Cursor<Bytes>) -> Result<Self> {
let raw = remaining_bytes(data);
ensure!(!raw.is_empty(), "URI must not be empty");
Ok(UriText(raw))
}
}
impl DnsTextData for UriText {
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> ::errors::Result<Self> {
let raw = next_quoted_field(data)?;
ensure!(!raw.is_empty(), "URI must not be empty");
Ok(UriText(raw.into()))
}
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
write!(f, "{}", quote(&self.0))
}
}