📝 README and inline docs

This commit is contained in:
Brian Wiborg 2024-05-14 10:26:48 +02:00
parent d533a879ef
commit c61300c6b8
No known key found for this signature in database
GPG key ID: BE53FA9286B719D6
14 changed files with 139 additions and 24 deletions

View file

@ -5,8 +5,10 @@ import (
"strings"
)
// Threshold defines a custom uint8 type for representing a threshold.
type Threshold uint8
// Supported thresholds are:
const (
Simple Threshold = iota
OneFifth
@ -21,16 +23,19 @@ const (
Unanimous
)
// ValidThresholds returns a []Threshold of all supported thresholds.
func ValidThresholds() []Threshold {
return []Threshold{
Simple, Unanimous,
OneFifth, OneQuarter, OneThird, OneHalf,
TwoFifths, TwoThirds,
ThreeFifths, ThreeQuarters, ThreeFifths,
ThreeQuarters, ThreeFifths,
FourFifths,
}
}
// FromString takes a string and returns an initialized Threshold or an error
// if the string could not be parsed.
func FromString(s string) (Threshold, error) {
for _, t := range ValidThresholds() {
if strings.ToUpper(t.String()) == strings.ToUpper(s) {
@ -40,6 +45,7 @@ func FromString(s string) (Threshold, error) {
return Simple, fmt.Errorf("invalid threshold: %s", s)
}
// String implements fmt.Stringer
func (t Threshold) String() string {
switch t {
case Simple:
@ -69,6 +75,8 @@ func (t Threshold) String() string {
}
}
// IsSatisfied takes the number of all votes, yes votes and no votes and
// returns a bool indicating if the threshold is satisfied.
func (t Threshold) IsSatisfied(totalVotes, yesVotes, noVotes int) bool {
if totalVotes == 0 {
return false