Skip to content

Commit

Permalink
Remove implicits needed for older ZIO versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvv committed May 17, 2020
1 parent f2e87de commit 4742a04
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -5,7 +5,7 @@ import xerial.sbt.Sonatype._
inThisBuild(
Seq(
organization := "com.github.mvv.zilog",
version := "0.1-M5", // next is M6
version := "0.1-M6", // next is M7
homepage := Some(url("https://github.com/mvv/zilog")),
scmInfo := Some(ScmInfo(url("https://github.com/mvv/zilog"), "scm:git@github.com:mvv/zilog.git")),
licenses := List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
Expand Down
39 changes: 1 addition & 38 deletions core/src/main/scala/com/github/mvv/zilog/package.scala
@@ -1,44 +1,7 @@
package com.github.mvv

import zio.{CanFail, Cause, Has, ZIO, ZTrace}
import zio.Has

package object zilog {
final type Logging = Has[Logging.Service]

implicit final class ZioCauseZilogOps[E](val underlying: Cause[E]) extends AnyVal {
def failureTraceOption: Option[(E, Option[ZTrace])] =
underlying.find {
case Cause.Traced(Cause.Fail(e), trace) => (e, Some(trace))
case Cause.Fail(e) => (e, None)
}
def failureTraceOrCause: Either[(E, Option[ZTrace]), Cause[Nothing]] =
failureTraceOption match {
case Some(errorAndTrace) => Left(errorAndTrace)
case None => Right(underlying.asInstanceOf[Cause[Nothing]])
}
}

implicit final class ZioZilogOps[R, E, A](val underlying: ZIO[R, E, A]) extends AnyVal {
def foldTraceM[R1 <: R, E2, B](
failure: ((E, Option[ZTrace])) => ZIO[R1, E2, B],
success: A => ZIO[R1, E2, B]
)(implicit ev: CanFail[E]): ZIO[R1, E2, B] =
underlying.foldCauseM(_.failureTraceOrCause.fold(failure, ZIO.halt(_)), success)
def tapErrorTrace[R1 <: R, E1 >: E](
f: ((E, Option[ZTrace])) => ZIO[R1, E1, Any]
)(implicit ev: CanFail[E]): ZIO[R1, E1, A] =
underlying.foldCauseM(c => c.failureTraceOrCause.fold(f(_) *> ZIO.halt(c), _ => ZIO.halt(c)),
new ZIO.SucceedFn(f))
def catchAllTrace[R1 <: R, E2, A1 >: A](
h: ((E, Option[ZTrace])) => ZIO[R1, E2, A1]
)(implicit ev: CanFail[E]): ZIO[R1, E2, A1] =
foldTraceM[R1, E2, A1](h, new ZIO.SucceedFn(h))
def catchSomeTrace[R1 <: R, E1 >: E, A1 >: A](
pf: PartialFunction[(E, Option[ZTrace]), ZIO[R1, E1, A1]]
)(implicit ev: CanFail[E]): ZIO[R1, E1, A1] = {
def tryRescue(c: Cause[E]): ZIO[R1, E1, A1] =
c.failureTraceOrCause.fold(t => pf.applyOrElse(t, (_: (E, Option[ZTrace])) => ZIO.halt(c)), ZIO.halt(_))
underlying.foldCauseM[R1, E1, A1](tryRescue, new ZIO.SucceedFn(pf))
}
}
}

0 comments on commit 4742a04

Please sign in to comment.