pub struct Vector(/* private fields */);Expand description
CVSS v2 vector.
Notes:
- Represented internally as a
u64. See Internal Representation below. - When iterating the metrics in a
Vectoror converting aVectorto a string, the metrics are sorted in the order specified in Table 11 of Section 2.4 of the CVSS v2.0 documentation; the sort order of metrics within the source vector string is not preserved. See Examples below. - Optional metrics with a value of
Not Defined (ND)are skipped when iterating the metrics in aVectorand when converting aVectorto a string. See Examples below.
§Examples
// CVSS v2 vector string
let s = "AV:N/AC:L/Au:N/C:C/I:C/A:C";
// parse string as Vector
let v: Vector = s.parse()?;Get base score:
// parse CVSS v2 vector string
let v: Vector = "AV:N/AC:L/Au:N/C:N/I:N/A:C".parse()?;
// get scores
let scores = Scores::from(v);
// check result
assert_eq!(scores.base, Score::from(7.8));Iterate over vector metrics:
// parse vector string
let v: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
// iterate over and print each metric
for m in v {
println!("metric: {m}");
}Get metric from vector:
// parse vector string
let v: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
// get metric
let metric = v.get(Name::AccessVector);
// check result
assert_eq!(metric, Metric::AccessVector(AccessVector::Network));Show that the order of metrics within a vector string is not
preserved when parsing a vector string and then converting the
Vector back to a string:
// vector string with first two metrics (AV and AC) swapped
let s = "AC:L/AV:N/Au:N/C:C/I:C/A:C";
// expected result after parsing vector string above and converting
// the parsed vector back to a vector string
let exp = "AV:N/AC:L/Au:N/C:C/I:C/A:C";
// parse vector string, then convert parsed vector back to vector string
let got = s.parse::<Vector>()?.to_string();
// check result
assert_eq!(got, exp);Show that optional metrics with a value of Not Defined (ND) are
not preserved when parsing a vector string and then converting the
Vector back to a string:
// vector string which contains an optional metric (CR) with a
// value of `Not Defined (ND)`
let s = "AV:N/AC:L/Au:N/C:C/I:C/A:C/CR:ND";
// expected result after parsing vector string above and converting
// the parsed vector back to a vector string
let exp = "AV:N/AC:L/Au:N/C:C/I:C/A:C";
// parse vector string, then convert parsed vector back to vector string
let got = s.parse::<Vector>()?.to_string();
// check result
assert_eq!(got, exp);Verify that a vector is the same size as a u64:
assert_eq!(size_of::<Vector>(), size_of::<u64>());§Internal Representation
A Vector is represented internally as a bit field
in the lower 32 bits (bits 0..32) of a u64, The number of
bits used by a metric value is calculated by the number of possible
values for that metric (e.g. num_bits = ceil(log2(num_vals))):
| # of Values | # of Bits |
|---|---|
| 2 values | 1 bit |
| 3 values | 2 bits |
| 4 values | 2 bits |
| 5 values | 3 bits |
Implementations§
Source§impl Vector
impl Vector
Sourcepub fn get(self, name: Name) -> Metric
pub fn get(self, name: Name) -> Metric
Get Metric from Vector by Name.
§Examples
Get metric from vector:
// parse vector string
let v: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
// get metric
let metric = v.get(Name::AccessVector);
// check result
assert_eq!(metric, Metric::AccessVector(AccessVector::Network));Get optional metric from vector:
// parse vector string
let v: Vector = "AV:N/AC:L/Au:N/C:C/I:C/A:C".parse()?;
// get metric
let metric = v.get(Name::ConfidentialityRequirement);
// check result
assert_eq!(metric, Metric::ConfidentialityRequirement(Requirement::NotDefined));