@@ -1118,6 +1118,23 @@ impl PathList {
1118
1118
)
1119
1119
}
1120
1120
1121
+ /// Create a `PathList` from a list of `PathBuf`s.
1122
+ ///
1123
+ /// # Arguments
1124
+ ///
1125
+ /// * `source` - The `PathBuf`s to use
1126
+ ///
1127
+ /// # Returns
1128
+ ///
1129
+ /// A `PathList` containing the `PathBuf`s
1130
+ pub fn from_iter < I > ( source : I ) -> Self
1131
+ where
1132
+ I : IntoIterator ,
1133
+ I :: Item : Into < PathBuf > ,
1134
+ {
1135
+ Self ( source. into_iter ( ) . map ( Into :: into) . collect ( ) )
1136
+ }
1137
+
1121
1138
/// Create a `PathList` by parsing a Strings containing paths separated by whitspaces.
1122
1139
///
1123
1140
/// # Arguments
@@ -1130,7 +1147,16 @@ impl PathList {
1130
1147
///
1131
1148
/// [`SnapshotFileErrorKind::FromSplitError`]: crate::error::SnapshotFileErrorKind::FromSplitError
1132
1149
pub fn from_string ( sources : & str ) -> RusticResult < Self > {
1133
- let sources = sources. split ( ' ' ) . collect :: < Vec < _ > > ( ) ;
1150
+ // TODO: we need to handle paths, that have a whitespace in them
1151
+ // they should be surrounded by single quotes
1152
+ let sources_trimmed = sources. trim ( ) ;
1153
+
1154
+ if sources_trimmed. is_empty ( ) {
1155
+ return Ok ( Self :: default ( ) ) ;
1156
+ }
1157
+
1158
+ // Split by whitespace
1159
+ let sources = sources_trimmed. split ( ' ' ) . collect :: < Vec < _ > > ( ) ;
1134
1160
1135
1161
Ok ( Self :: from_strings ( sources) )
1136
1162
}
@@ -1199,3 +1225,33 @@ impl PathList {
1199
1225
Self ( paths)
1200
1226
}
1201
1227
}
1228
+
1229
+ #[ cfg( test) ]
1230
+ mod tests {
1231
+
1232
+ use rstest:: rstest;
1233
+
1234
+ use super :: * ;
1235
+
1236
+ #[ rstest]
1237
+ #[ case( "/tmp /var/tmp" , PathList :: from_iter( vec![ PathBuf :: from( "/tmp" ) , PathBuf :: from( "/var/tmp" ) ] ) ) ]
1238
+ #[ case( " C:\\ Users\\ test_user\\ backup\\ myfiles\\ C:\\ backup\\ this\\ " , PathList :: from_iter( vec![ PathBuf :: from( "C:\\ Users\\ test_user\\ backup\\ myfiles\\ " ) , PathBuf :: from( "C:\\ backup\\ this\\ " ) ] ) ) ]
1239
+ #[ case( "' C:\\ Users\\ test_user\\ Back Up\\ myfiles\\ ' 'C:\\ backup\\ this\\ '" , PathList :: from_iter( vec![ PathBuf :: from( "C:\\ Users\\ test_user\\ Back Up\\ myfiles\\ " ) , PathBuf :: from( "C:\\ backup\\ this\\ " ) ] ) ) ]
1240
+ #[ case( "'/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App' '/Users/Alice/Library/Application Support/com.Bar-Corp.Foo-App'" , PathList :: from_iter( vec![ PathBuf :: from( "/Users/Alice/Library/Application Support/com.Foo-Corp.Bar-App" ) , PathBuf :: from( "/Users/Alice/Library/Application Support/com.Bar-Corp.Foo-App" ) ] ) ) ]
1241
+ fn test_pathlist_from_string_passes (
1242
+ #[ case] input : & str ,
1243
+ #[ case] output : PathList ,
1244
+ ) -> RusticResult < ( ) > {
1245
+ let pathlist = PathList :: from_string ( input) ?;
1246
+ assert_eq ! ( pathlist, output) ;
1247
+
1248
+ Ok ( ( ) )
1249
+ }
1250
+
1251
+ #[ rstest]
1252
+ #[ case( vec![ "/tmp" , "/var/tmp" ] , vec![ PathBuf :: from( "/tmp" ) , PathBuf :: from( "/var/tmp" ) ] ) ]
1253
+ fn test_pathlist_from_iter_passes ( #[ case] input : Vec < & str > , #[ case] output : Vec < PathBuf > ) {
1254
+ let pathlist = PathList :: from_iter ( input) ;
1255
+ assert_eq ! ( pathlist. paths( ) , output) ;
1256
+ }
1257
+ }
0 commit comments