aboutsummaryrefslogtreecommitdiff
path: root/bpkg/manifest.cxx
blob: 85e42966f027ad63ff6c1016c2bc45ff74e97769 (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
// file      : bpkg/manifest.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/manifest>

#include <utility> // move()

#include <bpkg/manifest-parser>
#include <bpkg/manifest-serializer>

using namespace std;

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

  // package_manifest
  //
  package_manifest::
  package_manifest (parser& p)
      : package_manifest (p, p.next ()) // Delegate
  {
    // 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 package manifest expected");
  }

  package_manifest::
  package_manifest (parser& p, const name_value& s)
  {
    // Make sure this is the start and we support the version.
    //
    if (!s.name.empty ())
      throw parsing (p.name (), s.name_line, s.name_column,
                     "start of package manifest expected");

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

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

      if (n == "name")
        name = move (v);
      // ...
      else
        throw parsing (p.name (), nv.value_line, nv.value_column,
                       "unknown name '" + n + "' in package manifest");
    }

    // Verify all non-optional values were specified.
    //
  }

  void package_manifest::
  serialize (serializer& s) const
  {
    s.next ("", "1"); // Start of manifest.
    s.next ("name", name);
    // ...
    s.next ("", ""); // End of manifest.
  }

  // repository_manifest
  //
  repository_manifest::
  repository_manifest (parser& p)
      : repository_manifest (p, p.next ()) // Delegate
  {
    // 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 repository manifest expected");
  }

  repository_manifest::
  repository_manifest (parser& p, const name_value& s)
  {
    // Make sure this is the start and we support the version.
    //
    if (!s.name.empty ())
      throw parsing (p.name (), s.name_line, s.name_column,
                     "start of repository manifest expected");

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

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

      if (n == "location")
        location = move (v);
      else
        throw parsing (p.name (), nv.value_line, nv.value_column,
                       "unknown name '" + n + "' in repository manifest");
    }

    // Verify all non-optional values were specified.
    //
    // - location can be omitted
  }

  void repository_manifest::
  serialize (serializer& s) const
  {
    s.next ("", "1"); // Start of manifest.

    if (!location.empty ())
      s.next ("location", location);

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

  // manifests
  //
  manifests::
  manifests (parser& p)
  {
    bool rep (true); // Parsing repository list.

    for (name_value nv (p.next ()); !nv.empty (); nv = p.next ())
    {
      if (rep)
      {
        repositories.push_back (repository_manifest (p, nv));

        // Manifest for local repository signals the end of the
        // repository list.
        //
        if (repositories.back ().location.empty ())
          rep = false;
      }
      else
        packages.push_back (package_manifest (p, nv));
    }
  }

  void manifests::
  serialize (serializer& s) const
  {
    if (repositories.empty () || !repositories.back ().location.empty ())
      throw serialization (s.name (), "local repository manifest expected");

    for (const repository_manifest& r: repositories)
      r.serialize (s);

    for (const package_manifest& p: packages)
      p.serialize (s);

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