Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

status.gno

2.70 Kb · 83 lines
 1package releases
 2
 3import "strings"
 4
 5// Status is where a release stands. It is stored on the release, except
 6// Latest, which is computed: the newest Supported release of a project is its
 7// Latest, and storing that would mean rewriting the previous one every time a
 8// new version ships.
 9type Status int
10
11const (
12	// Draft is listed but not announced: the code may not be on chain yet.
13	Draft Status = iota
14	// Prerelease is on chain and usable, with no promise attached.
15	Prerelease
16	// Supported is a release its owner still answers for.
17	Supported
18	// Latest is the newest Supported release. Computed, never stored.
19	Latest
20	// Deprecated still works, says so, and goes read-only at its sunset.
21	Deprecated
22	// Yanked was withdrawn: it stays listed, because a version that existed
23	// cannot un-exist, and every write is refused.
24	Yanked
25	// Vulnerable has a known flaw: it stays listed and everything is refused.
26	Vulnerable
27)
28
29var statusWords = []string{"draft", "prerelease", "supported", "latest", "deprecated", "yanked", "vulnerable"}
30
31var statusEmoji = []string{"📝", "🧪", "✅", "🌟", "⚠️", "🚫", "🔴"}
32
33// statusSeverity orders statuses by how much a reader should worry, so a
34// table can sort on it and a dashboard can pick the worst one. Draft and
35// Prerelease tie: neither is a promise.
36var statusSeverity = []int{2, 2, 1, 0, 3, 4, 5}
37
38func (s Status) valid() bool { return s >= Draft && s <= Vulnerable }
39
40// String is the lower-case word, the same one ParseStatus reads back.
41func (s Status) String() string {
42	if !s.valid() {
43		return "unknown"
44	}
45	return statusWords[s]
46}
47
48// Emoji is the one-glyph form a table shows in front of the word.
49func (s Status) Emoji() string {
50	if !s.valid() {
51		return "❓"
52	}
53	return statusEmoji[s]
54}
55
56// Label is the emoji and the word together, "⚠️ deprecated".
57func (s Status) Label() string { return s.Emoji() + " " + s.String() }
58
59// Severity is 0 for Latest and grows with how much a reader should worry:
60// Supported 1, Draft and Prerelease 2, Deprecated 3, Yanked 4, Vulnerable 5.
61func (s Status) Severity() int {
62	if !s.valid() {
63		return statusSeverity[Vulnerable]
64	}
65	return statusSeverity[s]
66}
67
68// Storable reports whether an owner may set this status. Latest is refused,
69// since it is derived from the others.
70func (s Status) Storable() bool { return s.valid() && s != Latest }
71
72// ParseStatus reads a status word, case-insensitively. ok is false for
73// anything else, "latest" included, since a stored Latest would lie the moment
74// a newer release ships.
75func ParseStatus(raw string) (Status, bool) {
76	w := strings.ToLower(strings.TrimSpace(raw))
77	for i, word := range statusWords {
78		if word == w && Status(i).Storable() {
79			return Status(i), true
80		}
81	}
82	return Draft, false
83}