Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
mio-19 committed Aug 23, 2024
1 parent 57099dc commit b023c4e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package chester.utils.io

import effekt.{Control, Handler}
import typings.node.anon.EncodingFlag
import typings.node.bufferMod.global.BufferEncoding
import typings.node.fsMod
import typings.node.osMod

case class NodeHandlerSync[R]() extends Handler[R] with FileOpsEff {
type P = String

def pathOps = PathOpsString

def read(path: P) = use { k =>
val content = fsMod.readFileSync(path, EncodingFlag(BufferEncoding.utf8))
k(content)
}

def write(path: P, content: String) = use { k =>
fsMod.writeFileSync(path, content)
k(())
}

def append(path: P, content: String) = use { k =>
fsMod.appendFileSync(path, content)
k(())
}

def remove(path: P) = use { k =>
fsMod.unlinkSync(path)
k(())
}

def getHomeDir = use { k =>
k(osMod.homedir())
}
}

inline def nodeHandlerSync[R](inline prog: FileOpsEff ?=> Control[R]): Control[R] = NodeHandlerSync[R]().handle(x => prog(using x))
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ case class NioHandler[R]() extends Handler[R] with NioOps {

}

inline def nioHandler[R](inline prog: FileOpsEff ?=> Control[R]): Control[R] = NioHandler[R]() handle (x => prog(using x))
inline def nioHandler[R](inline prog: FileOpsEff ?=> Control[R]): Control[R] = NioHandler[R]().handle(x => prog(using x))
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension [T](p: T)(using ops: PathOps[T]) {
def /(p2: T): T = ops.join(p, p2)
}

implicit val PathOpsString: PathOps[String] = new PathOps[String] {
implicit object PathOpsString extends PathOps[String] {
def of(path: String): String = path

def join(p1: String, p2: String): String = p1 + "/" + p2
Expand Down

0 comments on commit b023c4e

Please sign in to comment.