aboutsummaryrefslogtreecommitdiff
path: root/libbrep/review-manifest.cxx
blob: 3592e699149669cfc825466319972553d8509634 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// file      : libbrep/review-manifest.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbrep/review-manifest.hxx>

#include <libbutl/manifest-parser.hxx>
#include <libbutl/manifest-serializer.hxx>

using namespace std;
using namespace butl;

namespace brep
{
  using parser = manifest_parser;
  using parsing = manifest_parsing;
  using serializer = manifest_serializer;
  using serialization = manifest_serialization;
  using name_value = manifest_name_value;

  // review_result
  //
  string
  to_string (review_result r)
  {
    switch (r)
    {
    case review_result::pass:      return "pass";
    case review_result::fail:      return "fail";
    case review_result::unchanged: return "unchanged";
    }

    assert (false);
    return string ();
  }

  review_result
  to_review_result (const string& r)
  {
         if (r == "pass")      return review_result::pass;
    else if (r == "fail")      return review_result::fail;
    else if (r == "unchanged") return review_result::unchanged;
    else throw invalid_argument ("invalid review result '" + r + '\'');
  }

  // review_manifest
  //
  review_manifest::
  review_manifest (parser& p, bool iu)
      : review_manifest (p, p.next (), iu)
  {
    // Make sure this is the end.
    //
    name_value nv (p.next ());
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single review manifest expected");
  }

  review_manifest::
  review_manifest (parser& p, name_value nv, bool iu)
  {
    auto bad_name ([&p, &nv](const string& d) {
        throw parsing (p.name (), nv.name_line, nv.name_column, d);});

    auto bad_value ([&p, &nv](const string& d) {
        throw parsing (p.name (), nv.value_line, nv.value_column, d);});

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "start of review manifest expected");

    if (nv.value != "1")
      throw parsing (p.name (), nv.value_line, nv.value_column,
                     "unsupported format version");

    bool need_base (false);
    bool need_details (false);

    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "reviewed-by")
      {
        if (!reviewed_by.empty ())
          bad_name ("reviewer redefinition");

        if (v.empty ())
          bad_value ("empty reviewer");

        reviewed_by = move (v);
      }
      else if (n.size () > 7 && n.compare (0, 7, "result-") == 0)
      {
        string name (n, 7, n.size () - 7);

        if (find_if (results.begin (), results.end (),
                     [&name] (const review_aspect& r)
                     {
                       return name == r.name;
                     }) != results.end ())
          bad_name (name + " review result redefinition");

        try
        {
          review_result r (to_review_result (v));

          if (r == review_result::fail)
            need_details = true;

          if (r == review_result::unchanged)
            need_base = true;

          results.push_back (review_aspect {move (name), r});
        }
        catch (const invalid_argument& e)
        {
          bad_value (e.what ());
        }
      }
      else if (n == "base-version")
      {
        if (base_version)
          bad_name ("base version redefinition");

        try
        {
          base_version = bpkg::version (v);
        }
        catch (const invalid_argument& e)
        {
          bad_value (e.what ());
        }
      }
      else if (n == "details-url")
      {
        if (details_url)
          bad_name ("details url redefinition");

        try
        {
          details_url = url (v);
        }
        catch (const invalid_argument& e)
        {
          bad_value (e.what ());
        }
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in review manifest");
    }

    // Verify all non-optional values were specified.
    //
    if (reviewed_by.empty ())
      bad_value ("no reviewer specified");

    if (results.empty ())
      bad_value ("no result specified");

    if (!base_version && need_base)
      bad_value ("no base version specified");

    if (!details_url && need_details)
      bad_value ("no details url specified");
  }

  void review_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //
    s.next ("", "1"); // Start of manifest.

    auto bad_value ([&s](const string& d) {
        throw serialization (s.name (), d);});

    if (reviewed_by.empty ())
      bad_value ("empty reviewer");

    s.next ("reviewed-by", reviewed_by);

    for (const review_aspect& r: results)
      s.next ("result-" + r.name, to_string (r.result));

    if (base_version)
      s.next ("base-version", base_version->string ());

    if (details_url)
      s.next ("details-url", details_url->string ());

    s.next ("", ""); // End of manifest.
  }

  // review_manifests
  //
  review_manifests::
  review_manifests (parser& p, bool iu)
  {
    // Parse review manifests.
    //
    for (name_value nv (p.next ()); !nv.empty (); nv = p.next ())
      emplace_back (p, move (nv), iu);
  }

  void review_manifests::
  serialize (serializer& s) const
  {
    // Serialize review manifests.
    //
    for (const review_manifest& m: *this)
      m.serialize (s);

    s.next ("", ""); // End of stream.
  }
}