aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/archive/document.lux
blob: 6e56f7f8b204f8b319a9cc710aba4c4db16a3a83 (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
(.module:
  [library
   [lux {"-" [Module]}
    [abstract
     [monad {"+" [do]}]]
    [control
     ["[0]" try {"+" [Try]}]
     ["[0]" exception {"+" [exception:]}]
     ["<>" parser
      [binary {"+" [Parser]}]]]
    [data
     [collection
      ["[0]" dictionary {"+" [Dictionary]}]]
     [format
      ["[0]" binary {"+" [Writer]}]]]
    [type {"+" [:sharing]}
     abstract]]]
  [//
   ["[0]" signature {"+" [Signature]}]
   ["[0]" key {"+" [Key]}]
   [descriptor {"+" [Module]}]])

(exception: .public (invalid_signature {expected Signature} {actual Signature})
  (exception.report
   ["Expected" (signature.description expected)]
   ["Actual" (signature.description actual)]))

(abstract: .public (Document d)
  {}
  
  (Record
   [#signature Signature
    #content d])

  (def: .public (read key document)
    (All (_ d) (-> (Key d) (Document Any) (Try d)))
    (let [[document//signature document//content] (:representation document)]
      (if (\ signature.equivalence =
             (key.signature key)
             document//signature)
        (#try.Success (:sharing [e]
                                (Key e)
                                key
                                
                                e
                                (:expected document//content)))
        (exception.except ..invalid_signature [(key.signature key)
                                               document//signature]))))

  (def: .public (write key content)
    (All (_ d) (-> (Key d) d (Document d)))
    (:abstraction [#signature (key.signature key)
                   #content content]))

  (def: .public (check key document)
    (All (_ d) (-> (Key d) (Document Any) (Try (Document d))))
    (do try.monad
      [_ (..read key document)]
      (in (:expected document))))

  (def: .public signature
    (-> (Document Any) Signature)
    (|>> :representation (value@ #signature)))

  (def: .public (writer content)
    (All (_ d) (-> (Writer d) (Writer (Document d))))
    (let [writer (binary.and signature.writer
                             content)]
      (|>> :representation writer)))

  (def: .public parser
    (All (_ d) (-> (Parser d) (Parser (Document d))))
    (|>> (<>.and signature.parser)
         (\ <>.monad each (|>> :abstraction))))
  )