@@ -36,31 +36,19 @@ impl ProcessId {
3636
3737impl std:: str:: FromStr for ProcessId {
3838 type Err = ProcessIdParseError ;
39- /// Attempts to parse a `ProcessId` from a string. To succeed, the string must contain
40- /// exactly 3 segments, separated by colons `:`. The segments must not contain colons.
39+ /// Attempts to parse a `ProcessId` from a string. The string must match the pattern
40+ /// of three segments containing only lowercase letters, numbers and hyphens, separated by colons.
4141 fn from_str ( input : & str ) -> Result < Self , ProcessIdParseError > {
42- let segments: Vec < & str > = input. split ( ':' ) . collect ( ) ;
43- if segments. len ( ) < 3 {
44- return Err ( ProcessIdParseError :: MissingField ) ;
45- } else if segments. len ( ) > 3 {
46- return Err ( ProcessIdParseError :: TooManyColons ) ;
47- }
48- let process_name = segments[ 0 ] . to_string ( ) ;
49- if process_name. is_empty ( ) {
50- return Err ( ProcessIdParseError :: MissingField ) ;
51- }
52- let package_name = segments[ 1 ] . to_string ( ) ;
53- if package_name. is_empty ( ) {
54- return Err ( ProcessIdParseError :: MissingField ) ;
55- }
56- let publisher_node = segments[ 2 ] . to_string ( ) ;
57- if publisher_node. is_empty ( ) {
58- return Err ( ProcessIdParseError :: MissingField ) ;
42+ let re = regex:: Regex :: new ( r"^[a-z0-9-]+:[a-z0-9-]+:[a-z0-9-]+$" ) . unwrap ( ) ;
43+ if !re. is_match ( input) {
44+ return Err ( ProcessIdParseError :: InvalidCharacter ) ;
5945 }
46+
47+ let segments: Vec < & str > = input. split ( ':' ) . collect ( ) ;
6048 Ok ( ProcessId {
61- process_name,
62- package_name,
63- publisher_node,
49+ process_name : segments [ 0 ] . to_string ( ) ,
50+ package_name : segments [ 1 ] . to_string ( ) ,
51+ publisher_node : segments [ 2 ] . to_string ( ) ,
6452 } )
6553 }
6654}
@@ -134,6 +122,7 @@ impl PartialEq<ProcessId> for &str {
134122pub enum ProcessIdParseError {
135123 TooManyColons ,
136124 MissingField ,
125+ InvalidCharacter ,
137126}
138127
139128impl std:: fmt:: Display for ProcessIdParseError {
@@ -144,6 +133,7 @@ impl std::fmt::Display for ProcessIdParseError {
144133 match self {
145134 ProcessIdParseError :: TooManyColons => "Too many colons" ,
146135 ProcessIdParseError :: MissingField => "Missing field" ,
136+ ProcessIdParseError :: InvalidCharacter => "Invalid character" ,
147137 }
148138 )
149139 }
@@ -154,6 +144,7 @@ impl std::error::Error for ProcessIdParseError {
154144 match self {
155145 ProcessIdParseError :: TooManyColons => "Too many colons" ,
156146 ProcessIdParseError :: MissingField => "Missing field" ,
147+ ProcessIdParseError :: InvalidCharacter => "Invalid character" ,
157148 }
158149 }
159150}
0 commit comments