feat: StringRenderer

This commit is contained in:
DavidOnTop 2024-09-21 09:38:02 +02:00
parent 6515854b30
commit 0c0ed26408
No known key found for this signature in database
GPG key ID: 5D05538A45D5149F
5 changed files with 68 additions and 43 deletions

View file

@ -1,7 +0,0 @@
package top.davidon.sfs.dom
trait AsValue[F, T] {
extension (from: F) {
def asStringValue(): Value[F, String]
}
}

View file

@ -20,39 +20,23 @@ trait ScalaFullStack
object svg extends SvgTags with SvgAttrs with ComplexSvgKeys
given AsValue[String, String] with {
extension (from: String) {
def asStringValue(): Value[String, String] = {
Value(from, StringAsIsCodec)
}
}
given strToVal: Conversion[String, Value[String, String]] with {
def apply(from: String): Value[String, String] =
Value(from, StringAsIsCodec)
}
given AsValue[Int, String] with {
extension (from: Int) {
def asStringValue(): Value[Int, String] = {
Value(from, IntAsStringCodec)
}
}
given intToVal: Conversion[Int, Value[Int, String]] with {
def apply(from: Int): Value[Int, String] = Value(from, IntAsStringCodec)
}
given AsValue[Double, String] with {
extension (from: Double) {
def asStringValue(): Value[Double, String] = {
Value(from, DoubleAsStringCodec)
}
}
given doubleToVal: Conversion[Double, Value[Double, String]] with {
def apply(from: Double): Value[Double, String] =
Value(from, DoubleAsStringCodec)
}
given AsValue[Boolean, String] with {
extension (from: Boolean) {
def asStringValue(): Value[Boolean, String] = {
Value(from, BooleanAsTrueFalseStringCodec)
}
}
given longToVal: Conversion[Long, Value[Long, String]] with {
def apply(from: Long): Value[Long, String] = Value(from, LongAsStringCodec)
}
given AsValue[Iterable[String], String] with {
extension (from: Iterable[String]) {
def asStringValue(): Value[Iterable[String], String] = {
Value(from, IterableAsSpaceSeparatedStringCodec)
}
}
given iterToVal: Conversion[Iterable[String], Value[Iterable[String], String]]
with {
def apply(from: Iterable[String]): Value[Iterable[String], String] =
Value(from, IterableAsSpaceSeparatedStringCodec)
}
}

View file

@ -20,6 +20,17 @@ package object codecs {
override def encode(scalaValue: Double): String = scalaValue.toString
}
lazy val LongAsIsCodec: Codec[Long, Long] = AsIsCodec()
lazy val LongAsStringCodec: Codec[Long, String] =
new Codec[Long, String] {
override def decode(domValue: String): Long =
domValue.toLong // @TODO this can throw exception. How do we handle this?
override def encode(scalaValue: Long): String = scalaValue.toString
}
lazy val BooleanAsTrueFalseStringCodec: Codec[Boolean, String] =
new Codec[Boolean, String] {