11use serde:: { Deserialize , Serialize } ;
22use std:: {
3- collections:: HashMap ,
3+ collections:: HashSet ,
44 fmt:: { Display , Formatter } ,
55} ;
66
@@ -12,15 +12,15 @@ pub enum PeerKind {
1212
1313impl Default for PeerKind {
1414 fn default ( ) -> Self {
15- PeerKind :: HostName ( "default" . to_owned ( ) )
15+ Self :: HostName ( "default" . to_owned ( ) )
1616 }
1717}
1818
1919impl Display for PeerKind {
2020 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
2121 match & self {
22- PeerKind :: DNSName ( d) => write ! ( f, "{d}" ) ,
23- PeerKind :: HostName ( h) => write ! ( f, "{h}" ) ,
22+ Self :: DNSName ( d) => write ! ( f, "{d}" ) ,
23+ Self :: HostName ( h) => write ! ( f, "{h}" ) ,
2424 }
2525 }
2626}
@@ -62,50 +62,55 @@ pub fn has_suffix(name: &str, suffix: &str) -> bool {
6262pub fn trim_suffix ( name : & str , suffix : & str ) -> String {
6363 let mut new_name = name;
6464 if has_suffix ( name, suffix) {
65- new_name = new_name. trim_end_matches ( '.' ) ;
6665 let suffix = suffix. trim_start_matches ( '.' ) . trim_end_matches ( '.' ) ;
66+ new_name = new_name. trim_end_matches ( '.' ) ;
6767 new_name = new_name. trim_end_matches ( suffix) ;
6868 }
6969 new_name. trim_end_matches ( '.' ) . to_string ( )
7070}
7171
7272pub fn sanitize_hostname ( hostname : & str ) -> String {
73- const MAX_LABEL_LEN : usize = 63 ;
74- let mut sb = "" . to_string ( ) ;
73+ const MAX_LABEL_LENGTH : usize = 63 ;
74+
75+ // Trim suffixes
7576 let hostname = hostname
7677 . trim_end_matches ( ".local" )
7778 . trim_end_matches ( ".localdomain" )
7879 . trim_end_matches ( ".lan" ) ;
79- let mut start = 0 ;
80- let mut end = hostname. len ( ) ;
81- if end > MAX_LABEL_LEN {
82- end = MAX_LABEL_LEN ;
83- }
84- let mut chars = hostname. chars ( ) ;
85- while start < end {
86- if chars. nth ( start) . unwrap ( ) . is_alphanumeric ( ) {
87- break ;
88- }
89- start += 1 ;
90- }
91- while start < end {
92- if chars. nth ( end - 1 ) . unwrap ( ) . is_alphanumeric ( ) {
93- break ;
94- }
95- end -= 1 ;
96- }
97- let seperators: HashMap < char , bool > =
98- HashMap :: from ( [ ( ' ' , true ) , ( '.' , true ) , ( '@' , true ) , ( '_' , true ) ] ) ;
9980
100- let mut chars = hostname. chars ( ) ;
101- for i in start..end - 1 {
102- let boundary = ( i == start) || ( i == end - 1 ) ;
103- let chari = chars. nth ( i) . unwrap ( ) ;
104- if !boundary && seperators[ & chari] {
105- sb. push ( '-' ) ;
106- } else if chari. is_alphanumeric ( ) || chari == '-' {
107- sb. push ( chari. to_ascii_lowercase ( ) )
108- }
81+ // Find the first/last alphanumeric characters
82+ let start = hostname. find ( |c : char | c. is_alphanumeric ( ) ) . unwrap_or ( 0 ) ;
83+ let end = hostname
84+ . rfind ( |c : char | c. is_alphanumeric ( ) )
85+ . map_or ( 0 , |e| e + 1 ) ;
86+
87+ let separators: HashSet < char > = [ ' ' , '.' , '@' , '_' ] . into ( ) ;
88+
89+ let mut sanitized: String = hostname[ start..end]
90+ . chars ( )
91+ . enumerate ( )
92+ . map ( |( index, char) | {
93+ let boundary = ( index == 0 ) || ( index == end - start - 1 ) ;
94+ if !boundary && separators. contains ( & char) {
95+ '-'
96+ } else if char. is_alphanumeric ( ) || char == '-' {
97+ char. to_ascii_lowercase ( )
98+ } else {
99+ char
100+ }
101+ } )
102+ . collect ( ) ;
103+
104+ sanitized. truncate ( MAX_LABEL_LENGTH ) ;
105+ sanitized
106+ }
107+
108+ pub fn set_display_name ( m : & mut Machine , dns_suffix : & str ) {
109+ let dns_name = trim_suffix ( & m. dns_name , dns_suffix) ;
110+
111+ if dns_name. is_empty ( ) {
112+ m. display_name = PeerKind :: DNSName ( sanitize_hostname ( & m. hostname ) ) ;
113+ } else {
114+ m. display_name = PeerKind :: HostName ( dns_name) ;
109115 }
110- sb
111116}
0 commit comments