…lines 1–30 unchanged (30 lines)
31 def get = value
32 def toOption: Option[T] = Some(value)
33 def toEither: Either[String, T] = Right(value)
34 def errorOpt: Option[String] = None
35 }
36 final case class Failure(
37 error: String,
38 @com.lihaoyi.unroll path: java.nio.file.Path = null,
39 index: Int = -1,
40 next: Option[Failure] = None
41 ) extends Result[Nothing] {
42 def map[V](f: Nothing => V): Result[Nothing] = this
43
44 def flatMap[V](f: Nothing => Result[V]): Result[Nothing] = this
45 def get = sys.error(error)
46 def toOption: Option[Nothing] = None
47 def toEither: Either[String, Nothing] = Left(error)
48 def errorOpt: Option[String] = Some(error)
49 }
50
51 object Failure {
52 def combine(failures: Seq[Failure]): Failure = {
53 val flattened: Seq[Failure] = failures.flatMap(f =>
54 Iterator.unfold(Option(f))(_.map(t => t -> t.next))
55 )
56 flattened
57 .foldLeft(Option.empty[Failure])((f0, f) => Some(Failure(f.error, f.path, f.index, f0)))
58 .get
59 }
60 }
61
62 def fromEither[T](either: Either[String, T]) = either match {
63 case Left(err) => Result.Failure(err)
64 case Right(value) => Result.Success(value)
65 }
66
…lines 67–75 unchanged (9 lines)
76 ): Result[M[B]] = {
77 boundary {
78 val builder = factory.newBuilder
79 builder.sizeHint(in)
80 in.iterator.foreach {
81 case Success(b) => builder += b
82 case f: Failure => boundary.break(f)
83 }
84
85 builder.result()
86 }
87 }
88
…lines 89–93 unchanged (5 lines)
94 )(using factory: Factory[B, Collection[B]]): Result[Collection[B]] = {
95 boundary {
96 val builder = factory.newBuilder
97 builder.sizeHint(collection)
98 collection.iterator.map(_.flatMap(f)).foreach {
99 case Success(b) => builder += b
100 case f: Failure => boundary.break(f)
101 }
102
103 builder.result()
104 }
105 }
106
107 final class Exception(val error: String) extends java.lang.Exception(error)
108}
109