aboutsummaryrefslogtreecommitdiff
path: root/cvss/score.go
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-07 11:07:39 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-07 11:07:39 -0500
commitf2cef211cda0d86ccd42e8aa411f9865a2a22cce (patch)
treede9537c79853f496141e737206e65e33fb1637c0 /cvss/score.go
parent19bcc5e8df039f5de0f46fd6aa71e425a5e797bb (diff)
downloadcvez-f2cef211cda0d86ccd42e8aa411f9865a2a22cce.tar.bz2
cvez-f2cef211cda0d86ccd42e8aa411f9865a2a22cce.zip
cvss: rename v2Score to Score, update deps and tests
Diffstat (limited to 'cvss/score.go')
-rw-r--r--cvss/score.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/cvss/score.go b/cvss/score.go
new file mode 100644
index 0000000..e6a6faa
--- /dev/null
+++ b/cvss/score.go
@@ -0,0 +1,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
+}