@@ -915,67 +915,72 @@ impl Readable for String {
915
915
}
916
916
}
917
917
918
- /// Represents a printable ASCII string whose length can be represented by a single byte.
918
+ /// Represents a hostname for serialization purposes.
919
+ /// Only the character set and length will be validated.
920
+ /// The character set consists of ASCII alphanumeric characters, hyphens, and periods.
921
+ /// Its length is guaranteed to be representable by a single byte.
919
922
/// This serialization is used by BOLT 7 hostnames.
920
923
#[ derive( Clone , Debug , PartialEq ) ]
921
- pub struct ShortAsciiString ( String ) ;
922
- impl ShortAsciiString {
923
- /// Returns the length of the short ASCII string .
924
+ pub struct Hostname ( String ) ;
925
+ impl Hostname {
926
+ /// Returns the length of the hostname .
924
927
pub fn len ( & self ) -> u8 {
925
928
( & self . 0 ) . len ( ) as u8
926
929
}
927
930
}
928
- impl Deref for ShortAsciiString {
931
+ impl Deref for Hostname {
929
932
type Target = String ;
930
933
931
934
fn deref ( & self ) -> & Self :: Target {
932
935
& self . 0
933
936
}
934
937
}
935
- impl From < ShortAsciiString > for String {
936
- fn from ( short_s : ShortAsciiString ) -> Self {
938
+ impl From < Hostname > for String {
939
+ fn from ( short_s : Hostname ) -> Self {
937
940
short_s. 0
938
941
}
939
942
}
940
- impl TryFrom < Vec < u8 > > for ShortAsciiString {
943
+ impl TryFrom < Vec < u8 > > for Hostname {
941
944
type Error = ( ) ;
942
945
943
946
fn try_from ( bytes : Vec < u8 > ) -> Result < Self , Self :: Error > {
944
947
if let Ok ( s) = String :: from_utf8 ( bytes) {
945
- ShortAsciiString :: try_from ( s)
948
+ Hostname :: try_from ( s)
946
949
} else {
947
950
Err ( ( ) )
948
951
}
949
952
}
950
953
}
951
- impl TryFrom < String > for ShortAsciiString {
954
+ impl TryFrom < String > for Hostname {
952
955
type Error = ( ) ;
953
956
954
957
fn try_from ( s : String ) -> Result < Self , Self :: Error > {
955
958
if s. len ( ) <= 255 && s. chars ( ) . all ( |c|
956
- c. is_ascii ( ) && !c. is_ascii_control ( )
959
+ c. is_ascii_alphanumeric ( ) ||
960
+ c == '.' ||
961
+ c == '-'
957
962
) {
958
- Ok ( ShortAsciiString ( s) )
963
+ Ok ( Hostname ( s) )
959
964
} else {
960
965
Err ( ( ) )
961
966
}
962
967
}
963
968
}
964
- impl Writeable for ShortAsciiString {
969
+ impl Writeable for Hostname {
965
970
#[ inline]
966
971
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
967
972
self . len ( ) . write ( w) ?;
968
973
w. write_all ( self . as_bytes ( ) )
969
974
}
970
975
}
971
- impl Readable for ShortAsciiString {
976
+ impl Readable for Hostname {
972
977
#[ inline]
973
- fn read < R : Read > ( r : & mut R ) -> Result < ShortAsciiString , DecodeError > {
978
+ fn read < R : Read > ( r : & mut R ) -> Result < Hostname , DecodeError > {
974
979
let len: u8 = Readable :: read ( r) ?;
975
980
let mut vec = Vec :: with_capacity ( len. into ( ) ) ;
976
981
vec. resize ( len. into ( ) , 0 ) ;
977
982
r. read_exact ( & mut vec) ?;
978
- ShortAsciiString :: try_from ( vec) . map_err ( |_| DecodeError :: InvalidValue )
983
+ Hostname :: try_from ( vec) . map_err ( |_| DecodeError :: InvalidValue )
979
984
}
980
985
}
981
986
@@ -998,23 +1003,25 @@ impl Readable for Duration {
998
1003
#[ cfg( test) ]
999
1004
mod tests {
1000
1005
use core:: convert:: TryFrom ;
1001
- use util:: ser:: { Readable , ShortAsciiString , Writeable } ;
1006
+ use util:: ser:: { Readable , Hostname , Writeable } ;
1002
1007
1003
1008
#[ test]
1004
- fn short_ascii_string_conversion ( ) {
1005
- assert_eq ! ( ShortAsciiString :: try_from( String :: from( "test" ) ) . unwrap( ) . as_str( ) , "test" ) ;
1009
+ fn hostname_conversion ( ) {
1010
+ assert_eq ! ( Hostname :: try_from( String :: from( "a- test.com " ) ) . unwrap( ) . as_str( ) , "a- test.com " ) ;
1006
1011
1007
- assert ! ( ShortAsciiString :: try_from( String :: from( "⚡" ) ) . is_err( ) ) ;
1012
+ assert ! ( Hostname :: try_from( String :: from( "\" " ) ) . is_err( ) ) ;
1013
+ assert ! ( Hostname :: try_from( String :: from( "$" ) ) . is_err( ) ) ;
1014
+ assert ! ( Hostname :: try_from( String :: from( "⚡" ) ) . is_err( ) ) ;
1008
1015
let mut large_vec = Vec :: with_capacity ( 256 ) ;
1009
1016
large_vec. resize ( 256 , b'A' ) ;
1010
- assert ! ( ShortAsciiString :: try_from( String :: from_utf8( large_vec) . unwrap( ) ) . is_err( ) ) ;
1017
+ assert ! ( Hostname :: try_from( String :: from_utf8( large_vec) . unwrap( ) ) . is_err( ) ) ;
1011
1018
}
1012
1019
1013
1020
#[ test]
1014
- fn short_ascii_string_serialization ( ) {
1015
- let short_s = ShortAsciiString :: try_from ( String :: from ( "test" ) ) . unwrap ( ) ;
1021
+ fn hostname_serialization ( ) {
1022
+ let short_s = Hostname :: try_from ( String :: from ( "test" ) ) . unwrap ( ) ;
1016
1023
let mut buf: Vec < u8 > = Vec :: new ( ) ;
1017
1024
short_s. write ( & mut buf) . unwrap ( ) ;
1018
- assert_eq ! ( ShortAsciiString :: read( & mut buf. as_slice( ) ) . unwrap( ) . as_str( ) , "test" ) ;
1025
+ assert_eq ! ( Hostname :: read( & mut buf. as_slice( ) ) . unwrap( ) . as_str( ) , "test" ) ;
1019
1026
}
1020
1027
}
0 commit comments