ctrf-rs 0.2.0

A reporter for Common Test Report Format (CTRF) in Rust
Documentation
// std import(s)
use std::ops::Sub;

// other import(s)
use serde::{Deserialize, Serialize};

/// Metric Delta element for a CTRF report.
/// Corresponds to the spec's ["Metric Delta"](https://www.ctrf.io/docs/specification/insights#metric-delta) object.
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct MetricDelta<T>
where
    T: Sub,
{
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current: Option<T>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub baseline: Option<T>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub change: Option<T>,
}

impl<T> MetricDelta<T>
where
    T: Copy + Sub<Output = T>,
{
    /// Creates an instance of `MetricDelta<T>`, calculating the `change` from `baseline` to
    /// `current` if neither is `None`, or `None` otherwise
    pub fn new(current: Option<T>, baseline: Option<T>) -> Self {
        Self {
            current,
            baseline,
            change: current.zip(baseline).map(|(c, b)| c - b),
        }
    }
}