aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/tool/compiler/meta/archive/document.lux
blob: 30777c282341722defbf2fbe1c9e8fcf1439e99f (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
(.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: .public (invalid_signature {expected Signature} {actual Signature})
  (exception.report
   ["Expected" (signature.description expected)]
   ["Actual" (signature.description actual)]))

(abstract: .public (Document d)
  {}
  
  {#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 (get@ #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 map (|>> :abstraction))))
  )