Skip to content

Commit 8529813

Browse files
committed
Change API for MattrOption
1 parent b3f84a8 commit 8529813

File tree

2 files changed

+55
-63
lines changed

2 files changed

+55
-63
lines changed

SectionMattr.Test/Program.fs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ type ParsedData = { title: string }
88
let read (fixtureName: string) : string =
99
File.ReadAllText(Path.Combine("../../..", "fixtures", fixtureName))
1010

11+
type MyJsonParser() =
12+
interface IMattrParser<ParsedData> with
13+
member _.Parse(section: MattrSection<string>, _: MattrSection<ParsedData> array) : MattrSection<ParsedData> =
14+
Mattrial.appendData (JsonSerializer.Deserialize<ParsedData>(section.data)) section
15+
1116
[<Tests>]
1217
let tests =
1318
testList
@@ -97,7 +102,7 @@ let tests =
97102
"~~~\ntitle: bar\n~~~\n\nfoo\n~~~one\ntitle: One\n~~~\nThis is one"
98103

99104
Expect.equal
100-
(NewMattr.sections (input, { MattrOption1.section_delimiter = "~~~" }))
105+
(NewMattr.sections (input, (Mattrial.defaultOption ()).SetDelimiter("~~~")))
101106
{ Mattr.content = ""
102107
Mattr.sections =
103108
[| { MattrSection.key = ""
@@ -113,11 +118,8 @@ let tests =
113118
let input: string =
114119
"---json\n{\"title\": \"bar\"}\n---\n\nfoo\n---json\n{\"title\": \"One\"}\n---\nThis is one"
115120

116-
let parse (section: MattrSection<string>) (_: MattrSection<ParsedData> array) : MattrSection<ParsedData> =
117-
Mattrial.appendData (JsonSerializer.Deserialize<ParsedData>(section.data)) section
118-
119121
Expect.equal
120-
(NewMattr.sections (input, parse))
122+
(NewMattr.sections (input, MyJsonParser()))
121123
{ Mattr.content = ""
122124
Mattr.sections =
123125
[| { MattrSection.key = "json"

SectionMattr/Library.fs

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,54 @@
33
open System.Text.RegularExpressions
44

55
[<Struct>]
6-
type MattrSection<'p> =
6+
type MattrSection<'TData> =
77
{ key: string
8-
data: 'p
8+
data: 'TData
99
content: string }
1010

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>
1513

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
1717

1818
[<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() }
2034

21-
[<Struct>]
22-
type MattrOption2<'p> = { parse: Parser<'p> }
2335

2436
[<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>
2842

2943
module Mattrial =
30-
let defaultOption<'p> (parser: Parser<'p>) : MattrOption<'p> =
44+
let defaultOption () =
3145
{ MattrOption.section_delimiter = "---"
32-
MattrOption.parse = parser }
46+
MattrOption.parser = DefaultMattrParser() }
3347

3448
let createSection () : MattrSection<string> =
3549
{ MattrSection.key = ""
3650
MattrSection.data = ""
3751
MattrSection.content = "" }
3852

39-
let toObject<'p> (input: string) : Mattr<'p> =
53+
let toObject<'TData> (input: string) : Mattr<'TData> =
4054
{ Mattr.content = input
4155
Mattr.sections = [||] }
4256

@@ -56,20 +70,20 @@ module Mattrial =
5670
else
5771
true
5872

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> =
6175
{ MattrSection.key = section.key
6276
MattrSection.content = section.content
6377
MattrSection.data = data }
6478

6579
parsed
6680

67-
let parse<'p> (entity: Mattr<'p>) (opts: MattrOption<'p>) =
81+
let parse<'TData> (entity: Mattr<'TData>) (opts: MattrOption<'TData>) =
6882
let delim: string = opts.section_delimiter
6983
let lines: string array = Regex.Split(entity.content, @"\r?\n")
7084

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
7387
let mutable content: string array = [||]
7488
let mutable section: MattrSection<string> = createSection ()
7589
let mutable stack: string array = [||]
@@ -88,8 +102,8 @@ module Mattrial =
88102

89103
sections <-
90104
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)) |])
93107
| None -> None
94108

95109
section <- createSection ()
@@ -132,56 +146,32 @@ module Mattrial =
132146
closeSection (content |> String.concat "\n")
133147

134148
match sections with
135-
| Some(sections: MattrSection<'p> array) -> { _entity with sections = sections }
149+
| Some(sections: MattrSection<'TData> array) -> { _entity with sections = sections }
136150
| None -> _entity
137151

138152
[<Struct>]
139153
type NewMattr =
140154
static member sections(input: string) : Mattr<string> =
141155
let file: Mattr<string> = Mattrial.toObject input
142-
let option: MattrOption<string> = Mattrial.defaultOption Mattrial.identity
156+
let option: MattrOption<string> = Mattrial.defaultOption ()
143157
Mattrial.parse file option
144158

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
148162
Mattrial.parse file option
149163

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
166166
Mattrial.parse file option
167167

168168
static member sections(entity: Mattr<string>) : Mattr<string> =
169-
let option: MattrOption<string> = Mattrial.defaultOption Mattrial.identity
169+
let option: MattrOption<string> = Mattrial.defaultOption ()
170170
Mattrial.parse entity option
171171

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
174174
Mattrial.parse entity option
175175

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> =
181177
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

Comments
 (0)