stages:
  - format
  - build
  - lint
  - test

.rust_template: &rust_template
  tags:
    - nix
  before_script:
    - export CARGO_HOME="$CI_PROJECT_DIR/cargo"
    - nix develop -c rustc --version
    - nix develop -c cargo --version

rust-nightly:
  stage: build
  needs: []
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - cargo/
      - target/
  <<: *rust_template
  script:
    - nix develop -c cargo build --verbose

rust-nightly-test:
  stage: test
  needs:
    - rust-nightly
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - cargo/
      - target/
    policy: pull
  <<: *rust_template
  script:
    - nix develop -c cargo test --verbose -j 1

rust-nightly-fmt:
  stage: format
  needs: []
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - cargo/
      - target/
    policy: pull
  <<: *rust_template
  script:
    - nix develop -c cargo fmt --all --verbose -- --check

rust-nightly-clippy:
  stage: lint
  needs:
    - rust-nightly
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - cargo/
      - target/
  <<: *rust_template
  script:
    - nix develop -c cargo clippy --all --tests --verbose -- -D warnings

pages:
  stage: build
  needs:
    - rust-nightly
  cache:
    key: "$CI_COMMIT_REF_SLUG"
    paths:
      - cargo/
      - target/
    policy: pull
  <<: *rust_template
  script:
    - nix develop -c cargo doc --no-deps
    - rm -rf public
    - mkdir public
    - cp -R target/doc/* public
  artifacts:
    paths:
    - public
