m-bus-parser 0.1.0

A library for parsing M-Bus frames
Documentation
name: Rust

on:
  push:
    branches: [main]
    tags:
      - 'v*'
      - 'cli-v*'
      - 'wasm-v*'
  pull_request:
    branches: [main]
  workflow_dispatch:

jobs:
  build-library:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/cli-v') || github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v2
      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - name: Check Formatting
        run: cargo fmt -- --check
      - name: Security audit
        run: | 
          cargo install cargo-audit
          cargo audit
      - name: Build Library
        run: cargo build --verbose
      - name: Run Library Tests
        run: | 
          cargo test --verbose
          cargo test --verbose -F std
      - name: Run Library Tests with plaintext before with enabled
        run: cargo test --verbose -F plaintext-before-extension
      - name: Lint with Clippy
        run: |
          rustup component add clippy
          cargo clippy -- -D warnings

  build-cli:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' || github.event_name == 'pull_request'
    needs: build-library
    steps:
      - uses: actions/checkout@v2
      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - name: Check CLI Formatting
        run: |
          cd cli
          cargo fmt -- --check
      - name: Build CLI
        run: |
          cd cli
          cargo build --verbose
      - name: Run CLI Tests
        run: |
          cd cli
          cargo test --verbose
      - name: Lint CLI with Clippy
        run: |
          rustup component add clippy
          cd cli
          cargo clippy -- -D warnings

  build-wasm:
    runs-on: ubuntu-latest
    if: github.event_name == 'push' || github.event_name == 'pull_request'
    needs: build-library
    steps:
      - uses: actions/checkout@v2
      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
      - name: Check WASM Formatting
        run: |
          cd wasm
          cargo fmt -- --check
      - name: Build WASM
        run: |
          cd wasm
          cargo build --verbose
      - name: Run WASM Tests
        run: |
          cd wasm
          cargo test --verbose
      - name: Lint WASM with Clippy
        run: |
          rustup component add clippy
          cd wasm
          cargo clippy -- -D warnings

  publish-library:
    needs: build-library
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v2
      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true
      - name: Publish Library to crates.io
        uses: actions-rs/cargo@v1
        with:
          command: publish
          args: --token ${{ secrets.CRATESTOKEN }}

  publish-cli:
    needs: build-cli
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/cli-v')
    steps:
      - uses: actions/checkout@v2
      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true
      - name: Publish CLI to crates.io
        run: |
          cd cli
          cargo publish --token ${{ secrets.CRATESTOKEN }}

  publish-wasm:
    needs: build-wasm
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
    if: startsWith(github.ref, 'refs/tags/wasm-v')
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true
      - name: Install wasm-pack
        run: cargo install wasm-pack
      - name: Build wasm package
        run: |
          cd wasm
          wasm-pack build --target bundler
      - uses: actions/setup-node@v4
        with:
          node-version: '20.x'
          registry-url: 'https://registry.npmjs.org'
          token: ${{ secrets.NPM_TOKEN }}
      - name: Install npm dependencies
        run: |
          cd wasm/pkg
          npm install
          npm ci
      - name: Publish to npm
        run: |
          cd wasm/pkg
          npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Create new artifacts for website
        run: |
          rm -rf wasm/pkg
          cd wasm
          wasm-pack build --target web
      - name: Copy wasm artifacts to docs
        run: |
          cp wasm/pkg/m_bus_parser_wasm_pack.js docs/
          cp wasm/pkg/m_bus_parser_wasm_pack_bg.js docs/
          cp wasm/pkg/m_bus_parser_wasm_pack_bg.wasm docs/
          cp wasm/pkg/package.json docs/
      - name: Commit updated wasm artifacts
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config user.name "github-actions"
          git config user.email "github-actions@github.com"
          git add docs/m_bus_parser_wasm_pack.js docs/m_bus_parser_wasm_pack_bg.js docs/m_bus_parser_wasm_pack_bg.wasm docs/meter.png docs/package.json
          git commit -m "Update docs wasm artifacts" || echo "No changes to commit"
          git push origin HEAD:main