blob: 54cb80e536830d0d399a11fbb52bcb42699baa5c [file] [log] [blame]
Alan Stokes3189af02021-09-30 17:51:19 +01001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Common items useful for binder clients and/or servers.
18
19use binder::public_api::{ExceptionCode, Status};
20use std::ffi::CString;
21
22/// Constructs a new Binder error `Status` with the given `ExceptionCode` and message.
23pub fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status {
24 match exception {
25 ExceptionCode::SERVICE_SPECIFIC => new_binder_service_specific_error(-1, message),
26 _ => Status::new_exception(exception, to_cstring(message).as_deref()),
27 }
28}
29
30/// Constructs a Binder `Status` representing a service-specific exception with the given code and
31/// message.
32pub fn new_binder_service_specific_error<T: AsRef<str>>(code: i32, message: T) -> Status {
33 Status::new_service_specific_error(code, to_cstring(message).as_deref())
34}
35
36fn to_cstring<T: AsRef<str>>(message: T) -> Option<CString> {
37 CString::new(message.as_ref()).ok()
38}