summaryrefslogtreecommitdiffstats
path: root/models/quota/limit_subject.go
blob: 4a49d33575c9c9d3d1b9f1146639827c2fefd038 (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
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package quota

import "fmt"

type (
	LimitSubject  int
	LimitSubjects []LimitSubject
)

const (
	LimitSubjectNone LimitSubject = iota
	LimitSubjectSizeAll
	LimitSubjectSizeReposAll
	LimitSubjectSizeReposPublic
	LimitSubjectSizeReposPrivate
	LimitSubjectSizeGitAll
	LimitSubjectSizeGitLFS
	LimitSubjectSizeAssetsAll
	LimitSubjectSizeAssetsAttachmentsAll
	LimitSubjectSizeAssetsAttachmentsIssues
	LimitSubjectSizeAssetsAttachmentsReleases
	LimitSubjectSizeAssetsArtifacts
	LimitSubjectSizeAssetsPackagesAll
	LimitSubjectSizeWiki

	LimitSubjectFirst = LimitSubjectSizeAll
	LimitSubjectLast  = LimitSubjectSizeWiki
)

var limitSubjectRepr = map[string]LimitSubject{
	"none":                             LimitSubjectNone,
	"size:all":                         LimitSubjectSizeAll,
	"size:repos:all":                   LimitSubjectSizeReposAll,
	"size:repos:public":                LimitSubjectSizeReposPublic,
	"size:repos:private":               LimitSubjectSizeReposPrivate,
	"size:git:all":                     LimitSubjectSizeGitAll,
	"size:git:lfs":                     LimitSubjectSizeGitLFS,
	"size:assets:all":                  LimitSubjectSizeAssetsAll,
	"size:assets:attachments:all":      LimitSubjectSizeAssetsAttachmentsAll,
	"size:assets:attachments:issues":   LimitSubjectSizeAssetsAttachmentsIssues,
	"size:assets:attachments:releases": LimitSubjectSizeAssetsAttachmentsReleases,
	"size:assets:artifacts":            LimitSubjectSizeAssetsArtifacts,
	"size:assets:packages:all":         LimitSubjectSizeAssetsPackagesAll,
	"size:assets:wiki":                 LimitSubjectSizeWiki,
}

func (subject LimitSubject) String() string {
	for repr, limit := range limitSubjectRepr {
		if limit == subject {
			return repr
		}
	}
	return "<unknown>"
}

func (subjects LimitSubjects) GoString() string {
	return fmt.Sprintf("%T{%+v}", subjects, subjects)
}

func ParseLimitSubject(repr string) (LimitSubject, error) {
	result, has := limitSubjectRepr[repr]
	if !has {
		return LimitSubjectNone, ErrParseLimitSubjectUnrecognized{Subject: repr}
	}
	return result, nil
}