name: Rust

on:
  push:
    branches: [ "main" ]
    tags:
      - "*"
  pull_request:
    branches: [ "main" ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Build
      run: cargo build --verbose
    - name: Run tests
      run: cargo test --verbose
    - name: Run tests (all features)
      run: cargo test --all-features --verbose
    - name: Coding style
      run: cargo fmt --all -- --check
    - name: Documentation
      run: cargo doc --all-features
  
  release:
    name: Release
    runs-on: ubuntu-latest
    needs: [build]
    if: github.ref_type == 'tag'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
      - name: Publish
        env:
          TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
        run: |
          cargo login $TOKEN
          cargo publish
