Skip to content
Myr

metadata.rs

This file

Path

src/export/json/metadata.rs

51 lines · 1.4 KiB · Rust

Last changed by

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

History of this file

Take it

1use serde::Serialize;23#[derive(Debug, Serialize)]4pub(super) struct Metadata {5    hyperfine_version: &'static str,6    #[serde(skip_serializing_if = "Option::is_none")]7    pub start_time: Option<String>,8    pub platform: Platform,9}1011impl Default for Metadata {12    fn default() -> Self {13        Self {14            hyperfine_version: env!("CARGO_PKG_VERSION"),15            start_time: utc_now(),16            platform: Platform::detect(),17        }18    }19}2021fn utc_now() -> Option<String> {22    use std::{fmt::Write, time::SystemTime};2324    let now = SystemTime::now();25    now.duration_since(SystemTime::UNIX_EPOCH).ok()?;26    let mut timestamp = String::new();27    write!(&mut timestamp, "{}", humantime::format_rfc3339_seconds(now)).ok()?;28    Some(timestamp)29}3031#[derive(Debug, Default, Serialize)]32pub(super) struct Platform {33    #[serde(skip_serializing_if = "Option::is_none")]34    os: Option<String>,35    #[serde(skip_serializing_if = "Option::is_none")]36    architecture: Option<String>,37}3839impl Platform {40    fn detect() -> Self {41        use platform_info::{PlatformInfo, PlatformInfoAPI, UNameAPI};4243        let Ok(info) = PlatformInfo::new() else {44            return Self::default();45        };46        Self {47            os: Some(info.sysname().to_string_lossy().into_owned()),48            architecture: Some(info.machine().to_string_lossy().into_owned()),49        }50    }51}