Skip to main content

fidl_next_protocol/
flexible_result.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::FrameworkError;
6
7/// A flexible FIDL result.
8#[derive(Clone, Debug)]
9pub enum FlexibleResult<T, E> {
10    /// The value of the flexible call when successful.
11    Ok(T),
12    /// The error returned from a successful flexible call.
13    Err(E),
14    /// The error indicating that the flexible call failed.
15    FrameworkErr(FrameworkError),
16}
17
18impl<T, E> FlexibleResult<T, E> {
19    /// Returns whether the flexible result is `Ok`.
20    pub fn is_ok(&self) -> bool {
21        matches!(self, Self::Ok(_))
22    }
23
24    /// Returns whether the flexible result if `Err`.
25    pub fn is_err(&self) -> bool {
26        matches!(self, Self::Err(_))
27    }
28
29    /// Returns whether the flexible result is `FrameworkErr`.
30    pub fn is_framework_err(&self) -> bool {
31        matches!(self, Self::FrameworkErr(_))
32    }
33
34    /// Returns the `Ok` value of the result, if any.
35    pub fn ok(self) -> Option<T> {
36        if let Self::Ok(value) = self { Some(value) } else { None }
37    }
38
39    /// Returns the `Err` value of the result, if any.
40    pub fn err(self) -> Option<E> {
41        if let Self::Err(error) = self { Some(error) } else { None }
42    }
43
44    /// Returns the `FrameworkErr` value of the result, if any.
45    pub fn framework_err(self) -> Option<FrameworkError> {
46        if let Self::FrameworkErr(error) = self { Some(error) } else { None }
47    }
48
49    /// Returns the contained `Ok` value.
50    ///
51    /// Panics if the result was not `Ok`.
52    pub fn unwrap(self) -> T {
53        self.ok().unwrap()
54    }
55
56    /// Returns the contained `Err` value.
57    ///
58    /// Panics if the result was not `Err`.
59    pub fn unwrap_err(self) -> E {
60        self.err().unwrap()
61    }
62
63    /// Returns the contained `FrameworkErr` value.
64    ///
65    /// Panics if the result was not `FrameworkErr`.
66    pub fn unwrap_framework_err(self) -> FrameworkError {
67        self.framework_err().unwrap()
68    }
69
70    /// Converts from `FlexibleResult<T, E>` to `FlexibleResult<&T, &E>`.
71    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}