aboutsummaryrefslogtreecommitdiff
path: root/yamlesque/src/main/scala/printers.scala
diff options
context:
space:
mode:
Diffstat (limited to 'yamlesque/src/main/scala/printers.scala')
-rw-r--r--yamlesque/src/main/scala/printers.scala46
1 files changed, 46 insertions, 0 deletions
diff --git a/yamlesque/src/main/scala/printers.scala b/yamlesque/src/main/scala/printers.scala
new file mode 100644
index 0000000..0a1c008
--- /dev/null
+++ b/yamlesque/src/main/scala/printers.scala
@@ -0,0 +1,46 @@
+package yamlesque
+
+import annotation.tailrec
+
+class YamlPrinter(compact: Boolean = true) extends (YamlValue => String) {
+
+ def apply(value: YamlValue): String = {
+ val str = new StringBuilder()
+ def p(value: YamlValue, indentation: Int): Unit = value match {
+ case YamlScalar(value) =>
+ str ++= " " * indentation
+ str ++= value
+ str += '\n'
+ case YamlSequence(items) =>
+ for (item <- items) {
+ str ++= " " * indentation
+ item match {
+ case YamlScalar(v) if compact =>
+ str ++= "- "
+ str ++= v
+ str += '\n'
+ case _ =>
+ str ++= "-\n"
+ p(item, indentation + 1)
+ }
+ }
+ case YamlMapping(fields) =>
+ for ((key, value) <- fields) {
+ str ++= " " * indentation
+ str ++= key
+ value match {
+ case YamlScalar(v) if compact =>
+ str ++= ": "
+ str ++= v
+ str += '\n'
+ case _ =>
+ str ++= ":\n"
+ p(value, indentation + 1)
+ }
+ }
+ }
+ p(value, 0)
+ str.toString
+ }
+
+}