rust_util/
lib.rs

1// Copyright 2022 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 fidl_fidl_clientsuite::FidlErrorKind;
6
7/// Classify a [`fidl::Error`] as a [`FidlErrorKind`] that can be returned in a
8/// dynsuite client test.
9pub fn classify_error(error: fidl::Error) -> FidlErrorKind {
10    match error {
11        fidl::Error::InvalidBoolean
12        | fidl::Error::InvalidHeader
13        | fidl::Error::IncompatibleMagicNumber(_)
14        | fidl::Error::UnsupportedWireFormatVersion
15        | fidl::Error::InvalidResponseOrdinal { .. }
16        | fidl::Error::Invalid
17        | fidl::Error::OutOfRange
18        | fidl::Error::ExtraBytes
19        | fidl::Error::ExtraHandles
20        | fidl::Error::NonZeroPadding { .. }
21        | fidl::Error::MaxRecursionDepth
22        | fidl::Error::NotNullable
23        | fidl::Error::UnexpectedNullRef
24        | fidl::Error::Utf8Error
25        | fidl::Error::InvalidBitsValue
26        | fidl::Error::InvalidEnumValue
27        | fidl::Error::UnknownUnionTag
28        | fidl::Error::InvalidPresenceIndicator
29        | fidl::Error::InvalidInlineBitInEnvelope
30        | fidl::Error::InvalidInlineMarkerInEnvelope
31        | fidl::Error::InvalidNumBytesInEnvelope
32        | fidl::Error::InvalidHostHandle
33        | fidl::Error::IncorrectHandleSubtype { .. }
34        | fidl::Error::MissingExpectedHandleRights { .. } => FidlErrorKind::DecodingError,
35
36        fidl::Error::UnknownOrdinal { .. }
37        | fidl::Error::InvalidResponseTxid { .. }
38        | fidl::Error::UnexpectedSyncResponse => FidlErrorKind::UnexpectedMessage,
39
40        fidl::Error::UnsupportedMethod { .. } => FidlErrorKind::UnknownMethod,
41
42        fidl::Error::ClientChannelClosed { .. } => FidlErrorKind::ChannelPeerClosed,
43
44        _ => FidlErrorKind::OtherError,
45    }
46}
47
48/// Returns the FIDL method name for a request/event enum value.
49// TODO(https://fxbug.dev/42078541): Provide this in FIDL bindings.
50pub fn method_name(request_or_event: &impl std::fmt::Debug) -> String {
51    let mut string = format!("{:?}", request_or_event);
52    let len = string.find('{').unwrap_or(string.len());
53    let len = string[..len].trim_end().len();
54    string.truncate(len);
55    assert!(
56        string.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'),
57        "failed to parse the method name from {:?}",
58        request_or_event
59    );
60    string
61}