]> gitweb @ CieloNegro.org - task-reporter.git/blobdiff - 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
diff --git a/src/main/scala/jp/ymir/taskReporter/core/LTSV.scala b/src/main/scala/jp/ymir/taskReporter/core/LTSV.scala
new file mode 100644 (file)
index 0000000..5550c54
--- /dev/null
@@ -0,0 +1,27 @@
+package jp.ymir.taskReporter.core
+import scala.collection.immutable.StringLike
+import jp.ymir.taskReporter.core._
+
+trait LTSVFunctions {
+  import TSV._
+
+  def decode[A: FromRecord](tsv: StringLike[_]): Seq[A] = {
+    def toTuple(col: String): (Symbol, Field) = {
+      val pattern = """^([^:]+):(.*)$""".r
+      col match {
+        case pattern(label, value) =>
+          (Symbol(label), Field(value))
+      }
+    }
+    val lines   = tsv.split('\n').filter(!_.isEmpty)
+    val records = lines.map(line => Record(line.split('\t').map(toTuple).toMap))
+    return records.map { r =>
+      runParser(r.parseRecord[A]) match {
+        case Right(a) => a
+        case Left(e)  => throw new DecodeFailedException(e)
+      }
+    }
+  }
+}
+
+object LTSV extends TSVTypes with LTSVFunctions