@ -1,19 +1,19 @@
use super ::* ;
use ser ::text ::{ DnsTextData , DnsTextFormatter , next_field , quoted } ;
use ser ::text ::{ DnsTextData , DnsTextFormatter , DnsTextContext , next_field , quoted } ;
impl DnsName {
/// Parse text representation of a domain name
pub fn parse < ' a , O > ( value : & str , origin : O ) -> ::errors ::Result < Self >
where
O : IntoIterator < Item = DnsLabelRef < ' a > >
pub fn parse ( context : & DnsTextContext , value : & str ) -> Result < Self >
{
let raw = value . as_bytes ( ) ;
let mut name = DnsName ::new_root ( ) ;
if raw = = b" . " {
return Ok ( name ) ;
} else if raw = = b" @ " {
for l in origin . into_iter ( ) { name . push_back ( l ) ? ; }
return Ok ( name ) ;
match context . origin ( ) {
Some ( o ) = > return Ok ( o . clone ( ) ) ,
None = > bail ! ( "@ invalid without $ORIGIN" ) ,
}
}
ensure ! ( ! raw . is_empty ( ) , "invalid empty name" ) ;
let mut label = Vec ::new ( ) ;
@ -48,8 +48,16 @@ impl DnsName {
if ! label . is_empty ( ) {
// no trailing dot, relative name
// push last label
name . push_back ( DnsLabelRef ::new ( & label ) ? ) ? ;
for l in origin . into_iter ( ) { name . push_back ( l ) ? ; }
match context . origin ( ) {
Some ( o ) = > {
for l in o { name . push_back ( l ) ? ; }
} ,
None = > bail ! ( "missing trailing dot without $ORIGIN" ) ,
}
}
Ok ( name )
@ -57,9 +65,9 @@ impl DnsName {
}
impl DnsTextData for DnsName {
fn dns_parse ( data : & mut & str ) -> ::errors :: Result < Self > {
fn dns_parse ( context : & DnsTextContext , data : & mut & str ) -> Result < Self > {
let field = next_field ( data ) ? ;
DnsName ::parse ( field , & DnsName ::new_root ( ) )
DnsName ::parse ( context , field )
}
fn dns_format ( & self , f : & mut DnsTextFormatter ) -> fmt ::Result {
@ -69,18 +77,16 @@ impl DnsTextData for DnsName {
impl DnsCompressedName {
/// Parse text representation of a domain name
pub fn parse < ' a , O > ( value : & str , origin : O ) -> ::errors ::Result < Self >
where
O : IntoIterator < Item = DnsLabelRef < ' a > >
pub fn parse ( context : & DnsTextContext , value : & str ) -> Result < Self >
{
Ok ( DnsCompressedName ( DnsName ::parse ( value , origin ) ? ) )
Ok ( DnsCompressedName ( DnsName ::parse ( context , value ) ? ) )
}
}
impl DnsTextData for DnsCompressedName {
fn dns_parse ( data : & mut & str ) -> ::errors :: Result < Self > {
fn dns_parse ( context : & DnsTextContext , data : & mut & str ) -> Result < Self > {
let field = next_field ( data ) ? ;
DnsCompressedName ::parse ( field , & DnsName ::new_root ( ) )
DnsCompressedName ::parse ( context , field )
}
fn dns_format ( & self , f : & mut DnsTextFormatter ) -> fmt ::Result {