safe_rs/types/
operation.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
7#[repr(u8)]
8pub enum Operation {
9 #[default]
11 Call = 0,
12 DelegateCall = 1,
14}
15
16impl Operation {
17 pub fn as_u8(&self) -> u8 {
19 *self as u8
20 }
21
22 pub fn from_u8(value: u8) -> Option<Self> {
24 match value {
25 0 => Some(Operation::Call),
26 1 => Some(Operation::DelegateCall),
27 _ => None,
28 }
29 }
30}
31
32impl From<Operation> for u8 {
33 fn from(op: Operation) -> Self {
34 op.as_u8()
35 }
36}
37
38impl TryFrom<u8> for Operation {
39 type Error = &'static str;
40
41 fn try_from(value: u8) -> Result<Self, Self::Error> {
42 Operation::from_u8(value).ok_or("Invalid operation value")
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_operation_values() {
52 assert_eq!(Operation::Call.as_u8(), 0);
53 assert_eq!(Operation::DelegateCall.as_u8(), 1);
54 }
55
56 #[test]
57 fn test_operation_from_u8() {
58 assert_eq!(Operation::from_u8(0), Some(Operation::Call));
59 assert_eq!(Operation::from_u8(1), Some(Operation::DelegateCall));
60 assert_eq!(Operation::from_u8(2), None);
61 }
62
63 #[test]
64 fn test_operation_default() {
65 assert_eq!(Operation::default(), Operation::Call);
66 }
67}