Skip to content

feat(flags): add semver targeting to local evaluation#74

Merged
dmarticus merged 2 commits intomainfrom
feat/semver-targeting
Mar 4, 2026
Merged

feat(flags): add semver targeting to local evaluation#74
dmarticus merged 2 commits intomainfrom
feat/semver-targeting

Conversation

@dmarticus
Copy link
Copy Markdown
Contributor

@dmarticus dmarticus commented Mar 2, 2026

Summary

Add semver comparison operators for local feature flag evaluation.

Problem

PostHog supports semver-based feature flag targeting in the UI (e.g., show a feature only to users on app version >=2.0.0), but the Rust SDK could not evaluate these flags locally. This forced all semver-targeted flags to fall back to server-side evaluation, adding latency and network dependency.

Related implementations:

Changes

  • Add parse_semver() function to parse version strings into (major, minor, patch) tuples
    • Handles: v-prefix stripping, whitespace trimming, pre-release/build metadata stripping
    • Supports partial versions (defaults missing components to 0)
    • Ignores extra components beyond major.minor.patch
  • Add 6 basic comparison operators: semver_eq, semver_neq, semver_gt, semver_gte, semver_lt, semver_lte
  • Add 3 range operators:
    • semver_tilde: patch-level range (~1.2.3 means >=1.2.3 <1.3.0)
    • semver_caret: compatible-with range per semver spec (^1.2.3 means >=1.2.3 <2.0.0, with special handling for 0.x versions)
    • semver_wildcard: wildcard patterns (1.2.* means >=1.2.0 <1.3.0)
  • Implement inline (~100 lines) rather than adding the semver crate to minimize dependency footprint for SDK consumers

How did you test this code?

Added 38 new tests covering:

Parsing tests:

  • Basic parsing (1.2.3, 0.0.0, 10.20.30)
  • v-prefix handling (v1.2.3, V1.2.3)
  • Whitespace trimming
  • Pre-release/build metadata stripping (1.2.3-alpha, 1.2.3+build)
  • Partial versions (1.2 → 1.2.0, 1 → 1.0.0)
  • Extra components ignored (1.2.3.4 → 1.2.3)
  • Leading zeros (01.02.03 → 1.2.3)
  • Invalid inputs (empty, non-numeric, leading dot)

Operator tests:

  • All 6 comparison operators with true/false cases
  • Tilde bounds including zero-version edge cases
  • Caret bounds for all 3 semver-spec behaviors (X>0, 0.Y>0, 0.0.Z)
  • Wildcard patterns (X.* and X.Y.*)
  • Numeric ordering verification (9.0.0 < 10.0.0, not string comparison)

Error handling tests:

  • Invalid property values return InconclusiveMatchError
  • Invalid target values return InconclusiveMatchError
  • Missing properties handled correctly
  • Null values handled correctly
cargo test --all-features
# 85 passed; 0 failed

@dmarticus dmarticus merged commit b55a4fb into main Mar 4, 2026
8 checks passed
@dmarticus dmarticus deleted the feat/semver-targeting branch March 4, 2026 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants