.push-zamok: &push-zamok
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - \[ -d ${ZAMOK_SOURCE} \] && 
        rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" -rqz ${ZAMOK_SOURCE}/. v-lafeychine@zamok.crans.org:www/proost/${ZAMOK_TARGET} || true

default:
  image: vlafeychine/rust
  interruptible: true
  cache:
    paths:
      - .cache/cargo/
      - target/

variables:
  CARGO_HOME: "$CI_PROJECT_DIR/.cache/cargo/"
  RUSTFLAGS: "-D warnings"


stages: [ check, build, tests, docs ]

format:
  stage: check
  cache: []
  script:
    - cargo fmt --check

lint:
  stage: check
  script:
    - cargo clippy --all-targets --all-features

build:
  stage: build
  script:
    - cargo build

tests:
  stage: tests
  variables:
    CARGO_INCREMENTAL: 0
    EXCL_LINE: "//!|///|#\\[|use|unreachable!"
    EXCL_BR_LINE: "#\\[|unreachable!|assert(_eq)?!|^(pub |pub(crate) )?(enum|struct)|^[[:space:]]*\\}(,)?$"
    EXCL_BR_START: "^(pub |pub(crate) )?(enum|struct) [[:alpha:]]+ \\{"
    EXCL_BR_STOP: "^\\}"
    RUSTDOCFLAGS: "${RUSTFLAGS}"
    RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
    ZAMOK_SOURCE: coverage
    ZAMOK_TARGET: coverage/${CI_COMMIT_REF_SLUG}

  cache:
    paths:
      - .cache/cargo/

  script:
    - cargo test
    - grcov . -s . -b ./target/debug/ --branch --ignore-not-existing --excl-line "${EXCL_LINE}" --excl-br-line "${EXCL_BR_LINE}" --excl-br-start "${EXCL_BR_START}" --excl-br-stop "${EXCL_BR_STOP}" --ignore "*cargo*" -t cobertura -o ./coverage_raw.xml &&
        xsltproc --novalid --output ./coverage.xml ./.gitlab-cov.xsl ./coverage_raw.xml || true
    - grcov . -s . -b ./target/debug/ --branch --ignore-not-existing --excl-line "${EXCL_LINE}" --excl-br-line "${EXCL_BR_LINE}" --excl-br-start "${EXCL_BR_START}" --excl-br-stop "${EXCL_BR_STOP}" --ignore "*cargo*" -t lcov -o ./coverage.lcov &&
        genhtml --branch --no-function-coverage --precision 2 ./coverage.lcov -o ./coverage || true
    - *push-zamok

  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: ./coverage.xml

  coverage: '/^  branches...: \d+.\d+%/'


docs:
  stage: docs
  variables:
    ZAMOK_SOURCE: target/doc
    ZAMOK_TARGET: doc

  only:
    refs: [ main ]

  script:
    - cargo doc --no-deps
    - *push-zamok
