xref: /base/msdp/device_status/rust/utils/src/errors.rs (revision f857971d)
1/*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16//! Error codes definitions.
17
18use std::ffi::NulError;
19
20/// Error codes.
21#[derive(Debug)]
22#[repr(i32)]
23pub enum FusionErrorCode {
24    /// Operation failed.
25    Fail,
26    /// Invalid input parameter(s).
27    InvalidParam,
28}
29
30impl From<NulError> for FusionErrorCode {
31    fn from(_: NulError) -> Self {
32        FusionErrorCode::Fail
33    }
34}
35
36impl From<FusionErrorCode> for i32 {
37    fn from(value: FusionErrorCode) -> Self
38    {
39        match value {
40            FusionErrorCode::Fail => { -1i32 },
41            FusionErrorCode::InvalidParam => { -2i32 },
42        }
43    }
44}
45
46impl TryFrom<i32> for FusionErrorCode {
47    type Error = FusionErrorCode;
48
49    fn try_from(value: i32) -> Result<Self, Self::Error> {
50        match value {
51            _ if i32::from(FusionErrorCode::Fail) == value => { Ok(FusionErrorCode::Fail) },
52            _ if i32::from(FusionErrorCode::InvalidParam) == value => { Ok(FusionErrorCode::InvalidParam) },
53            _ => { Err(FusionErrorCode::Fail) },
54        }
55    }
56}
57
58impl std::fmt::Display for FusionErrorCode {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            FusionErrorCode::Fail => write!(f, "Operation failed."),
62            FusionErrorCode::InvalidParam => write!(f, "Invalid input parameter(s).")
63        }
64    }
65}
66
67/// Fusion Interaction specific Result, error is FusionErrorCode type
68pub type FusionResult<T> = std::result::Result<T, FusionErrorCode>;
69