Vector

Struct Vector 

Source
pub struct Vector(/* private fields */);
Expand description

CVSS v2 vector.

Notes:

§Examples

Parse a &str into a Vector:

// 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 values1 bit
3 values2 bits
4 values2 bits
5 values3 bits

Implementations§

Source§

impl Vector

Source

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));

Trait Implementations§

Source§

impl Clone for Vector

Source§

fn clone(&self) -> Vector

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Vector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Vector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<Vector> for Score

Source§

fn from(vec: Vector) -> Score

Converts to this type from the input type.
Source§

impl From<Vector> for Scores

Source§

fn from(vec: Vector) -> Scores

Converts to this type from the input type.
Source§

impl From<Vector> for Vector

Source§

fn from(vec: Vector) -> Self

Converts to this type from the input type.
Source§

impl From<Vector> for Vector

Source§

fn from(vec: Vector) -> Vector

Converts to this type from the input type.
Source§

impl From<Vector> for Version

Source§

fn from(vec: Vector) -> Version

Converts to this type from the input type.
Source§

impl From<Vector> for u64

Source§

fn from(vec: Vector) -> u64

Converts to this type from the input type.
Source§

impl FromStr for Vector

Source§

type Err = Err

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl IntoIterator for Vector

Source§

type Item = Metric

The type of the elements being iterated over.
Source§

type IntoIter = VectorIterator

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Vector

Source§

fn eq(&self, other: &Vector) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<String> for Vector

Source§

type Error = Err

The type returned in the event of a conversion error.
Source§

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Vector

Source§

impl Eq for Vector

Source§

impl StructuralPartialEq for Vector

Auto Trait Implementations§

§

impl Freeze for Vector

§

impl RefUnwindSafe for Vector

§

impl Send for Vector

§

impl Sync for Vector

§

impl Unpin for Vector

§

impl UnwindSafe for Vector

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.