mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-21 18:08:25 +00:00
Update scalafmt to 2.5.1 + scalafmtAll
This commit is contained in:
@ -37,11 +37,10 @@ object Binary {
|
||||
Binary(name, MimeType.html.withCharset(cs), Stream.chunk(Chunk.byteVector(content)))
|
||||
|
||||
def decode[F[_]](cs: Charset): Pipe[F, Byte, String] =
|
||||
if (cs == StandardCharsets.UTF_8) {
|
||||
if (cs == StandardCharsets.UTF_8)
|
||||
fs2.text.utf8Decode
|
||||
} else {
|
||||
else
|
||||
util.decode[F](cs)
|
||||
}
|
||||
|
||||
def loadAllBytes[F[_]: Sync](data: Stream[F, Byte]): F[ByteVector] =
|
||||
data.chunks.map(_.toByteVector).compile.fold(ByteVector.empty)((r, e) => r ++ e)
|
||||
@ -78,17 +77,16 @@ object Binary {
|
||||
decoder.decode(byteBuffer, charBuffer, false)
|
||||
val nextStream = stream.consChunk(Chunk.byteBuffer(byteBuffer.slice()))
|
||||
Pull.output1(charBuffer.flip().toString).as(Some(nextStream))
|
||||
} else {
|
||||
} else
|
||||
Pull.output(Chunk.empty[String]).as(Some(stream))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private def skipByteOrderMark[F[_]](chunk: Chunk[Byte]): Chunk[Byte] =
|
||||
if (chunk.size >= 3 && chunk.take(3) == utf8Bom) {
|
||||
if (chunk.size >= 3 && chunk.take(3) == utf8Bom)
|
||||
chunk.drop(3)
|
||||
} else chunk
|
||||
else chunk
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -33,12 +33,13 @@ object CollectiveState {
|
||||
def unsafe(str: String): CollectiveState =
|
||||
fromString(str).fold(sys.error, identity)
|
||||
|
||||
def asString(state: CollectiveState): String = state match {
|
||||
case Active => "active"
|
||||
case Blocked => "blocked"
|
||||
case Closed => "closed"
|
||||
case ReadOnly => "readonly"
|
||||
}
|
||||
def asString(state: CollectiveState): String =
|
||||
state match {
|
||||
case Active => "active"
|
||||
case Blocked => "blocked"
|
||||
case Closed => "closed"
|
||||
case ReadOnly => "readonly"
|
||||
}
|
||||
|
||||
implicit val collectiveStateEncoder: Encoder[CollectiveState] =
|
||||
Encoder.encodeString.contramap(CollectiveState.asString)
|
||||
|
@ -25,30 +25,31 @@ object File {
|
||||
): F[Path] =
|
||||
mkDir(parent).map(p => Files.createTempFile(p, prefix, suffix.orNull))
|
||||
|
||||
def deleteDirectory[F[_]: Sync](dir: Path): F[Int] = Sync[F].delay {
|
||||
val count = new AtomicInteger(0)
|
||||
Files.walkFileTree(
|
||||
dir,
|
||||
new SimpleFileVisitor[Path]() {
|
||||
override def visitFile(
|
||||
file: Path,
|
||||
attrs: BasicFileAttributes
|
||||
): FileVisitResult = {
|
||||
Files.deleteIfExists(file)
|
||||
count.incrementAndGet()
|
||||
FileVisitResult.CONTINUE
|
||||
}
|
||||
override def postVisitDirectory(dir: Path, e: IOException): FileVisitResult =
|
||||
Option(e) match {
|
||||
case Some(ex) => throw ex
|
||||
case None =>
|
||||
Files.deleteIfExists(dir)
|
||||
FileVisitResult.CONTINUE
|
||||
def deleteDirectory[F[_]: Sync](dir: Path): F[Int] =
|
||||
Sync[F].delay {
|
||||
val count = new AtomicInteger(0)
|
||||
Files.walkFileTree(
|
||||
dir,
|
||||
new SimpleFileVisitor[Path]() {
|
||||
override def visitFile(
|
||||
file: Path,
|
||||
attrs: BasicFileAttributes
|
||||
): FileVisitResult = {
|
||||
Files.deleteIfExists(file)
|
||||
count.incrementAndGet()
|
||||
FileVisitResult.CONTINUE
|
||||
}
|
||||
}
|
||||
)
|
||||
count.get
|
||||
}
|
||||
override def postVisitDirectory(dir: Path, e: IOException): FileVisitResult =
|
||||
Option(e) match {
|
||||
case Some(ex) => throw ex
|
||||
case None =>
|
||||
Files.deleteIfExists(dir)
|
||||
FileVisitResult.CONTINUE
|
||||
}
|
||||
}
|
||||
)
|
||||
count.get
|
||||
}
|
||||
|
||||
def exists[F[_]: Sync](file: Path): F[Boolean] =
|
||||
Sync[F].delay(Files.exists(file))
|
||||
|
@ -20,12 +20,13 @@ object Ident {
|
||||
def randomUUID[F[_]: Sync]: F[Ident] =
|
||||
Sync[F].delay(unsafe(UUID.randomUUID.toString))
|
||||
|
||||
def randomId[F[_]: Sync]: F[Ident] = Sync[F].delay {
|
||||
val random = new SecureRandom()
|
||||
val buffer = new Array[Byte](32)
|
||||
random.nextBytes(buffer)
|
||||
unsafe(ByteVector.view(buffer).toBase58.grouped(11).mkString("-"))
|
||||
}
|
||||
def randomId[F[_]: Sync]: F[Ident] =
|
||||
Sync[F].delay {
|
||||
val random = new SecureRandom()
|
||||
val buffer = new Array[Byte](32)
|
||||
random.nextBytes(buffer)
|
||||
unsafe(ByteVector.view(buffer).toBase58.grouped(11).mkString("-"))
|
||||
}
|
||||
|
||||
def apply(str: String): Either[String, Ident] =
|
||||
fromString(str)
|
||||
|
@ -122,11 +122,12 @@ object LenientUri {
|
||||
val isRoot = false
|
||||
def /(seg: String): Path =
|
||||
copy(segs = segs.append(seg))
|
||||
def asString = segs.head match {
|
||||
case "." => segments.map(percentEncode).mkString("/")
|
||||
case ".." => segments.map(percentEncode).mkString("/")
|
||||
case _ => "/" + segments.map(percentEncode).mkString("/")
|
||||
}
|
||||
def asString =
|
||||
segs.head match {
|
||||
case "." => segments.map(percentEncode).mkString("/")
|
||||
case ".." => segments.map(percentEncode).mkString("/")
|
||||
case _ => "/" + segments.map(percentEncode).mkString("/")
|
||||
}
|
||||
}
|
||||
|
||||
def unsafe(str: String): LenientUri =
|
||||
@ -136,16 +137,17 @@ object LenientUri {
|
||||
unsafe(u.toExternalForm)
|
||||
|
||||
def parse(str: String): Either[String, LenientUri] = {
|
||||
def makePath(str: String): Path = str.trim match {
|
||||
case "/" => RootPath
|
||||
case "" => EmptyPath
|
||||
case _ =>
|
||||
NonEmptyList
|
||||
.fromList(stripLeading(str, '/').split('/').toList.map(percentDecode)) match {
|
||||
case Some(nl) => NonEmptyPath(nl)
|
||||
case None => sys.error(s"Invalid url: $str")
|
||||
}
|
||||
}
|
||||
def makePath(str: String): Path =
|
||||
str.trim match {
|
||||
case "/" => RootPath
|
||||
case "" => EmptyPath
|
||||
case _ =>
|
||||
NonEmptyList
|
||||
.fromList(stripLeading(str, '/').split('/').toList.map(percentDecode)) match {
|
||||
case Some(nl) => NonEmptyPath(nl)
|
||||
case None => sys.error(s"Invalid url: $str")
|
||||
}
|
||||
}
|
||||
|
||||
def makeNonEmpty(str: String): Option[String] =
|
||||
Option(str).filter(_.nonEmpty)
|
||||
|
@ -17,24 +17,25 @@ trait Logger[F[_]] {
|
||||
|
||||
object Logger {
|
||||
|
||||
def log4s[F[_]: Sync](log: Log4sLogger): Logger[F] = new Logger[F] {
|
||||
def trace(msg: => String): F[Unit] =
|
||||
log.ftrace(msg)
|
||||
def log4s[F[_]: Sync](log: Log4sLogger): Logger[F] =
|
||||
new Logger[F] {
|
||||
def trace(msg: => String): F[Unit] =
|
||||
log.ftrace(msg)
|
||||
|
||||
def debug(msg: => String): F[Unit] =
|
||||
log.fdebug(msg)
|
||||
def debug(msg: => String): F[Unit] =
|
||||
log.fdebug(msg)
|
||||
|
||||
def info(msg: => String): F[Unit] =
|
||||
log.finfo(msg)
|
||||
def info(msg: => String): F[Unit] =
|
||||
log.finfo(msg)
|
||||
|
||||
def warn(msg: => String): F[Unit] =
|
||||
log.fwarn(msg)
|
||||
def warn(msg: => String): F[Unit] =
|
||||
log.fwarn(msg)
|
||||
|
||||
def error(ex: Throwable)(msg: => String): F[Unit] =
|
||||
log.ferror(ex)(msg)
|
||||
def error(ex: Throwable)(msg: => String): F[Unit] =
|
||||
log.ferror(ex)(msg)
|
||||
|
||||
def error(msg: => String): F[Unit] =
|
||||
log.ferror(msg)
|
||||
}
|
||||
def error(msg: => String): F[Unit] =
|
||||
log.ferror(msg)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -47,17 +47,20 @@ object SystemCommand {
|
||||
for {
|
||||
_ <- writeToProcess(stdin, proc, blocker)
|
||||
term <- Sync[F].delay(proc.waitFor(cmd.timeout.seconds, TimeUnit.SECONDS))
|
||||
_ <- if (term)
|
||||
logger.debug(s"Command `${cmd.cmdString}` finished: ${proc.exitValue}")
|
||||
else
|
||||
logger.warn(
|
||||
s"Command `${cmd.cmdString}` did not finish in ${cmd.timeout.formatExact}!"
|
||||
)
|
||||
_ <-
|
||||
if (term)
|
||||
logger.debug(s"Command `${cmd.cmdString}` finished: ${proc.exitValue}")
|
||||
else
|
||||
logger.warn(
|
||||
s"Command `${cmd.cmdString}` did not finish in ${cmd.timeout.formatExact}!"
|
||||
)
|
||||
_ <- if (!term) timeoutError(proc, cmd) else Sync[F].pure(())
|
||||
out <- if (term) inputStreamToString(proc.getInputStream, blocker)
|
||||
else Sync[F].pure("")
|
||||
err <- if (term) inputStreamToString(proc.getErrorStream, blocker)
|
||||
else Sync[F].pure("")
|
||||
out <-
|
||||
if (term) inputStreamToString(proc.getInputStream, blocker)
|
||||
else Sync[F].pure("")
|
||||
err <-
|
||||
if (term) inputStreamToString(proc.getErrorStream, blocker)
|
||||
else Sync[F].pure("")
|
||||
} yield Result(proc.exitValue, out, err)
|
||||
}
|
||||
}
|
||||
|
@ -22,10 +22,11 @@ object UserState {
|
||||
def unsafe(str: String): UserState =
|
||||
fromString(str).fold(sys.error, identity)
|
||||
|
||||
def asString(s: UserState): String = s match {
|
||||
case Active => "active"
|
||||
case Disabled => "disabled"
|
||||
}
|
||||
def asString(s: UserState): String =
|
||||
s match {
|
||||
case Active => "active"
|
||||
case Disabled => "disabled"
|
||||
}
|
||||
|
||||
implicit val userStateEncoder: Encoder[UserState] =
|
||||
Encoder.encodeString.contramap(UserState.asString)
|
||||
|
@ -3,17 +3,19 @@ package docspell.common.syntax
|
||||
trait EitherSyntax {
|
||||
|
||||
implicit final class LeftStringEitherOps[A](e: Either[String, A]) {
|
||||
def throwLeft: A = e match {
|
||||
case Right(a) => a
|
||||
case Left(err) => sys.error(err)
|
||||
}
|
||||
def throwLeft: A =
|
||||
e match {
|
||||
case Right(a) => a
|
||||
case Left(err) => sys.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
implicit final class ThrowableLeftEitherOps[A](e: Either[Throwable, A]) {
|
||||
def throwLeft: A = e match {
|
||||
case Right(a) => a
|
||||
case Left(err) => throw err
|
||||
}
|
||||
def throwLeft: A =
|
||||
e match {
|
||||
case Right(a) => a
|
||||
case Left(err) => throw err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,9 +16,10 @@ trait StreamSyntax {
|
||||
.last
|
||||
.map(optStr =>
|
||||
for {
|
||||
str <- optStr
|
||||
.map(_.trim)
|
||||
.toRight(new Exception("Empty string cannot be parsed into a value"))
|
||||
str <-
|
||||
optStr
|
||||
.map(_.trim)
|
||||
.toRight(new Exception("Empty string cannot be parsed into a value"))
|
||||
json <- parse(str).leftMap(_.underlying)
|
||||
value <- json.as[A]
|
||||
} yield value
|
||||
|
Reference in New Issue
Block a user