stak_profiler/record/
procedure_operation.rs

1use crate::error::Error;
2use core::{
3    fmt::{self, Display, Formatter},
4    str::FromStr,
5};
6
7/// A procedure operation.
8#[derive(Debug, Clone, Copy, Eq, PartialEq)]
9pub enum ProcedureOperation {
10    /// A call.
11    Call,
12    /// A return.
13    Return,
14    /// A return (tail) call.
15    ReturnCall,
16}
17
18impl Display for ProcedureOperation {
19    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
20        match self {
21            Self::Call => write!(formatter, "call"),
22            Self::Return => write!(formatter, "return"),
23            Self::ReturnCall => write!(formatter, "return_call"),
24        }
25    }
26}
27
28impl FromStr for ProcedureOperation {
29    type Err = Error;
30
31    fn from_str(string: &str) -> Result<Self, Self::Err> {
32        match string {
33            "call" => Ok(Self::Call),
34            "return" => Ok(Self::Return),
35            "return_call" => Ok(Self::ReturnCall),
36            _ => Err(Error::UnknownRecordType),
37        }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_display() {
47        assert_eq!(ProcedureOperation::Call.to_string(), "call");
48        assert_eq!(ProcedureOperation::Return.to_string(), "return");
49        assert_eq!(ProcedureOperation::ReturnCall.to_string(), "return_call");
50    }
51
52    #[test]
53    fn test_from_str() {
54        assert_eq!("call".parse(), Ok(ProcedureOperation::Call));
55        assert_eq!("return".parse(), Ok(ProcedureOperation::Return));
56        assert_eq!("return_call".parse(), Ok(ProcedureOperation::ReturnCall));
57        assert_eq!(
58            "unknown".parse::<ProcedureOperation>(),
59            Err(Error::UnknownRecordType)
60        );
61    }
62}