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

(exception: #export (invalid-signature {expected Signature} {actual Signature})
  (exception.report
   ["Expected" (signature.description expected)]
   ["Actual" (signature.description actual)]))

(abstract: #export (Document d)
  {#signature Signature
   #content d}

  (def: #export (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 (:share [e]
                              (Key e)
                              key
                              
                              e
                              (:assume document//content)))
        (exception.throw ..invalid-signature [(key.signature key)
                                              document//signature]))))

  (def: #export (write key content)
    (All [d] (-> (Key d) d (Document d)))
    (:abstraction {#signature (key.signature key)
                   #content content}))

  (def: #export (check key document)
    (All [d] (-> (Key d) (Document Any) (Try (Document d))))
    (do try.monad
      [_ (..read key document)]
      (wrap (:assume document))))

  (def: #export signature
    (-> (Document Any) Signature)
    (|>> :representation (get@ #signature)))

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

  (def: #export parser
    (All [d] (-> (Parser d) (Parser (Document d))))
    (|>> (<>.and signature.parser)
         (\ <>.monad map (|>> :abstraction))))
  )