@@ -184,6 +184,16 @@ impl Packages {
184184 }
185185}
186186
187+ #[ derive( Debug , PartialEq , Eq ) ]
188+ pub enum LibRule {
189+ /// Include the library, fail if not present
190+ True ,
191+ /// Include the library if present
192+ Default ,
193+ /// Exclude the library
194+ False ,
195+ }
196+
187197#[ derive( Debug ) ]
188198pub enum FilterRule {
189199 All ,
@@ -198,7 +208,7 @@ pub enum CompileFilter {
198208 } ,
199209 Only {
200210 all_targets : bool ,
201- lib : bool ,
211+ lib : LibRule ,
202212 bins : FilterRule ,
203213 examples : FilterRule ,
204214 tests : FilterRule ,
@@ -361,6 +371,10 @@ impl FilterRule {
361371 }
362372 }
363373
374+ pub fn none ( ) -> FilterRule {
375+ FilterRule :: Just ( Vec :: new ( ) )
376+ }
377+
364378 fn matches ( & self , target : & Target ) -> bool {
365379 match * self {
366380 FilterRule :: All => true ,
@@ -384,7 +398,8 @@ impl FilterRule {
384398}
385399
386400impl CompileFilter {
387- pub fn new (
401+ /// Construct a CompileFilter from raw command line arguments.
402+ pub fn from_raw_arguments (
388403 lib_only : bool ,
389404 bins : Vec < String > ,
390405 all_bins : bool ,
@@ -396,6 +411,7 @@ impl CompileFilter {
396411 all_bens : bool ,
397412 all_targets : bool ,
398413 ) -> CompileFilter {
414+ let rule_lib = if lib_only { LibRule :: True } else { LibRule :: False } ;
399415 let rule_bins = FilterRule :: new ( bins, all_bins) ;
400416 let rule_tsts = FilterRule :: new ( tsts, all_tsts) ;
401417 let rule_exms = FilterRule :: new ( exms, all_exms) ;
@@ -404,21 +420,34 @@ impl CompileFilter {
404420 if all_targets {
405421 CompileFilter :: Only {
406422 all_targets : true ,
407- lib : true ,
423+ lib : LibRule :: Default ,
408424 bins : FilterRule :: All ,
409425 examples : FilterRule :: All ,
410426 benches : FilterRule :: All ,
411427 tests : FilterRule :: All ,
412428 }
413- } else if lib_only
429+ } else {
430+ CompileFilter :: new ( rule_lib, rule_bins, rule_tsts, rule_exms, rule_bens)
431+ }
432+ }
433+
434+ /// Construct a CompileFilter from underlying primitives.
435+ pub fn new (
436+ rule_lib : LibRule ,
437+ rule_bins : FilterRule ,
438+ rule_tsts : FilterRule ,
439+ rule_exms : FilterRule ,
440+ rule_bens : FilterRule ,
441+ ) -> CompileFilter {
442+ if rule_lib == LibRule :: True
414443 || rule_bins. is_specific ( )
415444 || rule_tsts. is_specific ( )
416445 || rule_exms. is_specific ( )
417446 || rule_bens. is_specific ( )
418447 {
419448 CompileFilter :: Only {
420449 all_targets : false ,
421- lib : lib_only ,
450+ lib : rule_lib ,
422451 bins : rule_bins,
423452 examples : rule_exms,
424453 benches : rule_bens,
@@ -454,7 +483,7 @@ impl CompileFilter {
454483 match * self {
455484 CompileFilter :: Default { .. } => true ,
456485 CompileFilter :: Only {
457- lib,
486+ ref lib,
458487 ref bins,
459488 ref examples,
460489 ref tests,
@@ -466,7 +495,11 @@ impl CompileFilter {
466495 TargetKind :: Test => tests,
467496 TargetKind :: Bench => benches,
468497 TargetKind :: ExampleBin | TargetKind :: ExampleLib ( ..) => examples,
469- TargetKind :: Lib ( ..) => return lib,
498+ TargetKind :: Lib ( ..) => return match * lib {
499+ LibRule :: True => true ,
500+ LibRule :: Default => true ,
501+ LibRule :: False => false ,
502+ } ,
470503 TargetKind :: CustomBuild => return false ,
471504 } ;
472505 rule. matches ( target)
@@ -620,13 +653,13 @@ fn generate_targets<'a>(
620653 }
621654 CompileFilter :: Only {
622655 all_targets,
623- lib,
656+ ref lib,
624657 ref bins,
625658 ref examples,
626659 ref tests,
627660 ref benches,
628661 } => {
629- if lib {
662+ if * lib != LibRule :: False {
630663 let mut libs = Vec :: new ( ) ;
631664 for proposal in filter_targets ( packages, Target :: is_lib, false , build_config. mode ) {
632665 let Proposal { target, pkg, .. } = proposal;
@@ -640,7 +673,7 @@ fn generate_targets<'a>(
640673 libs. push ( proposal)
641674 }
642675 }
643- if !all_targets && libs. is_empty ( ) {
676+ if !all_targets && libs. is_empty ( ) && * lib == LibRule :: True {
644677 let names = packages. iter ( ) . map ( |pkg| pkg. name ( ) ) . collect :: < Vec < _ > > ( ) ;
645678 if names. len ( ) == 1 {
646679 failure:: bail!( "no library targets found in package `{}`" , names[ 0 ] ) ;
0 commit comments