use serde::{Deserialize, Serialize};
use crate::AreaScores;
#[derive(Debug, Serialize, Deserialize)]
pub struct ScoreSummaryReport {
pub focus_areas: Vec<String>,
pub runs: Vec<RunSummary>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FocusArea {
pub name: String,
pub areas: Vec<String>,
}
impl From<&str> for FocusArea {
fn from(value: &str) -> Self {
Self {
name: value.to_string(),
areas: vec![value.to_string()],
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RunSummary {
pub date: String,
pub wpt_revision: String,
pub product_version: String,
pub scores: Vec<RunScores>,
}
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct RunScores {
pub total_tests: u32,
#[serde(serialize_with = "as_int_if_int")]
pub total_score: f64, pub total_subtests: u32,
pub total_subtests_passed: u32,
}
impl From<AreaScores> for RunScores {
fn from(scores: AreaScores) -> Self {
Self {
total_tests: scores.tests.total,
total_score: scores.servo_score(),
total_subtests: scores.subtests.total,
total_subtests_passed: scores.subtests.pass,
}
}
}
fn as_int_if_int<S>(x: &f64, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if x.fract() == 0.0 {
s.serialize_u64(*x as u64)
} else {
s.serialize_f64(*x)
}
}