aboutsummaryrefslogtreecommitdiff
path: root/cvss/score.go
blob: e6a6faa435807c0e86db300b3540b00d6cbb657e (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
package cvss

import (
  "fmt"
)

// Individual CVSS score.
//
// Note: since scores range from 0.0 to 10.0 with one decimal place of
// precision, they can be safely represented as a uint8.
type Score uint8

// Return floating point representation of score.
func NewScore(val float64) (Score, error) {
  // check score range
  if val < 0.0 || val > 10.0 {
    return Score(0), fmt.Errorf("score value out of range [0, 10]: %f", val)
  }

  // convert to score, return success
  return Score(uint8(10.0 * val)), nil
}

// Return string representation of score.
func (s Score) String() string {
  return fmt.Sprintf("%d.%d", s / 10, s % 10)
}

// Return floating point representation of score.
func (s Score) Float() float32 {
  return float32(s) / 10.0
}