#!/bin/bash

# Function to check if an element is present in an array.
has_element() {
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
}

# Get the current diff from git
diff_pre=($(git diff --name-only))

# Format files
cargo fmt

# Get the diff from git after formatting
diff_post=($(git diff --name-only))

# Check if any files were changed and add them to the current commit
for val in "${diff_post[@]}"; do
    if ! has_element "$val" "${diff_pre[@]}"; then
        git add "$val"
    fi
done

# Generate documentation
cargo doc

# Run tests
cargo test
