name: Rust
on: 
  push:
    branches: [ "main" ]
    tags:
      - '*'
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:  

env:
  CARGO_TERM_COLOR: always
  RUST_VERSION: 1.76.0

jobs:
  static_analysis:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' }}
    steps:
      - uses: actions/checkout@v3
      - name: rustup
        run: rustup default $RUST_VERSION
      - name: cargo-fmt
        run: rustup component add rustfmt && cargo fmt --check
      - name: cargo-clippy
        run: rustup component add clippy && cargo clippy --all-targets --all-features -- --no-deps -D warnings

  build:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' }}
    steps:
    - uses: actions/checkout@v3
    - uses: adambirds/docker-compose-action@v1.3.0
      with:
        compose-file: "./docker-compose.yaml"
    - name: rustup
      run: rustup default $RUST_VERSION
    - name: build
      run: make
    - name: test
      run: rustup component add clippy && make check

  publish:
      runs-on: ubuntu-latest
      if: startsWith(github.event.ref, 'refs/tags/')
      steps:
        - uses: actions/checkout@v3
        - name: rustup
          run: rustup default $RUST_VERSION
        - name: publish
          env:
            CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
          run: cargo publish --token $CARGO_REGISTRY_TOKEN