fidl_next_protocol/
flexible_result.rs1use crate::FrameworkError;
6
7#[derive(Clone, Debug)]
9pub enum FlexibleResult<T, E> {
10 Ok(T),
12 Err(E),
14 FrameworkErr(FrameworkError),
16}
17
18impl<T, E> FlexibleResult<T, E> {
19 pub fn is_ok(&self) -> bool {
21 matches!(self, Self::Ok(_))
22 }
23
24 pub fn is_err(&self) -> bool {
26 matches!(self, Self::Err(_))
27 }
28
29 pub fn is_framework_err(&self) -> bool {
31 matches!(self, Self::FrameworkErr(_))
32 }
33
34 pub fn ok(self) -> Option<T> {
36 if let Self::Ok(value) = self { Some(value) } else { None }
37 }
38
39 pub fn err(self) -> Option<E> {
41 if let Self::Err(error) = self { Some(error) } else { None }
42 }
43
44 pub fn framework_err(self) -> Option<FrameworkError> {
46 if let Self::FrameworkErr(error) = self { Some(error) } else { None }
47 }
48
49 pub fn unwrap(self) -> T {
53 self.ok().unwrap()
54 }
55
56 pub fn unwrap_err(self) -> E {
60 self.err().unwrap()
61 }
62
63 pub fn unwrap_framework_err(self) -> FrameworkError {
67 self.framework_err().unwrap()
68 }
69
70 pub fn as_ref(&self) -> FlexibleResult<&T, &E> {
72 match self {
73 Self::Ok(value) => FlexibleResult::Ok(value),
74 Self::Err(error) => FlexibleResult::Err(error),
75 Self::FrameworkErr(framework_error) => FlexibleResult::FrameworkErr(*framework_error),
76 }
77 }
78}
79
80impl<T, E, T2, E2> PartialEq<FlexibleResult<T2, E2>> for FlexibleResult<T, E>
81where
82 T: PartialEq<T2>,
83 E: PartialEq<E2>,
84{
85 fn eq(&self, other: &FlexibleResult<T2, E2>) -> bool {
86 match (self, other) {
87 (FlexibleResult::Ok(lhs), FlexibleResult::Ok(rhs)) => lhs == rhs,
88 (FlexibleResult::Err(lhs), FlexibleResult::Err(rhs)) => lhs == rhs,
89 (FlexibleResult::FrameworkErr(lhs), FlexibleResult::FrameworkErr(rhs)) => lhs == rhs,
90 _ => false,
91 }
92 }
93}