Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
Implement FilePathScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
sd-yip committed Feb 28, 2019
1 parent 5ea19e4 commit d1e061b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
2 changes: 2 additions & 0 deletions nio/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies {
api(project(":core", "default"))
api(project(":file", "default"))

compileOnly("com.github.mpilquist:simulacrum_2.12:0.14.0")

testImplementation("junit:junit:4.12")
testImplementation("org.scalatest:scalatest_2.12:3.0.5")
testCompileOnly("com.chuusai:shapeless_2.12:2.3.3")
Expand Down
39 changes: 37 additions & 2 deletions nio/src/main/scala/eyrie/nio/context/Sys.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package eyrie.nio.context

import java.nio.file.{FileSystem, FileSystems}
import java.nio.file._

import eyrie.nio.ops.JavaMirror
import eyrie.nio.FilePath
import eyrie.nio.FilePath.Internal
import eyrie.nio.ops.{FilePathScheme, JavaMirror}

sealed trait Sys

Expand All @@ -23,4 +25,37 @@ object Sys {
def asJava: FileSystem =
FileSystems.getDefault
}

private
def relative(path: Path): FilePath[Sys] =
if (path.getNameCount < 2 && Option(path.getFileName).forall(_.toString.isEmpty)) {
Internal.IdentityFilePath(path)
} else {
Internal.RelativeFile(path)
}

private
def absolute(path: Path): FilePath[Sys] =
if (path.getNameCount < 1) {
Internal.RootDirectory(path)
} else {
Internal.AbsoluteFile(path)
}

implicit val eyrieNioFilePathScheme: FilePathScheme[Sys] =
string =>
try {
Some {
val parsed = Paths.get(string)

if (parsed.isAbsolute) {
absolute(parsed)
} else {
relative(parsed)
}
}
} catch {
case _: InvalidPathException =>
None
}
}
9 changes: 9 additions & 0 deletions nio/src/main/scala/eyrie/nio/ops/FilePathScheme.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package eyrie.nio.ops

import eyrie.nio.FilePath
import simulacrum.typeclass

@typeclass
trait FilePathScheme[A] {
def getPath(string: String): Option[FilePath[A]]
}
14 changes: 14 additions & 0 deletions nio/src/main/scala/eyrie/nio/syntax/FilePathSchemeSyntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eyrie.nio.syntax

import eyrie.nio.FilePath
import eyrie.nio.ops.FilePathScheme

trait FilePathSchemeSyntax {
implicit def toFilePathSchemeOps(s: String): FilePathSchemeOps =
new FilePathSchemeOps(s)
}

final class FilePathSchemeOps(private val s: String) extends AnyVal {
def toFilePath[A](implicit A: FilePathScheme[A]): Option[FilePath[A]] =
A.getPath(s)
}
1 change: 1 addition & 0 deletions nio/src/main/scala/eyrie/nio/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package eyrie.nio

package object syntax {
object all extends AllSyntax
object filePathScheme extends FilePathSchemeSyntax
object javaMirror extends JavaMirrorSyntax
}
17 changes: 17 additions & 0 deletions nio/src/test/scala/eyrie/nio/test/FilePathSchemeSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eyrie.nio.test

import eyrie.nio.context.Sys
import org.junit.runner.RunWith
import org.scalatest.FreeSpec
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class FilePathSchemeSpec extends FreeSpec {
import eyrie.nio.syntax.filePathScheme._

"String" - {
"toFilePath should work" in {
"a/b/c".toFilePath[Sys]
}
}
}

0 comments on commit d1e061b

Please sign in to comment.