@@ -53,6 +53,34 @@ impl Component {
53
53
. cloned ( )
54
54
}
55
55
56
+ /// Returns a `Component` from the supplied `Component` name, plugin, or executable
57
+ ///
58
+ /// # Arguments
59
+ ///
60
+ /// * `name` - The name of the component, plugin, or executable
61
+ ///
62
+ /// # Examples
63
+ ///
64
+ /// ```rust
65
+ /// use component::Component;
66
+ ///
67
+ /// let forc = Component::resolve_from_name("forc").unwrap();
68
+ /// let publishable = Component::resolve_from_name("fuel-core").unwrap();
69
+ /// let plugin = Component::resolve_from_name("forc-fmt").unwrap();
70
+ /// let executable = Component::resolve_from_name("forc-run").unwrap();
71
+ /// ```
72
+ pub fn resolve_from_name ( name : & str ) -> Option < Component > {
73
+ Components :: collect ( ) . ok ( ) . and_then ( |components| {
74
+ components. component . get ( name) . cloned ( ) . or_else ( || {
75
+ components
76
+ . component
77
+ . values ( )
78
+ . find ( |comp| comp. executables . contains ( & name. to_string ( ) ) )
79
+ . cloned ( )
80
+ } )
81
+ } )
82
+ }
83
+
56
84
pub fn is_default_forc_plugin ( name : & str ) -> bool {
57
85
( Self :: from_name ( FORC )
58
86
. expect ( "there must always be a `forc` component" )
@@ -213,12 +241,34 @@ impl Components {
213
241
Ok ( executables)
214
242
}
215
243
216
- pub fn is_distributed_by_forc ( plugin_name : & str ) -> bool {
217
- let components = Self :: from_toml ( COMPONENTS_TOML ) . expect ( "Failed to parse components toml" ) ;
218
- if let Some ( forc) = components. component . get ( FORC ) {
219
- return forc. executables . contains ( & plugin_name. to_string ( ) ) ;
220
- } ;
221
- false
244
+ /// Tests if the supplied `Component` name, plugin, or executable is
245
+ /// distributed by forc
246
+ ///
247
+ /// # Arguments
248
+ ///
249
+ /// * `name` - The name of the `Component`, plugin, or executable
250
+ ///
251
+ /// # Examples
252
+ ///
253
+ /// ```rust
254
+ /// use component::Components;
255
+ ///
256
+ /// assert!(Components::is_distributed_by_forc("forc"));
257
+ /// assert!(!Components::is_distributed_by_forc("fuel-core"));
258
+ /// assert!(Components::is_distributed_by_forc("forc-fmt"));
259
+ /// assert!(Components::is_distributed_by_forc("forc-run"));
260
+ /// ```
261
+ pub fn is_distributed_by_forc ( name : & str ) -> bool {
262
+ match name {
263
+ FORC => true ,
264
+ _ => Component :: from_name ( FORC )
265
+ . ok ( )
266
+ . and_then ( |forc| {
267
+ Component :: resolve_from_name ( name)
268
+ . map ( |component| Component :: is_in_same_distribution ( & forc, & component) )
269
+ } )
270
+ . unwrap_or ( false ) ,
271
+ }
222
272
}
223
273
}
224
274
@@ -311,7 +361,7 @@ mod tests {
311
361
}
312
362
313
363
#[ test]
314
- #[ should_panic] // TODO: #654 will fix this
364
+ #[ should_panic] // This will fail as long as some executables are not plugins
315
365
fn test_from_name_executables ( ) {
316
366
for executable in & Components :: collect_plugin_executables ( ) . unwrap ( ) {
317
367
let component = Component :: from_name ( executable) . unwrap ( ) ;
@@ -324,6 +374,55 @@ mod tests {
324
374
}
325
375
}
326
376
377
+ #[ test]
378
+ fn test_resolve_from_name_forc ( ) {
379
+ let component = Component :: resolve_from_name ( FORC ) . unwrap ( ) ;
380
+ assert_eq ! ( component. name, FORC , "forc is a publishable component" ) ;
381
+ }
382
+
383
+ #[ test]
384
+ fn test_resolve_from_name_publishable ( ) {
385
+ for publishable in Components :: collect_publishables ( ) . unwrap ( ) {
386
+ let component = Component :: resolve_from_name ( & publishable. name ) . unwrap ( ) ;
387
+ assert_eq ! ( component. name, publishable. name) ;
388
+ }
389
+ }
390
+
391
+ #[ test]
392
+ fn test_resolve_from_name_plugin ( ) {
393
+ for plugin in Components :: collect_plugins ( ) . unwrap ( ) {
394
+ let component = Component :: resolve_from_name ( & plugin. name ) . unwrap ( ) ;
395
+ assert_eq ! ( component. name, plugin. name) ;
396
+ }
397
+ }
398
+
399
+ #[ test]
400
+ fn test_resolve_from_name_from_executable ( ) {
401
+ let executables = Components :: collect_plugin_executables ( ) . unwrap ( ) ;
402
+
403
+ for executable in & executables {
404
+ let component = Component :: resolve_from_name ( executable) . unwrap ( ) ;
405
+
406
+ if component. executables . len ( ) == 1 {
407
+ assert_eq ! ( component. name, * executable) ;
408
+ } else {
409
+ assert ! ( component. executables. contains( executable) ) ;
410
+ }
411
+ }
412
+ }
413
+
414
+ #[ test]
415
+ fn test_resolve_from_name_nonexistent ( ) {
416
+ assert ! ( Component :: resolve_from_name( "nonexistent-component" ) . is_none( ) ) ;
417
+ }
418
+
419
+ #[ test]
420
+ fn test_resolve_from_name_case_sensitivity ( ) {
421
+ let original = Component :: resolve_from_name ( "forc" ) ;
422
+ let uppercase = Component :: resolve_from_name ( "FORC" ) ;
423
+ assert_ne ! ( original, uppercase) ;
424
+ }
425
+
327
426
#[ test]
328
427
fn test_is_distributed_by_forc_forc ( ) {
329
428
assert ! (
@@ -341,7 +440,6 @@ mod tests {
341
440
}
342
441
343
442
#[ test]
344
- #[ should_panic] // TODO: #654 will fix this
345
443
fn test_is_distributed_by_forc_plugins ( ) {
346
444
for plugin in Components :: collect_plugins ( ) . unwrap ( ) {
347
445
let component = Component :: from_name ( & plugin. name ) . unwrap ( ) ;
@@ -350,7 +448,6 @@ mod tests {
350
448
}
351
449
352
450
#[ test]
353
- #[ should_panic] // TODO: #654 will fix this
354
451
fn test_is_distributed_by_forc_executables ( ) {
355
452
for executable in Components :: collect_plugin_executables ( ) . unwrap ( ) {
356
453
let components = Components :: collect ( ) . unwrap ( ) ;
0 commit comments