aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/format/lux/data/text.lux
blob: 43117912ecf87aed1cc689781092a9842ffea492 (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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(.require
 [library
  [lux (.except list nat int rev symbol type)
   [abstract
    [monad (.only do)]
    [functor
     ["[0]" contravariant]]]
   [control
    ["<>" parser]]
   [data
    ["[0]" bit]
    ["[0]" text]
    [format
     ["[0]" xml]
     ["[0]" json]]
    [collection
     ["[0]" list (.use "[1]#[0]" monad)]]]
   [math
    [number
     ["[0]" nat]
     ["[0]" int]
     ["[0]" rev]
     ["[0]" frac]
     ["[0]" ratio]]
    [arithmetic
     ["[0]" modular]]]
   [meta
    ["[0]" location]
    ["[0]" symbol]
    ["[0]" type]
    ["[0]" code (.only)
     ["<[1]>" \\parser (.only Parser)]]
    [macro
     [syntax (.only syntax)]
     ["[0]" template]]]
   [world
    ["[0]" time (.only)
     ["[0]" instant]
     ["[0]" duration]
     ["[0]" date]
     ["[0]" day]
     ["[0]" month]]]]])

(.type .public (Format of)
  (-> of
      Text))

(def .public functor
  (contravariant.Functor Format)
  (implementation
   (def (each f fb)
     (|>> f fb))))

(def .public format
  (syntax (_ [fragments (<>.many <code>.any)])
    (in (.list (` (.text_composite# (,* fragments)))))))

(with_template [<name> <type> <formatter>]
  [(def .public <name>
     (Format <type>)
     <formatter>)]

  [bit      Bit               (of bit.codec encoded)]
  [nat      Nat               (of nat.decimal encoded)]
  [int      Int               (of int.decimal encoded)]
  [rev      Rev               (of rev.decimal encoded)]
  [frac     Frac              (of frac.decimal encoded)]
  [text     Text              text.format]
  
  [ratio    ratio.Ratio       (of ratio.codec encoded)]
  [symbol   Symbol            (of symbol.codec encoded)]
  [location Location          location.format]
  [code     Code              code.format]
  [type     Type              type.format]
  
  [instant  instant.Instant   (of instant.codec encoded)]
  [duration duration.Duration (of duration.codec encoded)]
  [date     date.Date         (of date.codec encoded)]
  [time     time.Time         (of time.codec encoded)]
  [day      day.Day           (of day.codec encoded)]
  [month    month.Month       (of month.codec encoded)]
  
  [xml      xml.XML           (of xml.codec encoded)]
  [json     json.JSON         (of json.codec encoded)]
  )

(with_template [<type> <format>,<codec>]
  [(`` (with_template [<format> <codec>]
         [(def .public <format>
            (Format <type>)
            (of <codec> encoded))]

         (,, (template.spliced <format>,<codec>))))]

  [Nat
   [[nat_2 nat.binary]
    [nat_8 nat.octal]
    [nat_10 nat.decimal]
    [nat_16 nat.hex]]]
  [Int
   [[int_2 int.binary]
    [int_8 int.octal]
    [int_10 int.decimal]
    [int_16 int.hex]]]
  [Rev
   [[rev_2 rev.binary]
    [rev_8 rev.octal]
    [rev_10 rev.decimal]
    [rev_16 rev.hex]]]
  [Frac
   [[frac_2 frac.binary]
    [frac_8 frac.octal]
    [frac_10 frac.decimal]
    [frac_16 frac.hex]]]
  )

(def .public (mod modular)
  (All (_ %)
    (Format (modular.Mod %)))
  (let [codec (modular.codec (modular.modulus modular))]
    (of codec encoded modular)))

(def .public (list formatter)
  (All (_ of)
    (-> (Format of)
        (Format (List of))))
  (|>> (list#each (|>> formatter (format " ")))
       text.together
       (text.enclosed ["(list" ")"])))

(def .public (maybe format)
  (All (_ of)
    (-> (Format of)
        (Format (Maybe of))))
  (function (_ value)
    (when value
      {.#None}
      "{.#None}"

      {.#Some value}
      (..format "{.#Some " (format value) "}"))))