-
Notifications
You must be signed in to change notification settings - Fork 27
/
package.scala
56 lines (40 loc) · 1.49 KB
/
package.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2016-2020 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause
package edu.gemini
import cats.data.IorNec
import cats.data.Ior
import cats.data.NonEmptyChain
import cats.syntax.all._
package object grackle {
/**
* A result value.
*
* A result of type `T`, a non-empty collection of errors encoded as
* Json, or both.
*/
type Result[+T] = IorNec[Problem, T]
object Result {
val unit: Result[Unit] =
apply(())
def apply[A](a: A): Result[A] = Ior.right(a)
def failure[A](s: String): Result[A] =
failure(Problem(s))
def failure[A](p: Problem): Result[A] =
Ior.left(NonEmptyChain(p))
def fromOption[A](oa: Option[A], ifNone: => Problem): Result[A] =
oa match {
case Some(a) => Result(a)
case None => Result.failure(ifNone)
}
def fromEither[A](ea: Either[Problem, A]): Result[A] =
ea.fold(Result.failure(_), Result.apply)
def warning[A](warning: Problem, value: A): Result[A] =
Result.failure[A](warning).putRight(value)
def fromOption[A](oa: Option[A], ifNone: => String)(implicit ev: DummyImplicit): Result[A] =
fromOption(oa, Problem(ifNone))
def fromEither[A](ea: Either[String, A])(implicit ev: DummyImplicit): Result[A] =
fromEither(ea.leftMap(Problem(_)))
def warning[A](warning: String, value: A): Result[A] =
this.warning(Problem(warning), value)
}
}