
spice.net.package.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spice-core_3 Show documentation
Show all versions of spice-core_3 Show documentation
Core functionality leveraged and shared by most other sub-projects of YouI.
package spice
import org.typelevel.literally.Literally
import spice.net.PortLiteral
package object net {
extension (inline ctx: StringContext)
inline def port(inline args: Any*): Port =
${PortLiteral('ctx, 'args)}
inline def ip(inline args: Any*): IP =
${IPLiteral('ctx, 'args)}
inline def path(inline args: Any*): Path =
${PathLiteral('ctx, 'args)}
inline def url(inline args: Any*): URL =
${URLLiteral('ctx, 'args)}
object PortLiteral extends Literally[Port]:
def validate(s: String)(using Quotes): Either[String, Expr[Port]] =
s.toIntOption.flatMap(Port.fromInt) match
case None => Left(s"Invalid port - must be integer between ${Port.MinValue} and ${Port.MaxValue}")
case Some(_) => Right('{Port.fromInt(${Expr(s)}.toInt).get})
object IPLiteral extends Literally[IP]:
def validate(s: String)(using Quotes): Either[String, Expr[IP]] =
IP.fromString(s) match
case None => Left(s"Invalid IP address: $s")
case Some(_) => Right('{IP.fromString(${Expr(s)}).get})
object PathLiteral extends Literally[Path]:
def validate(s: String)(using Quotes): Either[String, Expr[Path]] =
Right('{Path.parse(${Expr(s)})})
object URLLiteral extends Literally[URL]:
def validate(s: String)(using Quotes): Either[String, Expr[URL]] =
URL.get(s) match
case Left(f) => Left(f.message)
case Right(_) => Right('{URL(${Expr(s)})})
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy