@@ -7,7 +7,7 @@ import scala.language.implicitConversions
77import acyclic .skipped
88import os .PathError .{InvalidSegment , NonCanonicalLiteral }
99
10- import scala .util .Try // needed for cross-version defined macros
10+ import scala .util .{ DynamicVariable , Try } // needed for cross-version defined macros
1111
1212trait PathChunk {
1313 def segments : Seq [String ]
@@ -438,6 +438,31 @@ object SubPath extends SubPathMacros {
438438}
439439
440440object Path extends PathMacros {
441+ @ experimental trait Serializer {
442+ def serializeString (p : os.Path ): String
443+ def serializeFile (p : os.Path ): java.io.File
444+ def serializePath (p : os.Path ): java.nio.file.Path
445+ def deserialize (s : String ): java.nio.file.Path
446+ def deserialize (s : java.io.File ): java.nio.file.Path
447+ def deserialize (s : java.nio.file.Path ): java.nio.file.Path
448+ def deserialize (s : java.net.URI ): java.nio.file.Path
449+ }
450+ @ experimental val pathSerializer = new DynamicVariable [Serializer ](defaultPathSerializer)
451+ @ experimental object defaultPathSerializer extends Serializer {
452+ def serializeString (p : os.Path ): String = p.wrapped.toString
453+ def serializeFile (p : os.Path ): java.io.File = p.wrapped.toFile
454+ def serializePath (p : os.Path ): java.nio.file.Path = p.wrapped
455+ def deserialize (s : String ) = Paths .get(s)
456+ def deserialize (s : java.io.File ) = Paths .get(s.getPath)
457+ def deserialize (s : java.nio.file.Path ) = s
458+ def deserialize (s : java.net.URI ) = s.getScheme() match {
459+ case " file" => Paths .get(s)
460+ case uriType =>
461+ throw new IllegalArgumentException (
462+ s """ os.Path can only be created from a "file" URI scheme, but found " ${uriType}" """
463+ )
464+ }
465+ }
441466 def apply (p : FilePath , base : Path ) = p match {
442467 case p : RelPath => base / p
443468 case p : SubPath => base / p
@@ -562,7 +587,7 @@ class Path private[os] (val wrapped: java.nio.file.Path)
562587 val resolved = wrapped.resolve(chunk.toString).normalize()
563588 new Path (resolved)
564589 }
565- override def toString = wrapped.toString
590+ override def toString = Path .pathSerializer.value.serializeString( this )
566591
567592 override def equals (o : Any ): Boolean = o match {
568593 case p : Path => wrapped.equals(p.wrapped)
@@ -593,8 +618,8 @@ class Path private[os] (val wrapped: java.nio.file.Path)
593618 new RelPath (segments.drop(nonUpIndex), nonUpIndex)
594619 }
595620
596- def toIO : java.io.File = wrapped.toFile
597- def toNIO : java.nio.file.Path = wrapped
621+ def toIO : java.io.File = Path .pathSerializer.value.serializeFile( this )
622+ def toNIO : java.nio.file.Path = Path .pathSerializer.value.serializePath( this )
598623
599624 def resolveFrom (base : os.Path ) = this
600625
@@ -608,23 +633,18 @@ sealed trait PathConvertible[T] {
608633
609634object PathConvertible {
610635 implicit object StringConvertible extends PathConvertible [String ] {
611- def apply (t : String ) = Paths .get (t)
636+ def apply (t : String ) = Path .pathSerializer.value.deserialize (t)
612637 }
613638 implicit object JavaIoFileConvertible extends PathConvertible [java.io.File ] {
614- def apply (t : java.io.File ) = Paths .get(t.getPath )
639+ def apply (t : java.io.File ) = Path .pathSerializer.value.deserialize(t )
615640 }
616641 implicit object NioPathConvertible extends PathConvertible [java.nio.file.Path ] {
617- def apply (t : java.nio.file.Path ) = t
642+ def apply (t : java.nio.file.Path ) = Path .pathSerializer.value.deserialize(t)
618643 override def isCustomFs (t : java.nio.file.Path ): Boolean =
619644 t.getFileSystem() != java.nio.file.FileSystems .getDefault()
620645 }
621646 implicit object UriPathConvertible extends PathConvertible [URI ] {
622- def apply (uri : URI ) = uri.getScheme() match {
623- case " file" => Paths .get(uri)
624- case uriType =>
625- throw new IllegalArgumentException (
626- s """ os.Path can only be created from a "file" URI scheme, but found " ${uriType}" """
627- )
628- }
647+ def apply (uri : URI ) = Path .pathSerializer.value.deserialize(uri)
648+
629649 }
630650}
0 commit comments