Files
puregym-unofficial-dotnet/PureGym/GymSelector.fs
Smaug123 42eb1f7726
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
Initial commit
2023-10-11 21:16:40 +01:00

49 lines
1.6 KiB
Forth

namespace PureGym
open System.Threading.Tasks
open Fastenshtein
/// Identifies a gym, possibly non-uniquely and possibly ambiguously.
[<RequireQualifiedAccess>]
type GymSelector =
/// The ID of this gym, according to the PureGym internal mapping.
| Id of int
/// The user-specified name of this gym.
| Name of string
/// The home gym of the authenticated user.
| Home
/// Methods for manipulating GymSelector.
[<RequireQualifiedAccess>]
module GymSelector =
/// Get the canonical PureGym ID for this user-specified gym.
let canonicalId (client : IPureGymApi) (gym : GymSelector) : int Task =
match gym with
| GymSelector.Home ->
task {
let! self = client.GetMember ()
return self.HomeGymId
}
| GymSelector.Id i -> Task.FromResult<_> i
| GymSelector.Name name ->
task {
let! allGyms = client.GetGyms ()
if allGyms.IsEmpty then
return failwith "PureGym API returned no gyms!"
else
let distance = Levenshtein (name.ToLowerInvariant ())
let bestDistance, bestGym =
allGyms
|> Seq.map (fun gym -> distance.DistanceFrom (gym.Name.ToLowerInvariant ()), gym)
|> Seq.sortBy fst
|> Seq.head
if bestDistance <> 0 then
System.Console.Error.WriteLine $"Autocorrected gym from %s{name} to %s{bestGym.Name}"
return bestGym.Id
}