mirror of
https://github.com/GayPizzaSpecifications/dough.git
synced 2025-08-17 20:01:32 +00:00
Initial Commit
This commit is contained in:
@ -0,0 +1,40 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.SerializationStrategy
|
||||
|
||||
interface FsOperations {
|
||||
fun exists(path: FsPath): Boolean
|
||||
fun isDirectory(path: FsPath): Boolean
|
||||
fun isRegularFile(path: FsPath): Boolean
|
||||
fun isSymbolicLink(path: FsPath): Boolean
|
||||
fun isReadable(path: FsPath): Boolean
|
||||
fun isWritable(path: FsPath): Boolean
|
||||
fun isExecutable(path: FsPath): Boolean
|
||||
|
||||
fun list(path: FsPath): Sequence<FsPath>
|
||||
|
||||
fun walk(path: FsPath): Sequence<FsPath>
|
||||
fun visit(path: FsPath, visitor: FsPathVisitor)
|
||||
|
||||
fun readString(path: FsPath): String
|
||||
fun readAllBytes(path: FsPath): ByteArray
|
||||
fun readBytesChunked(path: FsPath, block: (ByteArray, Int) -> Unit)
|
||||
fun <T> readJsonFile(path: FsPath, deserializer: DeserializationStrategy<T>): T
|
||||
|
||||
fun readLines(path: FsPath, block: (String) -> Unit)
|
||||
fun readLines(path: FsPath): Sequence<String>
|
||||
|
||||
fun <T> readJsonLinesToList(path: FsPath, deserializer: DeserializationStrategy<T>, block: (T) -> Unit)
|
||||
fun <T> readJsonLinesToList(path: FsPath, deserializer: DeserializationStrategy<T>): List<T>
|
||||
|
||||
fun writeString(path: FsPath, content: String)
|
||||
fun writeAllBytes(path: FsPath, bytes: ByteArray)
|
||||
fun <T> writeJsonFile(path: FsPath, serializer: SerializationStrategy<T>, value: T)
|
||||
|
||||
fun delete(path: FsPath)
|
||||
fun deleteOnExit(path: FsPath)
|
||||
fun deleteRecursively(path: FsPath)
|
||||
|
||||
fun createDirectories(path: FsPath)
|
||||
}
|
19
dough-fs/src/commonMain/kotlin/gay/pizza/dough/fs/FsPath.kt
Normal file
19
dough-fs/src/commonMain/kotlin/gay/pizza/dough/fs/FsPath.kt
Normal file
@ -0,0 +1,19 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable(with = FsPathSerializer::class)
|
||||
interface FsPath : Comparable<FsPath> {
|
||||
val fullPathString: String
|
||||
val entityNameString: String
|
||||
|
||||
val parent: FsPath?
|
||||
val operations: FsOperations
|
||||
|
||||
fun resolve(part: String): FsPath
|
||||
fun relativeTo(path: FsPath): FsPath
|
||||
|
||||
override fun compareTo(other: FsPath): Int {
|
||||
return fullPathString.compareTo(other.fullPathString)
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
|
||||
object FsPathSerializer : KSerializer<FsPath> {
|
||||
override val descriptor: SerialDescriptor = String.serializer().descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): FsPath {
|
||||
return FsPath(decoder.decodeString())
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: FsPath) {
|
||||
encoder.encodeString(value.fullPathString)
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
interface FsPathVisitor {
|
||||
fun beforeVisitDirectory(path: FsPath): VisitResult
|
||||
fun visitFile(path: FsPath): VisitResult
|
||||
fun visitFileFailed(path: FsPath, exception: Exception): VisitResult
|
||||
fun afterVisitDirectory(path: FsPath): VisitResult
|
||||
|
||||
enum class VisitResult {
|
||||
Continue,
|
||||
Terminate,
|
||||
SkipSubtree,
|
||||
SkipSiblings
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
import kotlinx.serialization.DeserializationStrategy
|
||||
import kotlinx.serialization.SerializationStrategy
|
||||
|
||||
fun FsPath.exists(): Boolean =
|
||||
operations.exists(this)
|
||||
|
||||
fun FsPath.isDirectory(): Boolean =
|
||||
operations.isDirectory(this)
|
||||
|
||||
fun FsPath.isRegularFile(): Boolean =
|
||||
operations.isRegularFile(this)
|
||||
|
||||
fun FsPath.isSymbolicLink(): Boolean =
|
||||
operations.isSymbolicLink(this)
|
||||
|
||||
fun FsPath.isReadable(): Boolean =
|
||||
operations.isReadable(this)
|
||||
|
||||
fun FsPath.isWritable(): Boolean =
|
||||
operations.isWritable(this)
|
||||
|
||||
fun FsPath.isExecutable(): Boolean =
|
||||
operations.isExecutable(this)
|
||||
|
||||
fun FsPath.list(): Sequence<FsPath> =
|
||||
operations.list(this)
|
||||
|
||||
fun FsPath.walk(): Sequence<FsPath> =
|
||||
operations.walk(this)
|
||||
|
||||
fun FsPath.visit(visitor: FsPathVisitor): Unit =
|
||||
operations.visit(this, visitor)
|
||||
|
||||
fun FsPath.readString(): String =
|
||||
operations.readString(this)
|
||||
|
||||
fun FsPath.readAllBytes(): ByteArray =
|
||||
operations.readAllBytes(this)
|
||||
|
||||
fun FsPath.readBytesChunked(block: (ByteArray, Int) -> Unit) =
|
||||
operations.readBytesChunked(this, block)
|
||||
|
||||
fun <T> FsPath.readJsonFile(deserializer: DeserializationStrategy<T>): T =
|
||||
operations.readJsonFile(this, deserializer)
|
||||
|
||||
fun FsPath.readLines(block: (String) -> Unit): Unit =
|
||||
operations.readLines(this, block)
|
||||
|
||||
fun FsPath.readLines(): Sequence<String> =
|
||||
operations.readLines(this)
|
||||
|
||||
fun FsPath.readLinesToList(): List<String> {
|
||||
val lines = mutableListOf<String>()
|
||||
readLines { line -> lines.add(line) }
|
||||
return lines
|
||||
}
|
||||
|
||||
fun <T> FsPath.readJsonLinesToList(deserializer: DeserializationStrategy<T>, block: (T) -> Unit) =
|
||||
operations.readJsonLinesToList(this, deserializer, block)
|
||||
|
||||
fun <T> FsPath.readJsonLinesToList(deserializer: DeserializationStrategy<T>): List<T> =
|
||||
operations.readJsonLinesToList(this, deserializer)
|
||||
|
||||
fun FsPath.writeString(content: String): Unit =
|
||||
operations.writeString(this, content)
|
||||
|
||||
fun FsPath.writeAllBytes(bytes: ByteArray): Unit =
|
||||
operations.writeAllBytes(this, bytes)
|
||||
|
||||
fun <T> FsPath.writeJsonFile(serializer: SerializationStrategy<T>, value: T): Unit =
|
||||
operations.writeJsonFile(this, serializer, value)
|
||||
|
||||
fun FsPath.delete(): Unit =
|
||||
operations.delete(this)
|
||||
|
||||
fun FsPath.deleteOnExit(): Unit =
|
||||
operations.delete(this)
|
||||
|
||||
fun FsPath.deleteRecursively(): Unit =
|
||||
operations.deleteRecursively(this)
|
||||
|
||||
fun FsPath.createDirectories(): Unit =
|
||||
operations.createDirectories(this)
|
@ -0,0 +1,3 @@
|
||||
package gay.pizza.dough.fs
|
||||
|
||||
expect fun FsPath(path: String): FsPath
|
Reference in New Issue
Block a user