name: CI

on:
  push:
    branches: ['main']
  pull_request:
  schedule:
    - cron: "22 3 * * 5"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:

  test:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        rust: [stable, beta, 1.81.0]

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@master
        id: toolchain
        with:
          toolchain: ${{ matrix.rust }}
      - uses: Swatinem/rust-cache@v2
      - name: Build 1
        run: cargo +${{steps.toolchain.outputs.name}} build --all-features
      - name: Build 2
        run: cargo +${{steps.toolchain.outputs.name}} build --all-features

  snowflake:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Martin's snowflake formatting rules
        uses: algesten/snowflake@v1.1.0
        with:
          check_diff: true
          line_width_rules: 'CHANGELOG.md:120;c_cpp_properties.json:160;Cargo.toml:130;README.md:180;README.tpl:180;*.md:110;*.rs:110;*.toml:110;DEFAULT=110'
          rust_import_ignore: 'wincrypto/src/**/*.rs'

  pii:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@master
        id: toolchain
        with:
          toolchain: stable
      - uses: Swatinem/rust-cache@v1
      - name: Test pii
        run: cargo +${{steps.toolchain.outputs.name}} test --test pii --features pii

  test_wincrypto:
    strategy:
      matrix:
        os: [windows-latest]
        rust: [stable, beta, 1.81.0]

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v2
      - uses: dtolnay/rust-toolchain@master
        id: toolchain
        with:
          toolchain: ${{ matrix.rust }}
      - uses: Swatinem/rust-cache@v1
      - name: Build
        run: cargo +${{steps.toolchain.outputs.name}} build --no-default-features --features wincrypto
      - name: Test
        run: cargo +${{steps.toolchain.outputs.name}} test --no-default-features --features wincrypto

  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: dtolnay/rust-toolchain@master
        id: toolchain
        with:
          toolchain: stable
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v1
      - name: fmt
        run: cargo +${{steps.toolchain.outputs.name}} fmt --all -- --check
      - name: clippy
        run: cargo +${{steps.toolchain.outputs.name}} clippy -- -D warnings
      - name: clippy (openssl features)
        run: cargo +${{steps.toolchain.outputs.name}} clippy --no-default-features --features openssl,vendored,sha1 -- -D warnings
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          components: clippy
      - name: doc
        run: cargo doc --no-deps
        env:
          RUSTDOCFLAGS: -Dwarnings

  cargo-deny:
    name: cargo-deny

    # TODO: remove this matrix when https://github.com/EmbarkStudios/cargo-deny/issues/324 is resolved
    strategy:
      fail-fast: false
      matrix:
        platform:
          - aarch64-apple-ios
          - aarch64-linux-android
          - i686-pc-windows-gnu
          - i686-pc-windows-msvc
          - i686-unknown-linux-gnu
          - wasm32-unknown-unknown
          - x86_64-apple-darwin
          - x86_64-apple-ios
          - x86_64-pc-windows-gnu
          - x86_64-pc-windows-msvc
          - x86_64-unknown-linux-gnu
          - x86_64-unknown-redox

    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: EmbarkStudios/cargo-deny-action@v1
      with:
        command: check
        log-level: error
        arguments: --all-features --target ${{ matrix.platform }}
