version: 2.1

jobs:
  test-nightly:
    docker:
      - image: cimg/rust:1.63.0
    steps:
      - checkout
      - run: cargo --version
      - run:
          name: Install Rust nightly
          command: rustup install nightly
      - run:
          name: Setting Rust nightly as default
          command: rustup default nightly
      - restore_cache:
          name: Restore cache of Cargo data
          keys:
            - cargo-cache
      - run:
          name: Run tests with all features enabled
          command: cargo test --workspace --all-features
      - run:
          name: Run tests with only default features enabled
          command: cargo test --workspace
      - save_cache:
          name: Save cache of Cargo data
          key: cargo-cache
          paths:
            - ~/.cargo

  test-stable:
    docker:
      - image: cimg/rust:1.63.0
    steps:
      - checkout
      - run: cargo --version
      - restore_cache:
          name: Restore cache of Cargo data
          keys:
            - cargo-cache
      - run:
          name: Run tests with all features except factory enabled
          command: cargo test --workspace --features async
      - run:
          name: Run tests with only default features enabled
          command: cargo test --workspace
      - save_cache:
          name: Save cache of Cargo data
          key: cargo-cache
          paths:
            - ~/.cargo

  coverage:
    machine: true
    steps:
      - checkout
      - run:
          name: Code Coverage
          command: |
            docker run \
              --security-opt seccomp=unconfined \
              -v "${PWD}:/volume" \
              xd009642/tarpaulin:0.22.0-nightly \
              cargo \
                +nightly \
                tarpaulin \
                --workspace \
                --all-features \
                --ciserver circle-ci \
                --out Xml
            bash <(curl -s https://codecov.io/bash)

  lints-stable:
    docker:
      - image: cimg/rust:1.63.0
    steps:
      - checkout
      - run: cargo --version
      - restore_cache:
          name: Restore cache of Cargo data
          keys:
            - cargo-cache
      - run:
          name: Run Clippy
          command: cargo clippy --workspace
      - save_cache:
          name: Save cache of Cargo data
          key: cargo-cache
          paths:
            - ~/.cargo

  code-style:
    docker:
      - image: cimg/rust:1.63.0
    steps:
      - checkout
      - run: cargo --version
      - run:
          name: Install Rust nightly
          command: rustup install nightly
      - run:
          name: Setting Rust nightly as default
          command: rustup default nightly
      - run:
          name: Run Rustfmt
          command: find src macros/src -type f -name "*.rs" -exec rustfmt --edition 2021 --check {} \;

workflows:
  test:
    jobs:
      - test-nightly
      - test-stable
      - coverage

  checks:
    jobs:
      - lints-stable
      - code-style