Skip to content

Commit

Permalink
Base64Writer and FileWriter use DefaultFrame
Browse files Browse the repository at this point in the history
- Adjust `Base64Writer` and `FileWriter` effects to use `DefaultFrame`
- Adjust implementations to remove unnecessary methods
- Adjust syntax as necessary
  • Loading branch information
noelwelsh committed Aug 12, 2024
1 parent 7e89c21 commit fee83df
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import doodle.algebra.Picture
import doodle.core.format.Format
import doodle.core.{Base64 as B64}
import doodle.effect.Base64Writer
import doodle.effect.DefaultFrame

trait Base64WriterSyntax {
implicit class Base64Ops[Alg <: Algebra, A](
Expand All @@ -34,6 +35,7 @@ trait Base64WriterSyntax {
class Base64OpsHelper[Fmt <: Format](picture: Picture[Alg, A]) {
def apply[Frame]()(implicit
w: Base64Writer[Alg, Frame, Fmt],
f: DefaultFrame[Frame],
r: IORuntime
): (A, B64[Fmt]) =
w.base64(picture).unsafeRunSync()
Expand All @@ -47,7 +49,8 @@ trait Base64WriterSyntax {

class Base64IOOpsHelper[Fmt <: Format](picture: Picture[Alg, A]) {
def apply[Frame]()(implicit
w: Base64Writer[Alg, Frame, Fmt]
w: Base64Writer[Alg, Frame, Fmt],
f: DefaultFrame[Frame]
): IO[(A, B64[Fmt])] =
w.base64(picture)

Expand Down
9 changes: 7 additions & 2 deletions core/jvm/src/main/scala/doodle/syntax/FileWriterSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import cats.effect.unsafe.IORuntime
import doodle.algebra.Algebra
import doodle.algebra.Picture
import doodle.core.format.Format
import doodle.effect.DefaultFrame
import doodle.effect.FileWriter

import java.io.File
Expand All @@ -35,12 +36,14 @@ trait FileWriterSyntax {
class FileWriterOpsHelper[Fmt <: Format](picture: Picture[Alg, A]) {
def apply[Frame](file: String)(implicit
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame],
r: IORuntime
): A =
apply(new File(file))

def apply[Frame](file: File)(implicit
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame],
r: IORuntime
): A =
w.write(file, picture).unsafeRunSync()
Expand All @@ -60,12 +63,14 @@ trait FileWriterSyntax {

class FileWriterIOOpsHelper[Fmt <: Format](picture: Picture[Alg, A]) {
def apply[Frame](file: String)(implicit
w: FileWriter[Alg, Frame, Fmt]
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame]
): IO[A] =
apply(new File(file))

def apply[Frame](file: File)(implicit
w: FileWriter[Alg, Frame, Fmt]
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame]
): IO[A] =
w.write(file, picture)

Expand Down
6 changes: 5 additions & 1 deletion core/shared/src/main/scala/doodle/effect/Base64Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ trait Base64Writer[+Alg <: Algebra, Frame, Fmt <: Format]
description: Frame,
picture: Picture[Alg, A]
): IO[(A, B64[Fmt])]
def base64[A](picture: Picture[Alg, A]): IO[(A, B64[Fmt])]

def base64[A](picture: Picture[Alg, A])(using
frame: DefaultFrame[Frame]
): IO[(A, B64[Fmt])] =
base64(frame.default, picture)
}
8 changes: 6 additions & 2 deletions core/shared/src/main/scala/doodle/effect/FileWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import java.io.File
*/
trait FileWriter[+Alg <: Algebra, Frame, Fmt <: Format]
extends Writer[Alg, Frame] {
def write[A](file: File, description: Frame, image: Picture[Alg, A]): IO[A]
def write[A](file: File, image: Picture[Alg, A]): IO[A]
def write[A](file: File, description: Frame, picture: Picture[Alg, A]): IO[A]

def write[A](file: File, picture: Picture[Alg, A])(using
frame: DefaultFrame[Frame]
): IO[A] =
write(file, frame.default, picture)
}
17 changes: 14 additions & 3 deletions image/jvm/src/main/scala/doodle/image/syntax/JvmImageSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import doodle.algebra.Picture
import doodle.core.format.Format
import doodle.core.{Base64 as B64}
import doodle.effect.Base64Writer
import doodle.effect.DefaultFrame
import doodle.effect.FileWriter
import doodle.language.Basic

Expand All @@ -41,6 +42,7 @@ class JvmImageSyntax extends AbstractImageSyntax(doodle.syntax.renderer) {
final class Base64Ops[Fmt <: Format](image: Image) {
def apply[Alg <: Basic, Frame](implicit
w: Base64Writer[Alg, Frame, Fmt],
f: DefaultFrame[Frame],
runtime: IORuntime
): B64[Fmt] = {
val picture = new Picture[Basic, Unit] {
Expand All @@ -66,6 +68,7 @@ class JvmImageSyntax extends AbstractImageSyntax(doodle.syntax.renderer) {
final class ImageWriterUnitOps[Fmt <: Format](image: Image) {
def apply[Alg <: Basic, Frame](file: String)(implicit
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame],
r: IORuntime
): Unit =
apply(new File(file))
Expand All @@ -78,7 +81,11 @@ class JvmImageSyntax extends AbstractImageSyntax(doodle.syntax.renderer) {

def apply[Alg <: Basic, Frame](
file: File
)(implicit w: FileWriter[Alg, Frame, Fmt], r: IORuntime): Unit =
)(implicit
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame],
r: IORuntime
): Unit =
image.compile[Alg].write[Fmt](file)

def apply[Alg <: Basic, Frame](file: File, frame: Frame)(implicit
Expand All @@ -94,7 +101,8 @@ class JvmImageSyntax extends AbstractImageSyntax(doodle.syntax.renderer) {
*/
final class ImageWriterIOOps[Fmt <: Format](image: Image) {
def apply[Alg <: Basic, Frame](file: String)(implicit
w: FileWriter[Alg, Frame, Fmt]
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame]
): IO[Unit] =
apply(new File(file))

Expand All @@ -105,7 +113,10 @@ class JvmImageSyntax extends AbstractImageSyntax(doodle.syntax.renderer) {

def apply[Alg <: Basic, Frame](
file: File
)(implicit w: FileWriter[Alg, Frame, Fmt]): IO[Unit] =
)(implicit
w: FileWriter[Alg, Frame, Fmt],
f: DefaultFrame[Frame]
): IO[Unit] =
image.compile[Alg].writeToIO[Fmt](file)

def apply[Alg <: Basic, Frame](file: File, frame: Frame)(implicit
Expand Down
7 changes: 0 additions & 7 deletions java2d/src/main/scala/doodle/java2d/effect/Java2dWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ trait Java2dWriter[Fmt <: Format]
// with alpha channels as described in https://bugs.openjdk.java.net/browse/JDK-8119048
def makeImage(w: Int, h: Int): BufferedImage

def write[A](file: File, picture: Picture[A]): IO[A] = {
write(file, Frame.default.withSizedToPicture(20), picture)
}

def write[A](file: File, frame: Frame, picture: Picture[A]): IO[A] = {
for {
result <- Java2d.renderBufferedImage(
Expand All @@ -70,9 +66,6 @@ trait Java2dWriter[Fmt <: Format]
base64 = JBase64.getEncoder.encodeToString(output.toByteArray)
} yield (value, B64[Fmt](base64))

def base64[A](image: Picture[A]): IO[(A, B64[Fmt])] =
base64(Frame.default.withSizedToPicture(20), image)

private def writeToOutput[A](
output: OutputStream,
frame: Frame,
Expand Down
8 changes: 0 additions & 8 deletions svg/jvm/src/main/scala/doodle/svg/effect/SvgWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ object SvgWriter
}
}

def write[A](file: File, picture: Picture[Algebra, A]): IO[A] =
write(file, Frame("").withSizedToPicture(), picture)

def base64[A](
frame: Frame,
image: Picture[Algebra, A]
Expand All @@ -59,9 +56,4 @@ object SvgWriter
(nodes, value) = rendered
b64 = JBase64.getEncoder.encodeToString(nodes.getBytes())
} yield (value, B64[format.Svg](b64))

def base64[A](
picture: Picture[Algebra, A]
): IO[(A, B64[format.Svg])] =
base64(Frame("").withSizedToPicture(), picture)
}

0 comments on commit fee83df

Please sign in to comment.