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

project.gno

5.71 Kb · 199 lines
  1package releases
  2
  3import (
  4	"gno.land/p/nt/bptree/v0"
  5)
  6
  7// Project is a changelog: the releases of one thing, in the order they were
  8// listed, each findable by version and by package path.
  9//
 10// It holds no owner. Who may write is the storing realm's question, and the
 11// library stays usable by a realm with one owner, a DAO, or none.
 12type Project struct {
 13	name        string
 14	description string
 15	byVersion   *bptree.BPTree // version -> *Release
 16	byPath      *bptree.BPTree // package path -> version
 17	order       []string       // versions, in the order they were added
 18}
 19
 20// NewProject opens an empty changelog.
 21func NewProject(name, description string) (*Project, error) {
 22	if !ValidName(name) {
 23		return nil, ErrInvalidName
 24	}
 25	if !validText(description, MaxDescriptionLen, false) {
 26		return nil, ErrInvalidText
 27	}
 28	return &Project{
 29		name:        name,
 30		description: description,
 31		// Releases are never removed, so the fanout that wins for a tree that
 32		// only grows (EFFECTIVE_GNO.md 2.5).
 33		byVersion: bptree.NewBPTreeN(128),
 34		byPath:    bptree.NewBPTreeN(128),
 35	}, nil
 36}
 37
 38func (p *Project) Name() string        { return p.name }
 39func (p *Project) Description() string { return p.description }
 40func (p *Project) Size() int           { return len(p.order) }
 41
 42// SetDescription replaces the one-line description.
 43func (p *Project) SetDescription(d string) error {
 44	if !validText(d, MaxDescriptionLen, false) {
 45		return ErrInvalidText
 46	}
 47	p.description = d
 48	return nil
 49}
 50
 51// Add lists a release. Version and package path are both keys and neither can
 52// be listed twice: a version that pointed at different code later would be a
 53// lie told to everyone who pinned it.
 54func (p *Project) Add(version, pkgPath, url, notes string, status Status, height int64) error {
 55	switch {
 56	case !ValidVersion(version):
 57		return ErrInvalidVersion
 58	case !ValidPkgPath(pkgPath):
 59		return ErrInvalidPkgPath
 60	case !validURL(url) || !validText(notes, MaxNotesLen, true):
 61		return ErrInvalidText
 62	case !status.Storable():
 63		return ErrInvalidStatus
 64	case p.byVersion.Has(version):
 65		return ErrVersionExists
 66	case p.byPath.Has(pkgPath):
 67		return ErrPathExists
 68	}
 69	r := &Release{
 70		Version:    version,
 71		PkgPath:    pkgPath,
 72		URL:        url,
 73		Notes:      notes,
 74		Status:     status,
 75		ReleasedAt: height,
 76	}
 77	p.byVersion.Set(version, r)
 78	p.byPath.Set(pkgPath, version)
 79	p.order = append(p.order, version)
 80	return nil
 81}
 82
 83func (p *Project) get(version string) *Release {
 84	r, _ := p.byVersion.Get(version).(*Release)
 85	return r
 86}
 87
 88// SetStatus moves a release to any storable status, with the reason readers
 89// will see. A yanked release is final, and a vulnerable one can only be
 90// yanked: neither can be made safe again, because its code cannot change.
 91// Setting Deprecated this way clears any sunset; Deprecate sets one.
 92func (p *Project) SetStatus(version string, s Status, reason string) error {
 93	return p.setStatus(version, s, reason, 0)
 94}
 95
 96// Deprecate marks a release deprecated. It keeps working, with a banner, until
 97// block height sunset, and is read-only from then on. sunset 0 means never.
 98func (p *Project) Deprecate(version, reason string, sunset int64) error {
 99	if sunset < 0 {
100		return ErrInvalidSunset
101	}
102	return p.setStatus(version, Deprecated, reason, sunset)
103}
104
105// Yank withdraws a release. It stays listed; every write to it is refused.
106func (p *Project) Yank(version, reason string) error {
107	return p.setStatus(version, Yanked, reason, 0)
108}
109
110func (p *Project) setStatus(version string, s Status, reason string, sunset int64) error {
111	if !s.Storable() {
112		return ErrInvalidStatus
113	}
114	if !validText(reason, MaxReasonLen, false) {
115		return ErrInvalidText
116	}
117	r := p.get(version)
118	if r == nil {
119		return ErrNotFound
120	}
121	if r.Status == Yanked {
122		return ErrFinal
123	}
124	if r.Status == Vulnerable && s != Yanked && s != Vulnerable {
125		return ErrVulnerableIsFinal
126	}
127	r.Status = s
128	r.Reason = reason
129	r.Sunset = sunset
130	return nil
131}
132
133// SetSuccessor points readers of version at successor instead of at Latest,
134// for a line that forked. An empty successor clears it.
135func (p *Project) SetSuccessor(version, successor string) error {
136	r := p.get(version)
137	if r == nil {
138		return ErrNotFound
139	}
140	if successor != "" && (successor == version || p.get(successor) == nil) {
141		return ErrNotFound
142	}
143	r.Successor = successor
144	return nil
145}
146
147// ByVersion returns a copy of the release listed under version.
148func (p *Project) ByVersion(version string) (Release, bool) {
149	r := p.get(version)
150	if r == nil {
151		return Release{}, false
152	}
153	return *r, true
154}
155
156// ByPath returns a copy of the release published at pkgPath.
157func (p *Project) ByPath(pkgPath string) (Release, bool) {
158	v, ok := p.byPath.Get(pkgPath).(string)
159	if !ok {
160		return Release{}, false
161	}
162	return p.ByVersion(v)
163}
164
165// Latest is the most recently added Supported release. Order of addition, not
166// version order, decides: versions here need not be semver, and an owner who
167// lists v2 before backfilling v1 meant v2.
168func (p *Project) Latest() (Release, bool) {
169	for i := len(p.order) - 1; i >= 0; i-- {
170		if r := p.get(p.order[i]); r.Status == Supported {
171			return *r, true
172		}
173	}
174	return Release{}, false
175}
176
177// IsLatest reports whether pkgPath is this project's Latest.
178func (p *Project) IsLatest(pkgPath string) bool {
179	l, ok := p.Latest()
180	return ok && l.PkgPath == pkgPath
181}
182
183// StatusOf is the status a reader sees: the stored one, with Supported
184// promoted to Latest for the one release it applies to.
185func (p *Project) StatusOf(r Release) Status {
186	if r.Status == Supported && p.IsLatest(r.PkgPath) {
187		return Latest
188	}
189	return r.Status
190}
191
192// Releases returns copies of every release, newest first.
193func (p *Project) Releases() []Release {
194	out := make([]Release, 0, len(p.order))
195	for i := len(p.order) - 1; i >= 0; i-- {
196		out = append(out, *p.get(p.order[i]))
197	}
198	return out
199}