aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/format/context.lux
blob: 71548907250885a7d03e2687e1d3cae0aa0ef724 (plain)
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
(.module:
  [lux #*
   [control
    [parser (#+ Parser)]
    ["ex" exception (#+ exception:)]
    [monad (#+ do)]]
   [data
    ["." error (#+ Error)]
    ["." text
     format]
    [collection
     ["." dictionary (#+ Dictionary)]]]])

(exception: #export (unknown-property {property Text})
  (ex.report ["Property" (%t property)]))

(type: #export Context
  (Dictionary Text Text))

(type: #export (Property a)
  (Parser Context a))

(def: #export empty
  Context
  (dictionary.new text.Hash<Text>))

(def: #export (property name)
  (-> Text (Property Text))
  (function (_ context)
    (case (dictionary.get name context)
      (#.Some value)
      (ex.return [context value])
      
      #.None
      (ex.throw unknown-property name))))

(def: #export (run context property)
  (All [a] (-> Context (Property a) (Error a)))
  (case (property context)
    (#error.Success [_ output])
    (#error.Success output)

    (#error.Failure error)
    (#error.Failure error)))