summaryrefslogtreecommitdiffhomepage
path: root/gleam/src/ocda_lib.gleam
blob: 6eccaff6415db446eb00639db98c48effc8a0390 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//// ocda-lib, in Gleam: the abstraction as a library, put to the question.
////
//// A library has no binary, so this is the programs over it: the reader,
//// and the two that test its claims.
////
////   ocda-lib dump [--unknown] <file>.ssp|<file>.ocda.db [lang]
////   ocda-lib agree <doc>.ssp <doc>.ocda.db [lang]
////   ocda-lib read  [options] <file> [lang]    (also installed as ocda-read)
////
//// `dump` writes the whole loaded model out canonically, byte for byte as
//// the D library's `ocda-dump` writes it, so that the two implementations
//// can be diffed. `agree` asks the same question the D library's
//// `ocda-agree` asks, within this language: does loading a document's
//// `.ssp` give what loading its `.ocda.db` gives?

import argv
import gleam/int
import gleam/io
import gleam/list
import gleam/string
import ocda/dump
import ocda/load
import ocda/model.{type Document}
import read/main as reader

pub fn main() {
  case argv.load().arguments {
    ["dump", "--unknown", path] -> dump_file(path, "", True)
    ["dump", "--unknown", path, lang] -> dump_file(path, lang, True)
    ["dump", path] -> dump_file(path, "", False)
    ["dump", path, lang] -> dump_file(path, lang, False)
    ["agree", ssp, db] -> agree(ssp, db, "")
    ["agree", ssp, db, lang] -> agree(ssp, db, lang)
    ["read", ..rest] ->
      case reader.run(rest) {
        0 -> Nil
        code -> halt(code)
      }
    _ -> usage()
  }
}

fn usage() -> Nil {
  io.println_error(
    "usage: ocda-lib dump [--unknown] <file>.ssp|<file>.ocda.db [lang]
       ocda-lib agree <doc>.ssp <doc>.ocda.db [lang]
       ocda-lib read [--help] [options] <file> [lang]",
  )
  halt(2)
}

fn dump_file(path: String, lang: String, unknown: Bool) -> Nil {
  case load.load(path, lang, False) {
    Error(message) -> fail(message)
    Ok(doc) ->
      case unknown {
        True -> io.print(dump.unknown(doc))
        False -> io.print(dump.document(doc))
      }
  }
}

/// The library's whole purpose is that a writer cannot tell which
/// serialisation it was handed. That is only true if the two loaders give
/// the same model, so this asks, and says where they part company rather
/// than only that they did.
fn agree(ssp_path: String, db_path: String, lang: String) -> Nil {
  case load.load(ssp_path, "", False), load.load(db_path, lang, False) {
    Error(message), _ -> fail(message)
    _, Error(message) -> fail(message)
    Ok(from_ssp), Ok(from_db) -> {
      let differences = compare(from_ssp, from_db)
      case differences {
        [] -> io.println("agree: the two loaders give the same model")
        _ -> {
          list.each(list.take(differences, 12), io.println)
          case list.length(differences) > 12 {
            True -> io.println("  ... stopping after 12")
            False -> Nil
          }
          io.println(int.to_string(list.length(differences)) <> " difference(s)")
          halt(1)
        }
      }
    }
  }
}

/// Compared through the dump rather than field by field, which is both
/// shorter and stricter: the dump carries every field, so nothing can
/// differ without showing here.
fn compare(from_ssp: Document, from_db: Document) -> List(String) {
  let a = dump.document(from_ssp)
  let b = dump.document(from_db)
  case a == b {
    True -> []
    False -> diff_lines(a, b)
  }
}

fn diff_lines(a: String, b: String) -> List(String) {
  let la = split_lines(a)
  let lb = split_lines(b)
  case list.length(la) == list.length(lb) {
    False -> [
      "  differ: line count",
      "    .ssp:     " <> int.to_string(list.length(la)),
      "    .ocda.db: " <> int.to_string(list.length(lb)),
      ..pairwise(la, lb)
    ]
    True -> pairwise(la, lb)
  }
}

fn pairwise(la: List(String), lb: List(String)) -> List(String) {
  list.zip(la, lb)
  |> list.index_map(fn(pair, i) { #(i, pair.0, pair.1) })
  |> list.filter(fn(t) { t.1 != t.2 })
  |> list.map(fn(t) {
    "  differ: line "
    <> int.to_string(t.0 + 1)
    <> "\n    .ssp:     "
    <> t.1
    <> "\n    .ocda.db: "
    <> t.2
  })
}

fn split_lines(text: String) -> List(String) {
  case text {
    "" -> []
    _ -> string.split(text, on: "\n")
  }
}

fn fail(message: String) -> Nil {
  io.println_error("error: " <> message)
  halt(2)
}

/// Gleam has no standard exit, and the exit status is what a sweep script
/// reads.
@external(erlang, "erlang", "halt")
fn halt(code: Int) -> Nil