packages feed

zeolite-lang-0.22.0.0: lib/util/src/extra.0rx

/* -----------------------------------------------------------------------------
Copyright 2019-2021 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- */

// Author: Kevin P. Barry [ta0kira@gmail.com]

define Void {
  @category Void singleton <- Void{ }

  default () {
    return singleton
  }

  duplicate () {
    return singleton
  }

  formatted () {
    return "Void"
  }

  equals (_,_) {
    return true
  }

  lessThan (_,_) {
    return false
  }

  hashed () {
    return 123456789
  }
}

define ErrorOr {
  @value optional #x maybeValue
  @value optional Formatted maybeError

  value (x) {
    return ErrorOr<#x>{ x, empty }
  }

  error (message) {
    return ErrorOr<all>{ empty, message }
  }

  isError () {
    return !present(maybeValue) && present(maybeError)
  }

  getValue () {
    if (present(maybeValue)) {
      return require(maybeValue)
    } else {
      fail(require(maybeError))
    }
  }

  getError () {
    return require(maybeError)
  }

  convertError () {
    if (!present(maybeError)) {
      fail("no error present to convert")
    } else {
      return ErrorOr<all>{ empty, maybeError }
    }
  }
}