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

#include <bpkg/satisfaction>

#include <bpkg/package-odb>
#include <bpkg/diagnostics>

using namespace std;
using namespace butl;

namespace bpkg
{
  bool
  satisfies (const version& v, const dependency_constraint& c)
  {
    using op = comparison;

    // Note that the constraint's version is always rhs (libfoo >= 1.2.3).
    //
    switch (c.operation)
    {
    case op::eq: return v == c.version;
    case op::lt: return v <  c.version;
    case op::gt: return v >  c.version;
    case op::le: return v <= c.version;
    case op::ge: return v >= c.version;
    }

    assert (false);
    return false;
  }

  bool
  satisfies (const dependency_constraint& l, const dependency_constraint& r)
  {
    using op = comparison;

    op lo (l.operation);
    op ro (r.operation);

    const version& lv (l.version);
    const version& rv (r.version);

    switch (lo)
    {
    case op::eq: // ==
      {
        return ro == op::eq && lv == rv;
      }
    case op::lt: // <
      {
        switch (ro)
        {
        case op::eq: return rv <  lv;
        case op::lt: return rv <= lv;
        case op::le: return rv <  lv;
        case op::gt:
        case op::ge: return false;
        }
      }
    case op::le: // <=
      {
        switch (ro)
        {
        case op::eq: return rv <= lv;
        case op::lt: return rv <  lv;
        case op::le: return rv <= lv;
        case op::gt:
        case op::ge: return false;
        }
      }
    case op::gt: // >
      {
        switch (ro)
        {
        case op::eq: return lv >  rv;
        case op::lt:
        case op::le: return false;
        case op::gt: return lv >= rv;
        case op::ge: return lv >  rv;
        }
      }
    case op::ge: // >=
      {
        switch (ro)
        {
        case op::eq: return lv >= rv;
        case op::lt:
        case op::le: return false;
        case op::gt: return lv >  rv;
        case op::ge: return lv >= rv;
        }
      }
    }

    assert (false);
    return false;
  }
}