aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/archive/document.lux
blob: 3e2e866632bc165212be50da60df7dad118a324b (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 (#+ :sharing)
     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 (:sharing [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))))
  )