-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.fs
116 lines (82 loc) · 3.3 KB
/
Core.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
namespace Flux
open System.Text.RegularExpressions
[<Struct>]
type NonEmptyString = private NonEmptyString of string
type nestring = NonEmptyString
module NonEmptyString =
[<RequireQualifiedAccess>]
type CouldNotCreateNonEmptyString = | StringWasEmpty
exception CouldNotCreateNonEmptyStringException of CouldNotCreateNonEmptyString
let tryCreate value =
if String.length value = 0 then
Ok (NonEmptyString value)
else
Error CouldNotCreateNonEmptyString.StringWasEmpty
let maybeCreate value =
if String.length value > 0 then
Some (NonEmptyString value)
else
None
let create value =
if String.length value > 0 then
NonEmptyString value
else
raise (CouldNotCreateNonEmptyStringException CouldNotCreateNonEmptyString.StringWasEmpty)
let toString (NonEmptyString value) = value
module ActivePattern =
let (|NonEmptyString|) (NonEmptyString value) = value
[<Struct>]
type EmailAddress = private EmailAddress of string
module Email =
[<RequireQualifiedAccess>]
type CouldNotCreateEmailAddress =
| ValueWasEmpty
| InvalidFormat of string
exception CouldNotCreateEmailAddressException of CouldNotCreateEmailAddress
let private regex =
Regex (
@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
RegexOptions.Compiled ||| RegexOptions.IgnoreCase
)
let tryCreate value =
if String.length value = 0 then
Error (CouldNotCreateEmailAddress.ValueWasEmpty)
else
let result = regex.Match value
if result.Success then
Ok (EmailAddress value)
else
Error (CouldNotCreateEmailAddress.InvalidFormat value)
let maybeCreate value =
if String.length value = 0 then
None
else
let result = regex.Match value
if result.Success then Some (EmailAddress value) else None
let create value =
if String.length value = 0 then
raise (CouldNotCreateEmailAddressException CouldNotCreateEmailAddress.ValueWasEmpty)
else
let result = regex.Match value
if result.Success then
EmailAddress value
else
raise (CouldNotCreateEmailAddressException (CouldNotCreateEmailAddress.InvalidFormat value))
let toString (EmailAddress value) = value
module ActivePattern =
let (|EmailAddress|) (EmailAddress value) = value
module Text =
open System.Text
let bytesToStringUTF8 (bytes: byte[]) = Encoding.UTF8.GetString bytes
let stringToBytesUTF8 (str: string) = Encoding.UTF8.GetBytes str
namespace Flux.LanguageUtils.Linq.Expressions
open System.Linq.Expressions
open System
type 'T Pred = Expression<Func<'T, bool>>
type Mapper<'T, 'R> = Expression<Func<'T, 'R>>
type Selector<'T, 'R> = Mapper<'T, 'R>
type Expr private () =
static member Of(e: Expression<Func<'T1, 'TResult>>) = e
static member Of(e: Expression<Func<'T1, 'T2, 'TResult>>) = e
static member Of(e: Expression<Func<'T1, 'T2, 'T3, 'TResult>>) = e
static member Of(e: Expression<Func<'T1, 'T2, 'T3, 'T4, 'TResult>>) = e