name: CI
on: [push, pull_request]

permissions:
  contents: read #  to fetch code (actions/checkout)

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        build: [stable, beta, nightly, macos, win32, win64, mingw]
        include:
          - build: stable
            os: ubuntu-latest
            rust: stable
          - build: beta
            os: ubuntu-latest
            rust: beta
          - build: nightly
            os: ubuntu-latest
            rust: nightly
          - build: macos
            os: macos-latest
            rust: stable
          - build: win32
            os: windows-latest
            rust: stable-i686-pc-windows-msvc
          - build: win64
            os: windows-latest
            rust: stable-x86_64-pc-windows-msvc
          - build: mingw
            os: windows-latest
            rust: stable-x86_64-pc-windows-gnu
    steps:
    - uses: actions/checkout@master
    - name: Install Rust
      run: |
        rustup update ${{ matrix.rust }} --no-self-update
        rustup default ${{ matrix.rust }}
    - run: cargo test --verbose
    - run: cargo test --verbose --no-default-features
    - run: cargo test --verbose --features std,serde,sval,sval_ref,value-bag,kv,kv_std,kv_sval,kv_serde
    - run: cargo test --verbose --features serde
    - run: cargo test --verbose --features std
    - run: cargo test --verbose --features kv
    - run: cargo test --verbose --features kv_sval
    - run: cargo test --verbose --features kv_serde
    - run: cargo test --verbose --features "kv kv_std kv_sval kv_serde"
    - run: cargo run --verbose --manifest-path test_max_level_features/Cargo.toml
    - run: cargo run --verbose --manifest-path test_max_level_features/Cargo.toml --release

  rustfmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Install Rust
      run: |
        rustup update stable --no-self-update
        rustup default stable
        rustup component add rustfmt
      # log repo does not use Cargo workspaces, so `cargo fmt` will not check all the code
      # perhaps this should be changed in the future
    - run: cargo fmt -- --check
    - run: cargo fmt --manifest-path test_max_level_features/Cargo.toml -- --check
    - run: cargo fmt --manifest-path tests/Cargo.toml -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Install Rust
      run: |
        rustup update stable --no-self-update
        rustup default stable
        rustup component add clippy
    - run: cargo clippy --verbose
    - run: cargo clippy --verbose --manifest-path test_max_level_features/Cargo.toml
    - run: cargo clippy --verbose --manifest-path tests/Cargo.toml

  doc:
    name: Check Documentation
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Install Rust
      run: |
        rustup update stable --no-self-update
        rustup default stable
        rustup component add rust-docs
    - name: Run rustdoc
      run: RUSTDOCFLAGS="-D warnings" cargo doc --verbose --features std,serde,sval,sval_ref,value-bag,kv,kv_std,kv_sval,kv_serde

  features:
    name: Feature check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Install Rust
        run: |
          rustup update nightly --no-self-update
          rustup default nightly
      - run: cargo build --verbose -Z avoid-dev-deps --features kv
      - run: cargo build --verbose -Z avoid-dev-deps --features "kv std"
      - run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_sval"
      - run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_serde"
      - run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_std"
      - run: cargo build --verbose -Z avoid-dev-deps --features "kv kv_sval kv_serde"

  minimalv:
    name: Minimal versions
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Install Rust
        run: |
          rustup update nightly --no-self-update
          rustup default nightly
      - run: cargo build --verbose -Z minimal-versions --features kv
      - run: cargo build --verbose -Z minimal-versions --features "kv std"
      - run: cargo build --verbose -Z minimal-versions --features "kv kv_sval"
      - run: cargo build --verbose -Z minimal-versions --features "kv kv_serde"
      - run: cargo build --verbose -Z minimal-versions --features "kv kv_std"
      - run: cargo build --verbose -Z minimal-versions --features "kv kv_sval kv_serde"

  msrv:
    name: MSRV
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Install Rust
        run: |
          rustup update 1.60.0 --no-self-update
          rustup default 1.60.0
      - run: |
          cargo test --verbose --manifest-path tests/Cargo.toml
          cargo test --verbose --manifest-path tests/Cargo.toml --features kv

  embedded:
    name: Embedded
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - name: Install Rust
        run: |
          rustup update stable --no-self-update
          rustup default stable
      - run: rustup target add thumbv6m-none-eabi riscv32imc-unknown-none-elf
      - run: cargo build --verbose --target=thumbv6m-none-eabi
      - run: cargo build --verbose --target=riscv32imc-unknown-none-elf
