stages:
  - check
  - publish

image: rust:latest

# Cache dependencies to speed up builds
cache:
  paths:
    - target/
    - ~/.cargo/

lint:
  stage: check
  script:
    - rustup component add rustfmt clippy
    - cargo fmt -- --check
    - cargo clippy -- -D warnings
  rules:
    - when: always

# Publish stage - only runs on tags matching vX.Y.Z pattern
publish:
  stage: publish
  script:
    # Extract version from tag (remove 'v' prefix)
    - TAG_VERSION=${CI_COMMIT_TAG#v}
    # Extract version from Cargo.toml
    - CARGO_VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml)
      # Verify versions match
    - |
      if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
        echo "Error: Tag version ($TAG_VERSION) does not match Cargo.toml version ($CARGO_VERSION)"
        exit 1
      fi
    - echo "Publishing crate version $CARGO_VERSION to crates.io"
    - cargo publish --no-verify
  rules:
    - if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+.*$/
  environment:
    name: crates-io