remove redundant pattern match

we get the attributes using the cursor, so no need to parse
it for the node
This commit is contained in:
Daniel Barlow 2024-10-31 17:25:01 +00:00
parent ff4451cb6f
commit 6e4073ca7a

View File

@ -61,40 +61,37 @@ instance Exception BadFile
elToPoint :: Cursor -> Either SomeException Point
elToPoint c =
case node c of
NodeElement (Element _ attrs _) ->
let lat = listToMaybe (attribute "lat" c) >>= asFloat
lon = listToMaybe (attribute "lon" c) >>= asFloat
ele = listToMaybe (child c >>= element (gpxNS "ele") >>= child >>= content) >>= asFloat
ts =
listToMaybe (child c >>= element (gpxNS "time") >>= child >>= content)
>>= (Data.Time.ISO8601.parseISO8601 . Data.Text.unpack)
gpxtpx =
child c
>>= element (gpxNS "extensions")
>>= child
>>= element (tpxNS "TrackPointExtension")
>>= child
extn n =
gpxtpx >>= element n >>= child >>= content
let lat = listToMaybe (attribute "lat" c) >>= asFloat
lon = listToMaybe (attribute "lon" c) >>= asFloat
ts =
listToMaybe (child c >>= element (gpxNS "time") >>= child >>= content)
>>= (Data.Time.ISO8601.parseISO8601 . Data.Text.unpack)
ele = listToMaybe (child c >>= element (gpxNS "ele") >>= child >>= content) >>= asFloat
gpxtpx =
child c
>>= element (gpxNS "extensions")
>>= child
>>= element (tpxNS "TrackPointExtension")
>>= child
extn n =
gpxtpx >>= element n >>= child >>= content
cadence = extn (tpxNS "cad")
hr = extn (tpxNS "hr")
power = extn "{http://www.garmin.com/xmlschemas/PowerExtension/v1}PowerInWatts"
in if isJust lat && isJust lon && isJust ts
then
Right $
Point
(Pos (fromJust lat) (fromJust lon) ele)
(fromJust ts)
(listToMaybe cadence >>= asInt)
(listToMaybe power >>= asInt)
(listToMaybe hr >>= asInt)
else Left (toException (BadFile "missing a required attribute"))
where
asFloat v = (readMaybe :: String -> Maybe Float) (Data.Text.unpack v)
asInt v = (readMaybe :: String -> Maybe Int) (Data.Text.unpack v)
_ -> Left (toException (BadFile "did not find trkpt"))
cadence = extn (tpxNS "cad")
hr = extn (tpxNS "hr")
power = extn "{http://www.garmin.com/xmlschemas/PowerExtension/v1}PowerInWatts"
in if isJust lat && isJust lon && isJust ts
then
Right $
Point
(Pos (fromJust lat) (fromJust lon) ele)
(fromJust ts)
(listToMaybe cadence >>= asInt)
(listToMaybe power >>= asInt)
(listToMaybe hr >>= asInt)
else Left (toException (BadFile "missing a required attribute"))
where
asFloat v = (readMaybe :: String -> Maybe Float) (Data.Text.unpack v)
asInt v = (readMaybe :: String -> Maybe Int) (Data.Text.unpack v)
getPoints :: Cursor -> Either SomeException [Point]
getPoints c =