#!/usr/bin/env bash

# Custom types: port, semver, datacenter enum.
# Multiple types in one file — each gets its own @type outer function.

@type.port() {
    @is() {
        local val="$1"
        @int.is "$val" || return 1
        if (( val < 1 || val > 65535 )); then
            @throw "port must be 1-65535"
            return 1
        fi
        @return "$val"
    }

    @default() {
        @return 8080
    }
}

@type.semver() {
    @is() {
        local val="$1"
        if [[ ! "$val" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            @throw "'$val' is not valid semver"
            return 1
        fi
        @return "\"$val\""
    }
}

@type.datacenter() {
    @is() {
        local val="$1"
        case "$val" in
            alpha|beta|production) @return "\"$val\"" ;;
            *) @throw "'$val' is not a valid datacenter (alpha|beta|production)"; return 1 ;;
        esac
    }

    @default() {
        @return "\"alpha\""
    }
}
