Skip to content

Commit

Permalink
Intro safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongl committed Sep 16, 2024
1 parent db2e601 commit 7fa4c28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
16 changes: 16 additions & 0 deletions src/core/Safety.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core

import scala.scalajs.js

class Safety private (d: js.Dynamic) extends Selectable:
def selectDynamic(name: String): Any =
js.typeOf(d) match
case "object" =>
val v = d.selectDynamic(name)
if js.isUndefined(v) then throw NoSuchFieldException(name) else v
case t =>
throw UnsupportedOperationException(s"get $name from $t")

object Safety:
def apply[T <: Safety](d: js.Dynamic) = new Safety(d).asInstanceOf[T]
def apply[A <: Safety, B](f: A => B): js.Dynamic => B = d => f(Safety[A](d))
28 changes: 18 additions & 10 deletions src/garmin/read/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package garmin.read
import scala.scalajs.js

import core.*
import core.Safety

given Read[Gauge.BeatPerMinute, js.Dynamic, Double] = Read: a =>
a.averageHR.asInstanceOf[Double].round
given Read[Gauge.BeatPerMinute, js.Dynamic, Double] = Read:
Safety[Lap, Double](_.averageHR.round)

given Read[Gauge.StepPerMinute, js.Dynamic, Double] = Read: a =>
a.averageRunCadence.asInstanceOf[Double].round
given Read[Gauge.StepPerMinute, js.Dynamic, Double] = Read:
Safety[Lap, Double](_.averageRunCadence.round)

given Read[Distance, js.Dynamic, Double] = Read: a =>
a.distance.asInstanceOf[Double]
given Read[Distance, js.Dynamic, Double] = Read:
Safety[Lap, Double](_.distance.round)

given Read[Duration, js.Dynamic, Double] = Read: a =>
a.duration.asInstanceOf[Double]
given Read[Duration, js.Dynamic, Double] = Read:
Safety[Lap, Double](_.duration.round)

given Read[Timestamp, js.Dynamic, String] = Read: a =>
a.startTimeGMT.asInstanceOf[String]
given Read[Timestamp, js.Dynamic, String] = Read:
Safety[Lap, String](_.startTimeGMT)

given [A](using
Read[Distance, A, Double],
Expand All @@ -41,3 +42,10 @@ given [A](using
end given

extension (d: Double) private inline def round = js.Math.round(d)

private type Lap = Safety:
val averageHR: Double
val averageRunCadence: Double
val distance: Double
val duration: Double
val startTimeGMT: String

0 comments on commit 7fa4c28

Please sign in to comment.