io.taig.taigless.geo.circe.scala Maven / Gradle / Ivy
The newest version!
package io.taig.taigless.geo
import java.net.{MalformedURLException, URL}
import cats.syntax.all._
import io.circe.{Decoder, Encoder}
trait circe {
implicit val decoderLatitude: Decoder[Latitude] = Decoder[Double].map(Latitude.apply)
implicit val encoderLatitude: Encoder[Latitude] = Encoder[Double].contramap(_.value)
implicit val decoderLongitude: Decoder[Longitude] = Decoder[Double].map(Longitude.apply)
implicit val encoderLongitude: Encoder[Longitude] = Encoder[Double].contramap(_.value)
implicit val decoderPosition: Decoder[Position] =
Decoder.forProduct2[Position, Latitude, Longitude]("latitude", "longitude")(Position.apply)
implicit val encoderPosition: Encoder[Position] =
Encoder.forProduct2[Position, Latitude, Longitude]("latitude", "longitude") { position =>
(position.latitude, position.longitude)
}
implicit private val decoderURL: Decoder[URL] = Decoder[String].emap { value =>
Either.catchOnly[MalformedURLException](new URL(value)).leftMap(_.getMessage)
}
implicit private val encoderURL: Encoder[URL] = Encoder[String].contramap(_.toExternalForm)
implicit def decoderGeoLocation[A: Decoder]: Decoder[Geo.Location[A]] =
Decoder.forProduct4[Geo.Location[A], A, String, Position, URL]("identifier", "formatted", "position", "url")(
Geo.Location.apply
)
implicit def encoderGeoLocation[A: Encoder]: Encoder[Geo.Location[A]] =
Encoder.forProduct4[Geo.Location[A], A, String, Position, URL]("identifier", "formatted", "position", "url") {
location =>
(location.identifier, location.formatted, location.position, location.url)
}
}
object circe extends circe