Skip to content

Commit

Permalink
Use Encodable in Text instead of Encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Nov 21, 2024
1 parent fd1fae4 commit 87b7b04
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/core/nettlesome.EmailAddress.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import denominative.*
import spectacular.*
import vacuous.*
import fulminate.*
import prepositional.*
import contingency.*
import anticipation.*
import contextual.*
Expand All @@ -36,7 +37,7 @@ object EmailAddress:
given Realm = realm"nettlesome"

given (using Tactic[EmailAddressError]) => Decoder[EmailAddress] = EmailAddress.parse(_)
given Encoder[EmailAddress] = _.text
given EmailAddress is Encodable in Text = _.text

def expand(context: Expr[StringContext])(using Quotes): Expr[EmailAddress] = abandonment:
val text: Text = context.valueOrAbort.parts.head.tt
Expand Down
9 changes: 5 additions & 4 deletions src/core/nettlesome.Nettlesome.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import hypotenuse.*
import spectacular.*
import gossamer.*
import contingency.*
import prepositional.*
import fulminate.*
import anticipation.*
import hieroglyph.*, textMetrics.uniform
Expand Down Expand Up @@ -53,7 +54,7 @@ object Nettlesome:
given Ipv4 is Showable = ip =>
t"${ip.byte0.toString}.${ip.byte1.toString}.${ip.byte2.toString}.${ip.byte3.toString}"

given Encoder[Ipv4] as encoder = _.show
given Ipv4 is Encodable in Text as encodable = _.show
given (using Tactic[IpAddressError]) => Decoder[Ipv4] as decoder = parse(_)

lazy val Localhost: Ipv4 = apply(127, 0, 0, 1)
Expand Down Expand Up @@ -83,7 +84,7 @@ object Nettlesome:
import MacAddressError.Reason.*
erased given Underlying[MacAddress, Long] as underlying = ###
given MacAddress is Showable = _.text
given Encoder[MacAddress] as encoder = _.text
given MacAddress is Encodable in Text as encodable = _.text
given (using Tactic[MacAddressError]) => Decoder[MacAddress] as decoder = parse(_)

def apply(value: Long): MacAddress = value
Expand Down Expand Up @@ -121,7 +122,7 @@ object Nettlesome:
object TcpPort:
erased given Underlying[TcpPort, Int] as underlying = ###
given TcpPort is Showable = port => TextConversion.int.text(port.number)
given Encoder[TcpPort] as encoder = port => TextConversion.int.text(port.number)
given TcpPort is Encodable in Text as encodable = port => TextConversion.int.text(port.number)

given (using Tactic[NumberError], Tactic[PortError]) => Decoder[TcpPort] as decoder =
text => apply(Decoder.int.decode(text))
Expand All @@ -134,7 +135,7 @@ object Nettlesome:
object UdpPort:
erased given Underlying[UdpPort, Int] as underlying = ###
given UdpPort is Showable = port => TextConversion.int.text(port.number)
given Encoder[UdpPort] as encoder = port => TextConversion.int.text(port.number)
given UdpPort is Encodable in Text as encodable = port => TextConversion.int.text(port.number)

given (using Tactic[NumberError], Tactic[PortError]) => Decoder[UdpPort] as decoder =
text => apply(Decoder.int.decode(text))
Expand Down
2 changes: 1 addition & 1 deletion src/url/nettlesome.Url.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ object Url:
=> Decoder[Url[SchemeType]] =
parse(_)

given [SchemeType <: Label]: Encoder[Url[SchemeType]] = _.show
given [SchemeType <: Label] => Url[SchemeType] is Encodable in Text = _.show

given [SchemeType <: Label] => Url[SchemeType] is Teletypeable as teletype =
url => e"$Underline(${Fg(0x00bfff)}(${url.show}))"
Expand Down
8 changes: 4 additions & 4 deletions src/url/nettlesome.UrlFragment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package nettlesome

import anticipation.*
import contextual.*
import spectacular.*
import prepositional.*

object UrlFragment:
def apply(value: Text): UrlFragment = UrlFragment.Textual(value)
Expand All @@ -27,9 +27,9 @@ object UrlFragment:
given Substitution[UrlFragment, Text, "x"] = UrlFragment.Textual(_)
given Substitution[UrlFragment, Raw, "x"] = raw => UrlFragment.RawTextual(raw.text)
given Substitution[UrlFragment, Int, "80"] = UrlFragment.Integral(_)
given [ValueType](using encoder: Encoder[ValueType]) => Substitution[UrlFragment, ValueType, "x"] =
value => UrlFragment.Textual(encoder.encode(value))

given [ValueType: Encodable in Text] => Substitution[UrlFragment, ValueType, "x"] =
value => UrlFragment.Textual(ValueType.encode(value))

enum UrlFragment:
case Integral(value: Int)
Expand Down

0 comments on commit 87b7b04

Please sign in to comment.