aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/name.lux
blob: 901de118e6799f50e5c23415c6019de75c1c740c (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
(.module:
  [library
   [lux #*
    [abstract
     [equivalence (#+ Equivalence)]
     [hash (#+ Hash)]
     [order (#+ Order)]
     [codec (#+ Codec)]]
    [data
     ["." text ("#\." equivalence monoid)]
     ["." product]]]])

... (type: Name
...   [Text Text])

(template [<name> <doc>]
  [(def: .public (<name> name)
     {#.doc (doc <doc>)}
     (-> Name Text)
     (let [[module short] name]
       <name>))]

  [module "The module part of a name."]
  [short "The short part of a name."]
  )

(def: .public hash
  (Hash Name)
  (product.hash text.hash text.hash))

(def: .public equivalence
  (Equivalence Name)
  (\ ..hash &equivalence))

(implementation: .public order
  (Order Name)
  
  (def: &equivalence ..equivalence)
  (def: (< [moduleP shortP] [moduleS shortS])
    (if (text\= moduleP moduleS)
      (\ text.order < shortP shortS)
      (\ text.order < moduleP moduleS))))

(def: separator
  ".")

(implementation: .public codec
  (Codec Text Name)
  
  (def: (encode [module short])
    (case module
      "" short
      _ ($_ text\compose module ..separator short)))
  
  (def: (decode input)
    (case (text.split_all_with ..separator input)
      (^ (list short))
      (#.Right ["" short])

      (^ (list module short))
      (#.Right [module short])

      _
      (#.Left (text\compose "Invalid format for Name: " input)))))