stages:
  - format
  - build
  - lint
  - test

.rust_template: &rust_template
  before_script:
    - export CARGO_HOME="$CI_PROJECT_DIR/cargo"
    - rustup --version
    - rustc --version
    - cargo --version

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

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

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

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

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