Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Oct 2, 2023
1 parent 1d0accc commit c88073d
Showing 1 changed file with 81 additions and 9 deletions.
90 changes: 81 additions & 9 deletions src/FSharpPlus/Control/Converter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,65 @@ type TryParse =
static member TryParse (_: string , _: TryParse) = fun x -> Some x : option<string>
static member TryParse (_: StringBuilder , _: TryParse) = fun x -> Some (new StringBuilder (x: string)) : option<StringBuilder>
#if !FABLE_COMPILER
static member TryParse (_: DateTime , _: TryParse) = fun (x:string) -> DateTime.TryParseExact (x, [|"yyyy-MM-ddTHH:mm:ss.fffZ"; "yyyy-MM-ddTHH:mm:ssZ"|], null, DateTimeStyles.RoundtripKind) |> tupleToOption : option<DateTime>
static member TryParse (_: DateTimeOffset, _: TryParse) = fun (x:string) -> DateTimeOffset.TryParseExact (x, [|"yyyy-MM-ddTHH:mm:ss.fffK"; "yyyy-MM-ddTHH:mm:ssK"|], null, DateTimeStyles.RoundtripKind) |> tupleToOption : option<DateTimeOffset>

static member TryParse (_: DateTime , _: TryParse) = fun (x:string) ->
match DateTime.TryParseExact (x, [|"yyyy-MM-ddTHH:mm:ss.fffZ"; "yyyy-MM-ddTHH:mm:ssZ"|], null, DateTimeStyles.RoundtripKind) with
| true, x -> Some x
| _ ->
match DateTime.TryParse (x, CultureInfo.InvariantCulture, DateTimeStyles.None) with
| true, x -> Some x
| _ -> None

static member TryParse (_: DateTimeOffset, _: TryParse) = fun (x:string) ->
match DateTimeOffset.TryParseExact (x, [|"yyyy-MM-ddTHH:mm:ss.fffK"; "yyyy-MM-ddTHH:mm:ssK"|], null, DateTimeStyles.AssumeUniversal) with
| true, x -> Some x
| _ ->
match DateTimeOffset.TryParseExact (x, [|"yyyy-MM-ddTHH:mm:ss.fffK"; "yyyy-MM-ddTHH:mm:ssK"|], null, DateTimeStyles.RoundtripKind) with
| true, x -> Some x
| _ ->
match DateTimeOffset.TryParse (x, CultureInfo.InvariantCulture, DateTimeStyles.None) with
| true, x -> Some x
| _ -> None
#endif

static member inline Invoke (value: string) =
let inline call_2 (a: ^a, b: ^b) = ((^a or ^b) : (static member TryParse : _*_ -> _) b, a)
let inline call (a: 'a) = fun (x: 'x) -> call_2 (a, Unchecked.defaultof<'r>) x : 'r option
call Unchecked.defaultof<TryParse> value

type TryParse with
static member inline TryParse (_: 'R, _: Default2) = fun x ->
/// The F# signature
static member inline InvokeOnInstance (value: string) = (^R: (static member TryParse : string -> 'R option) value)

/// The .Net signature
static member inline InvokeOnConvention (value: string) =
let mutable r = Unchecked.defaultof< ^R>
if (^R: (static member TryParse : _ * _ -> _) (x, &r)) then Some r else None
if (^R: (static member TryParse : _ * _ -> _) (value, &r)) then Some r else None

#if NET7_0
/// IParsable<'T>
static member InvokeOnInterface<'T when 'T :> IParsable<'T>> (value: string) =
let mutable r = Unchecked.defaultof<'T>
if ('T.TryParse(value, CultureInfo.InvariantCulture, &r)) then Some r else None
#endif

static member inline TryParse (_: ^t when ^t: null and ^t: struct, _: Default1) = id
static member inline TryParse (_: 'R, _: Default1) = fun x -> (^R: (static member TryParse : string -> 'R option) x)

type Parse =
inherit Default1
static member inline Parse (_: ^R , _: Default1) = fun (x:string) -> (^R: (static member Parse : _ -> ^R) x)
static member inline Parse (_: ^R , _: Parse ) = fun (x:string) -> (^R: (static member Parse : _ * _ -> ^R) (x, CultureInfo.InvariantCulture))

static member inline Parse (_: 'T when 'T : enum<_>, _: Parse ) = fun (x:string) ->
(match Enum.TryParse (x) with
| (true, v) -> v
| _ -> invalidArg "value" ("Requested value '" + x + "' was not found.")
) : 'enum

static member Parse (_: DateTime , _: Parse) = fun (x:string) ->
try DateTime.ParseExact (x, [|"yyyy-MM-ddTHH:mm:ss.fffZ"; "yyyy-MM-ddTHH:mm:ssZ"|], null, DateTimeStyles.RoundtripKind)
with _ -> DateTime.Parse (x, CultureInfo.InvariantCulture)

static member Parse (_: DateTimeOffset, _: Parse) = fun (x:string) ->
try DateTimeOffset.ParseExact (x, [|"yyyy-MM-ddTHH:mm:ss.fffK"; "yyyy-MM-ddTHH:mm:ssK"|], null, DateTimeStyles.AssumeUniversal)
with _ -> DateTimeOffset.Parse (x, CultureInfo.InvariantCulture)

static member Parse (_: bool , _: Parse) = fun (x:string) -> Boolean.Parse (x)

Expand All @@ -151,4 +182,45 @@ type Parse =
let inline call (a: 'a) = fun (x: 'x) -> call_2 (a, Unchecked.defaultof<'r>) x : 'r
call Unchecked.defaultof<Parse> value

static member inline InvokeOnInstance (value: string) = (^R: (static member Parse : _ -> ^R) value)


type Parse with

static member inline Parse (_: ^R , _: Default4) = fun (value: string) ->
match TryParse.InvokeOnConvention value with
| Some x -> x : ^R
| None -> invalidArg "value" ("Error parsing value '" + value + "'.")

static member inline Parse (_: ^R , _: Default3) = fun (value: string) ->
match TryParse.InvokeOnInstance value with
| Some x -> x : ^R
| None -> invalidArg "value" ("Error parsing value '" + value + "'.")

static member inline Parse (_: ^R , _: Default2) : string -> ^R = Parse.InvokeOnInstance

#if NET7_0
static member Parse<'T when 'T :> IParsable<'T>> (_: 'T, _: Default1) = fun (x: string) -> 'T.Parse (x, CultureInfo.InvariantCulture)
static member inline Parse (_: ^t when ^t: null and ^t: struct, _: Default1) = id
#else
static member inline Parse (_: ^t when ^t: null and ^t: struct, _: Default2) = id
#endif

type TryParse with

static member inline TryParse (_: 'R, _: Default4) : string -> 'R option = fun (value: string) ->
try Parse.InvokeOnInstance value |> Some
with _ -> None // todo, maybe match on invalidArg only

static member inline TryParse (_: 'R, _: Default3) : string -> 'R option = TryParse.InvokeOnConvention

static member inline TryParse (_: 'R, _: Default2) : string -> 'R option = TryParse.InvokeOnInstance

#if NET7_0
static member inline TryParse (_: 'R, _: Default1) : string -> 'R option = TryParse.InvokeOnInterface
static member inline TryParse (_: ^t when ^t: null and ^t: struct, _: Default1) = id
#else
static member inline TryParse (_: ^t when ^t: null and ^t: struct, _: Default2) = id
#endif

#endif

0 comments on commit c88073d

Please sign in to comment.