]> gitweb @ CieloNegro.org - task-reporter.git/blob - src/main/scala/jp/ymir/taskReporter/core/LTSV.scala
adopt the new format
[task-reporter.git] / src / main / scala / jp / ymir / taskReporter / core / LTSV.scala
1 package jp.ymir.taskReporter.core
2 import scala.collection.immutable.StringLike
3 import jp.ymir.taskReporter.core._
4
5 trait LTSVFunctions {
6   import TSV._
7
8   def decode[A: FromRecord](tsv: StringLike[_]): Seq[A] = {
9     def toTuple(col: String): (Symbol, Field) = {
10       val pattern = """^([^:]+):(.*)$""".r
11       col match {
12         case pattern(label, value) =>
13           (Symbol(label), Field(value))
14       }
15     }
16     val lines   = tsv.split('\n').filter(!_.isEmpty)
17     val records = lines.map(line => Record(line.split('\t').map(toTuple).toMap))
18     return records.map { r =>
19       runParser(r.parseRecord[A]) match {
20         case Right(a) => a
21         case Left(e)  => throw new DecodeFailedException(e)
22       }
23     }
24   }
25 }
26
27 object LTSV extends TSVTypes with LTSVFunctions