aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/meta/symbol.lux
blob: 3e8fbf7681ce5049c05185f19e19741f021b1c2b (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
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]
    [order (.only Order)]
    [codec (.only Codec)]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence monoid)]
    ["[0]" product]]]])

... (type Symbol
...   [Text Text])

(with_template [<name>]
  [(def .public (<name> [module short])
     (-> Symbol
         Text)
     <name>)]

  [module]
  [short]
  )

(def .public hash
  (Hash Symbol)
  (product.hash text.hash text.hash))

(def .public equivalence
  (Equivalence Symbol)
  (of ..hash equivalence))

(def .public order
  (Order Symbol)
  (implementation
   (def equivalence ..equivalence)
   (def (< [moduleP shortP] [moduleS shortS])
     (if (text#= moduleP moduleS)
       (of text.order < shortP shortS)
       (of text.order < moduleP moduleS)))))

(def .public separator
  ".")

(def .public codec
  (Codec Text Symbol)
  (implementation
   (def (encoded [module short])
     (when module
       ""
       short
       
       _
       (all text#composite module ..separator short)))
   
   (def (decoded input)
     (when (text.all_split_by ..separator input)
       (list short)
       {.#Right ["" short]}

       (list module short)
       {.#Right [module short]}

       _
       {.#Left (text#composite "Invalid format for Symbol: " input)}))))

(def .public (relative_codec expected)
  (-> Text
      (Codec Text Symbol))
  (implementation
   (def (encoded [module short])
     (when module
       ""
       short
       
       .prelude
       (all text#composite ..separator short)
       
       _
       (all text#composite
            (if (text#= expected module)
              ..separator
              module)
            ..separator short)))
   
   (def (decoded input)
     (when (text.all_split_by ..separator input)
       (list short)
       {.#Right ["" short]}

       (list "" short)
       {.#Right [.prelude short]}

       (list module short)
       {.#Right [module short]}

       (list "" "" short)
       {.#Right [expected short]}

       _
       {.#Left (text#composite "Invalid format for Symbol: " input)}))))