1package semver
2
3import "errors"
4
5var errNone = errors.New("The 'None' constraint admits no versions.")
6
7// Any is a constraint that is satisfied by any valid semantic version.
8type any struct{}
9
10// Any creates a constraint that will match any version.
11func Any() Constraint {
12	return any{}
13}
14
15func (any) String() string {
16	return "*"
17}
18
19func (any) ImpliedCaretString() string {
20	return "*"
21}
22
23// Matches checks that a version satisfies the constraint. As all versions
24// satisfy Any, this always returns nil.
25func (any) Matches(v Version) error {
26	return nil
27}
28
29// Intersect computes the intersection between two constraints.
30//
31// As Any is the set of all possible versions, any intersection with that
32// infinite set will necessarily be the entirety of the second set. Thus, this
33// simply returns the passed constraint.
34func (any) Intersect(c Constraint) Constraint {
35	return c
36}
37
38// MatchesAny indicates whether there exists any version that can satisfy both
39// this constraint, and the passed constraint. As all versions
40// satisfy Any, this is always true - unless none is passed.
41func (any) MatchesAny(c Constraint) bool {
42	if _, ok := c.(none); ok {
43		return false
44	}
45	return true
46}
47
48func (any) Union(c Constraint) Constraint {
49	return Any()
50}
51
52func (any) _private() {}
53
54// None is an unsatisfiable constraint - it represents the empty set.
55type none struct{}
56
57// None creates a constraint that matches no versions (the empty set).
58func None() Constraint {
59	return none{}
60}
61
62func (none) String() string {
63	return ""
64}
65
66func (none) ImpliedCaretString() string {
67	return ""
68}
69
70// Matches checks that a version satisfies the constraint. As no version can
71// satisfy None, this always fails (returns an error).
72func (none) Matches(v Version) error {
73	return errNone
74}
75
76// Intersect computes the intersection between two constraints.
77//
78// None is the empty set of versions, and any intersection with the empty set is
79// necessarily the empty set. Thus, this always returns None.
80func (none) Intersect(Constraint) Constraint {
81	return None()
82}
83
84func (none) Union(c Constraint) Constraint {
85	return c
86}
87
88// MatchesAny indicates whether there exists any version that can satisfy the
89// constraint. As no versions satisfy None, this is always false.
90func (none) MatchesAny(c Constraint) bool {
91	return false
92}
93
94func (none) _private() {}
95
96// IsNone indicates if a constraint will match no versions - that is, the
97// constraint represents the empty set.
98func IsNone(c Constraint) bool {
99	_, ok := c.(none)
100	return ok
101}
102
103// IsAny indicates if a constraint will match any and all versions.
104func IsAny(c Constraint) bool {
105	_, ok := c.(any)
106	return ok
107}
108