3
3
open System.Text .RegularExpressions
4
4
5
5
[<Struct>]
6
- type MattrSection < 'p > =
6
+ type MattrSection < 'TData > =
7
7
{ key: string
8
- data: 'p
8
+ data: 'TData
9
9
content: string }
10
10
11
- [<Struct>]
12
- type Mattr < 'p > =
13
- { content: string
14
- sections: MattrSection < 'p > array }
11
+ type IMattrParser < 'TData > =
12
+ abstract member Parse: MattrSection < string > * MattrSection < 'TData > array -> MattrSection < 'TData >
15
13
16
- type Parser < 'p > = MattrSection< string> -> MattrSection< 'p> array -> MattrSection< 'p>
14
+ type DefaultMattrParser () =
15
+ interface IMattrParser< string> with
16
+ member _.Parse ( section : MattrSection < string >, _ : MattrSection < string > array ) : MattrSection < string > = section
17
17
18
18
[<Struct>]
19
- type MattrOption1 = { section_delimiter: string }
19
+ type MattrOption < 'TData > =
20
+ { section_delimiter: string
21
+ parser: IMattrParser < 'TData > }
22
+
23
+ member this.SetDelimiter ( delimiter : string ) =
24
+ { parser = this.parser
25
+ section_ delimiter = delimiter }
26
+
27
+ member this.SetParser ( parser : IMattrParser < 'TNewData >) =
28
+ { section_ delimiter = this.section_ delimiter
29
+ parser = parser }
30
+
31
+ static member Default =
32
+ { MattrOption.section_ delimiter = " ---"
33
+ MattrOption.parser = DefaultMattrParser() }
20
34
21
- [<Struct>]
22
- type MattrOption2 < 'p > = { parse: Parser < 'p > }
23
35
24
36
[<Struct>]
25
- type MattrOption < 'p > =
26
- { section_delimiter: string
27
- parse: Parser < 'p > }
37
+ type Mattr < 'TData > =
38
+ { content: string
39
+ sections: MattrSection < 'TData > array }
40
+
41
+ type Parser < 'TData > = MattrSection< string> -> MattrSection< 'TData> array -> MattrSection< 'TData>
28
42
29
43
module Mattrial =
30
- let defaultOption < 'p > ( parser : Parser < 'p >) : MattrOption < 'p > =
44
+ let defaultOption () =
31
45
{ MattrOption.section_ delimiter = " ---"
32
- MattrOption.parse = parser }
46
+ MattrOption.parser = DefaultMattrParser () }
33
47
34
48
let createSection () : MattrSection < string > =
35
49
{ MattrSection.key = " "
36
50
MattrSection.data = " "
37
51
MattrSection.content = " " }
38
52
39
- let toObject < 'p > ( input : string ) : Mattr < 'p > =
53
+ let toObject < 'TData > ( input : string ) : Mattr < 'TData > =
40
54
{ Mattr.content = input
41
55
Mattr.sections = [||] }
42
56
@@ -56,20 +70,20 @@ module Mattrial =
56
70
else
57
71
true
58
72
59
- let appendData < 'p > ( data : 'p ) ( section : MattrSection < string >) : MattrSection < 'p > =
60
- let parsed : MattrSection < 'p > =
73
+ let appendData < 'TData > ( data : 'TData ) ( section : MattrSection < string >) : MattrSection < 'TData > =
74
+ let parsed : MattrSection < 'TData > =
61
75
{ MattrSection.key = section.key
62
76
MattrSection.content = section.content
63
77
MattrSection.data = data }
64
78
65
79
parsed
66
80
67
- let parse < 'p > ( entity : Mattr < 'p >) ( opts : MattrOption < 'p >) =
81
+ let parse < 'TData > ( entity : Mattr < 'TData >) ( opts : MattrOption < 'TData >) =
68
82
let delim : string = opts.section_ delimiter
69
83
let lines : string array = Regex.Split( entity.content, @" \r?\n" )
70
84
71
- let mutable _entity : Mattr < 'p > = entity
72
- let mutable sections : MattrSection < 'p > array option = None
85
+ let mutable _entity : Mattr < 'TData > = entity
86
+ let mutable sections : MattrSection < 'TData > array option = None
73
87
let mutable content : string array = [||]
74
88
let mutable section : MattrSection < string > = createSection ()
75
89
let mutable stack : string array = [||]
@@ -88,8 +102,8 @@ module Mattrial =
88
102
89
103
sections <-
90
104
match sections with
91
- | Some( sections: MattrSection< 'p > array) ->
92
- Some( Array.append sections [| ( opts.parse section sections) |])
105
+ | Some( sections: MattrSection< 'TData > array) ->
106
+ Some( Array.append sections [| ( opts.parser.Parse ( section, sections) ) |])
93
107
| None -> None
94
108
95
109
section <- createSection ()
@@ -132,56 +146,32 @@ module Mattrial =
132
146
closeSection ( content |> String.concat " \n " )
133
147
134
148
match sections with
135
- | Some( sections: MattrSection< 'p > array) -> { _ entity with sections = sections }
149
+ | Some( sections: MattrSection< 'TData > array) -> { _ entity with sections = sections }
136
150
| None -> _ entity
137
151
138
152
[<Struct>]
139
153
type NewMattr =
140
154
static member sections ( input : string ) : Mattr < string > =
141
155
let file : Mattr < string > = Mattrial.toObject input
142
- let option : MattrOption < string > = Mattrial.defaultOption Mattrial.identity
156
+ let option : MattrOption < string > = Mattrial.defaultOption ()
143
157
Mattrial.parse file option
144
158
145
- static member sections < 'p >( input : string , parser : Parser < 'p >) : Mattr < 'p > =
146
- let file : Mattr < 'p > = Mattrial.toObject input
147
- let option : MattrOption < 'p > = Mattrial.defaultOption parser
159
+ static member sections < 'TData >( input : string , parser : IMattrParser < 'TData >) : Mattr < 'TData > =
160
+ let file : Mattr < 'TData > = Mattrial.toObject input
161
+ let option : MattrOption < 'TData > = ( Mattrial.defaultOption ()) .SetParser parser
148
162
Mattrial.parse file option
149
163
150
- static member sections ( input : string , option : MattrOption1 ) : Mattr < string > =
151
- let file : Mattr < string > = Mattrial.toObject input
152
-
153
- let option : MattrOption < string > =
154
- { MattrOption.section_ delimiter = option.section_ delimiter
155
- MattrOption.parse = Mattrial.identity }
156
-
157
- Mattrial.parse file option
158
-
159
- static member sections < 'p >( input : string , option : MattrOption2 < 'p >) : Mattr < 'p > =
160
- let file : Mattr < 'p > = Mattrial.toObject input
161
- let option : MattrOption < 'p > = Mattrial.defaultOption option.parse
162
- Mattrial.parse file option
163
-
164
- static member sections < 'p >( input : string , option : MattrOption < 'p >) : Mattr < 'p > =
165
- let file : Mattr < 'p > = Mattrial.toObject input
164
+ static member sections < 'TData >( input : string , option : MattrOption < 'TData >) : Mattr < 'TData > =
165
+ let file : Mattr < 'TData > = Mattrial.toObject input
166
166
Mattrial.parse file option
167
167
168
168
static member sections ( entity : Mattr < string >) : Mattr < string > =
169
- let option : MattrOption < string > = Mattrial.defaultOption Mattrial.identity
169
+ let option : MattrOption < string > = Mattrial.defaultOption ()
170
170
Mattrial.parse entity option
171
171
172
- static member sections < 'p >( entity : Mattr < 'p >, parser : Parser < 'p >) : Mattr < 'p > =
173
- let option : MattrOption < 'p > = Mattrial.defaultOption parser
172
+ static member sections < 'TData >( entity : Mattr < 'TData >, parser : IMattrParser < 'TData >) : Mattr < 'TData > =
173
+ let option : MattrOption < 'TData > = ( Mattrial.defaultOption ()) .SetParser parser
174
174
Mattrial.parse entity option
175
175
176
- static member sections ( entity : Mattr < string >, option : MattrOption1 ) : Mattr < string > =
177
- let option : MattrOption < string > =
178
- { MattrOption.section_ delimiter = option.section_ delimiter
179
- MattrOption.parse = Mattrial.identity }
180
-
176
+ static member sections < 'TData >( entity : Mattr < 'TData >, option : MattrOption < 'TData >) : Mattr < 'TData > =
181
177
Mattrial.parse entity option
182
-
183
- static member sections < 'p >( entity : Mattr < 'p >, option : MattrOption2 < 'p >) : Mattr < 'p > =
184
- let option : MattrOption < 'p > = Mattrial.defaultOption option.parse
185
- Mattrial.parse entity option
186
-
187
- static member sections < 'p >( entity : Mattr < 'p >, option : MattrOption < 'p >) : Mattr < 'p > = Mattrial.parse entity option
0 commit comments