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 
18 use std::ffi::NulError;
19 
20 /// Error codes.
21 #[derive(Debug)]
22 #[repr(i32)]
23 pub enum FusionErrorCode {
24     /// Operation failed.
25     Fail,
26     /// Invalid input parameter(s).
27     InvalidParam,
28 }
29 
30 impl From<NulError> for FusionErrorCode {
from(_: NulError) -> Self31     fn from(_: NulError) -> Self {
32         FusionErrorCode::Fail
33     }
34 }
35 
36 impl From<FusionErrorCode> for i32 {
from(value: FusionErrorCode) -> Self37     fn from(value: FusionErrorCode) -> Self
38     {
39         match value {
40             FusionErrorCode::Fail => { -1i32 },
41             FusionErrorCode::InvalidParam => { -2i32 },
42         }
43     }
44 }
45 
46 impl TryFrom<i32> for FusionErrorCode {
47     type Error = FusionErrorCode;
48 
try_from(value: i32) -> Result<Self, Self::Error>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 
58 impl std::fmt::Display for FusionErrorCode {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result59     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
68 pub type FusionResult<T> = std::result::Result<T, FusionErrorCode>;
69