parse_version_constraint

Function parse_version_constraint 

Source
pub fn parse_version_constraint(constraint: &str) -> VersionConstraint
Expand description

Parse a version constraint string into a structured constraint type.

This function analyzes a constraint string and determines whether it represents a Git commit hash, a version/tag specification, or a branch name. It provides a simple classification system for different types of version references.

§Classification Logic

The function uses heuristics to determine constraint types:

  1. Commit hashes: 7+ hexadecimal characters (e.g., "abc123def")
  2. Version/tag specs: Valid semantic versions or requirements (e.g., "^1.0.0", "*")
  3. Branch names: Everything else (e.g., "main", "latest", "feature/auth")

§Arguments

  • constraint - The constraint string to parse and classify

§Returns

Returns a VersionConstraint enum variant indicating the constraint type:

§Examples

use agpm_cli::version::{parse_version_constraint, VersionConstraint};

// Semantic versions are classified as tags
let constraint = parse_version_constraint("1.0.0");
assert!(matches!(constraint, VersionConstraint::Tag(_)));

let constraint = parse_version_constraint("v1.2.3");
assert!(matches!(constraint, VersionConstraint::Tag(_)));

// Prefixed versions and constraints are also classified as tags
let constraint = parse_version_constraint("agents-v1.2.0");
assert!(matches!(constraint, VersionConstraint::Tag(_)));

let constraint = parse_version_constraint("agents-^v1.0.0");
assert!(matches!(constraint, VersionConstraint::Tag(_)));

// Version requirements are classified as tags
let constraint = parse_version_constraint("^1.0.0");
assert!(matches!(constraint, VersionConstraint::Tag(_)));

let constraint = parse_version_constraint("*");
assert!(matches!(constraint, VersionConstraint::Tag(_)));

// Commit hashes are detected by hex pattern
let constraint = parse_version_constraint("abc1234");
assert!(matches!(constraint, VersionConstraint::Commit(_)));

let constraint = parse_version_constraint("1234567890abcdef1234567890abcdef12345678");
assert!(matches!(constraint, VersionConstraint::Commit(_)));

// Branch names are the fallback
let constraint = parse_version_constraint("main");
assert!(matches!(constraint, VersionConstraint::Branch(_)));

let constraint = parse_version_constraint("feature/auth-system");
assert!(matches!(constraint, VersionConstraint::Branch(_)));

§Commit Hash Detection

The function identifies commit hashes using these criteria:

  • Minimum 7 characters (Git’s default abbreviation length)
  • All characters must be hexadecimal (0-9, a-f, A-F)
  • No maximum length (supports full 40-character SHA-1 hashes)

§Version/Tag Detection

Version and tag specifications are identified by:

  • Valid semantic version parsing (with or without v prefix)
  • Valid semantic version requirement parsing (ranges, comparisons)
  • Wildcard "*" for any version

§Branch Name Fallback

Any string that doesn’t match the above patterns is treated as a branch name:

  • Simple names: "main", "develop", "staging", "latest"
  • Namespaced branches: "feature/new-ui", "bugfix/auth-issue"
  • Special characters: "release/v1.0", "user/name/branch"

§Use Cases

This function is useful for:

  • Parsing user input in dependency specifications
  • Routing version resolution to appropriate handlers
  • Validating constraint syntax in configuration files
  • Building version constraint objects from strings