Skip to main content

compile_requirement_unless

Function compile_requirement_unless 

Source
pub fn compile_requirement_unless<M>(
    requirement: &str,
    is_undecidable: impl FnOnce(&str) -> bool,
    matcher: impl FnOnce(String) -> M,
) -> Option<Box<dyn RequirementMatcher>>
where M: RequirementMatcher + 'static,
Expand description

Shared shape for a crate::lsp_helpers::RequirementResolution::compile_requirement guarded by one predicate.

This is the pattern several ecosystems’ guards independently re-implemented (deps-go’s pseudo-version check, deps-composer’s dev-branch/@dev check, deps-bundler’s exact-pin check, deps-maven/deps-gradle’s malformed-range check, deps-nuget’s malformed-requirement check). See crate::lsp_helpers::RequirementResolution::compile_requirement’s docs for why None is correct in exactly this case: is_undecidable(requirement) true means the fetched available list structurally cannot contain a version that would decide the match either way, so scanning it would always report Some(false) and produce a false “no published version satisfies this requirement” diagnostic.

Returns None when is_undecidable(requirement) is true. Otherwise builds matcher from requirement’s owned String and boxes it as the trait object crate::lsp_helpers::RequirementResolution::compile_requirement returns.

Ecosystems whose guard is a fallible parse rather than a named predicate over the requirement string (deps-cargo, deps-npm, deps-pypi, deps-swift) don’t fit this shape and implement compile_requirement directly via .ok().map(...) instead. deps-dart implements compile_requirement but has no guard at all — every requirement string is a valid Dart constraint by construction, so it is always Some.

§Examples

use deps_core::lsp_helpers::{compile_requirement_unless, RequirementMatcher};
use deps_core::ConcreteVersion;

struct ExactMatcher(String);
impl RequirementMatcher for ExactMatcher {
    fn matches(&self, version: &ConcreteVersion) -> Option<bool> {
        Some(version.as_str() == self.0)
    }
}

let is_pseudo_version = |r: &str| r.starts_with("v0.0.0-");

assert!(
    compile_requirement_unless(
        "v0.0.0-20191109021931-daa7c04131f5",
        is_pseudo_version,
        ExactMatcher,
    )
    .is_none()
);
assert!(compile_requirement_unless("v1.2.3", is_pseudo_version, ExactMatcher).is_some());