split_prefix_and_version

Function split_prefix_and_version 

Source
pub fn split_prefix_and_version(s: &str) -> (Option<String>, &str)
Expand description

Splits a version string into an optional prefix and the version/constraint part.

This function extracts versioned prefixes from tag names and version constraints, enabling support for prefixed versioning schemes like agents-v1.0.0 or snippets-^v2.0.0. The prefix can contain hyphens and is separated from the version by detecting where the version pattern begins.

§Algorithm

Scans left-to-right to find the first occurrence of:

  • Constraint operators: ^, ~, =, <, >, !, *
  • Version prefix: v followed immediately by a digit
  • Bare digit (start of version number)

Everything before this point (minus trailing -) becomes the prefix.

§Arguments

  • s - The string to parse (tag name or version constraint)

§Returns

A tuple of (Option<String>, &str) where:

  • First element is the prefix (if any)
  • Second element is the version/constraint string

§Examples

use agpm_cli::version::split_prefix_and_version;

// Prefixed versions
assert_eq!(
    split_prefix_and_version("agents-v1.0.0"),
    (Some("agents".to_string()), "v1.0.0")
);
assert_eq!(
    split_prefix_and_version("my-tool-^v2.0.0"),
    (Some("my-tool".to_string()), "^v2.0.0")
);

// Unprefixed versions
assert_eq!(split_prefix_and_version("v1.0.0"), (None, "v1.0.0"));
assert_eq!(split_prefix_and_version("^1.0.0"), (None, "^1.0.0"));

// Edge cases
assert_eq!(
    split_prefix_and_version("tool-v-v1.0.0"),
    (Some("tool-v".to_string()), "v1.0.0")
);