File: set_bound.go

package info (click to toggle)
golang-github-apparentlymart-go-versions 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 292 kB
  • sloc: makefile: 2
file content (98 lines) | stat: -rw-r--r-- 1,974 bytes parent folder | download
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
package versions

import (
	"fmt"
)

type setBound struct {
	v  Version
	op setBoundOp
}

func (s setBound) Has(v Version) bool {
	switch s.op {
	case setBoundGT:
		return v.GreaterThan(s.v)
	case setBoundGTE:
		return v.GreaterThan(s.v) || v.Same(s.v)
	case setBoundLT:
		return v.LessThan(s.v)
	case setBoundLTE:
		return v.LessThan(s.v) || v.Same(s.v)
	default:
		// Should never happen because the above is exhaustive
		panic("invalid setBound operator")
	}
}

func (s setBound) AllRequested() Set {
	// Inequalities request nothing.
	return None
}

func (s setBound) GoString() string {
	switch s.op {
	case setBoundGT:
		return fmt.Sprintf("versions.NewerThan(%#v)", s.v)
	case setBoundGTE:
		return fmt.Sprintf("versions.AtLeast(%#v)", s.v)
	case setBoundLT:
		return fmt.Sprintf("versions.OlderThan(%#v)", s.v)
	case setBoundLTE:
		return fmt.Sprintf("versions.AtMost(%#v)", s.v)
	default:
		// Should never happen because the above is exhaustive
		return fmt.Sprintf("versions.Set{versions.setBound{v:%#v,op:%#v}}", s.v, s.op)
	}
}

// NewerThan returns a set containing all versions greater than the given
// version, non-inclusive.
func NewerThan(v Version) Set {
	return Set{
		setI: setBound{
			v:  v,
			op: setBoundGT,
		},
	}
}

// OlderThan returns a set containing all versions lower than the given
// version, non-inclusive.
func OlderThan(v Version) Set {
	return Set{
		setI: setBound{
			v:  v,
			op: setBoundLT,
		},
	}
}

// AtLeast returns a set containing all versions greater than or equal to
// the given version.
func AtLeast(v Version) Set {
	return Set{
		setI: setBound{
			v:  v,
			op: setBoundGTE,
		},
	}
}

// AtMost returns a set containing all versions less than or equal to the given
// version, non-inclusive.
func AtMost(v Version) Set {
	return Set{
		setI: setBound{
			v:  v,
			op: setBoundLTE,
		},
	}
}

type setBoundOp rune

const setBoundGT = '>'
const setBoundGTE = '≥'
const setBoundLT = '<'
const setBoundLTE = '≤'