xref: /aosp_15_r20/external/crosvm/cros_tracing_types/src/lib.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::time::Duration;
6 
7 use anyhow::bail;
8 
9 pub mod static_strings;
10 
11 /// Sets the duration for a trace.
12 pub enum TraceDuration {
13     AlwaysOn,
14     StopIn(Duration),
15 }
16 
17 impl TryFrom<TraceDuration> for Duration {
18     type Error = anyhow::Error;
19 
try_from(duration: TraceDuration) -> anyhow::Result<Self>20     fn try_from(duration: TraceDuration) -> anyhow::Result<Self> {
21         match duration {
22             TraceDuration::AlwaysOn => Ok(Duration::from_millis(0)),
23             TraceDuration::StopIn(d) if !d.is_zero() => Ok(d),
24             TraceDuration::StopIn(_) => {
25                 bail!("zero duration not permitted; did you mean TraceDuration::AlwaysOn?")
26             }
27         }
28     }
29 }
30