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 use crate::ipc_conn; 17 use std::fmt::{Debug, Display, Formatter}; 18 19 /// Struct of Synchronization Error. 20 pub enum SyncError { 21 /// Unknown error 22 Unknown, 23 24 /// Target table is not in the source database 25 NoSuchTableInDb, 26 27 /// Error because the input is not an asset value and can't call asset relating functions 28 NotAnAsset, 29 30 /// General asset download failure 31 AssetDownloadFailure, 32 33 /// General asset upload failure 34 AssetUploadFailure, 35 36 /// Session has been unlocked and can't be unclocked again 37 SessionUnlocked, 38 39 /// Unsupported functions or features 40 Unsupported, 41 42 /// IPCError 43 IPCError(ipc_conn::Error), 44 45 /// A bunch of IPC Errors 46 IPCErrors(Vec<ipc_conn::Error>), 47 48 /// IO Error 49 IO(std::io::Error), 50 } 51 52 impl Clone for SyncError { clone(&self) -> Self53 fn clone(&self) -> Self { 54 match self { 55 SyncError::Unknown => SyncError::Unknown, 56 SyncError::NoSuchTableInDb => SyncError::NoSuchTableInDb, 57 SyncError::NotAnAsset => SyncError::NotAnAsset, 58 SyncError::AssetDownloadFailure => SyncError::AssetDownloadFailure, 59 SyncError::AssetUploadFailure => SyncError::AssetUploadFailure, 60 SyncError::SessionUnlocked => SyncError::SessionUnlocked, 61 SyncError::Unsupported => SyncError::Unsupported, 62 SyncError::IPCError(a) => SyncError::IPCError(a.clone()), 63 SyncError::IPCErrors(a) => SyncError::IPCErrors(a.clone()), 64 SyncError::IO(a) => { 65 let e = std::io::Error::from(a.kind()); 66 SyncError::IO(e) 67 } 68 } 69 } 70 } 71 72 impl From<std::io::Error> for SyncError { from(value: std::io::Error) -> Self73 fn from(value: std::io::Error) -> Self { 74 SyncError::IO(value) 75 } 76 } 77 78 impl From<ipc_conn::Error> for SyncError { from(value: ipc_conn::Error) -> Self79 fn from(value: ipc_conn::Error) -> Self { 80 SyncError::IPCError(value) 81 } 82 } 83 84 impl From<ipc_conn::Errors> for SyncError { from(value: ipc_conn::Errors) -> Self85 fn from(value: ipc_conn::Errors) -> Self { 86 SyncError::IPCErrors(value.0) 87 } 88 } 89 90 impl Debug for SyncError { fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result91 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 92 let str = match self { 93 SyncError::Unknown => "unknown".to_string(), 94 SyncError::NotAnAsset => "NotAnAsset".to_string(), 95 SyncError::AssetDownloadFailure => "AssetDownloadFailure".to_string(), 96 SyncError::AssetUploadFailure => "AssetUploadFailure".to_string(), 97 SyncError::NoSuchTableInDb => "NoSuchTableInDb".to_string(), 98 SyncError::SessionUnlocked => "SessionUnlocked".to_string(), 99 SyncError::Unsupported => "Unsupported".to_string(), 100 SyncError::IPCError(e) => e.to_string(), 101 SyncError::IPCErrors(es) => { 102 let mut ret = String::new(); 103 for (idx, e) in es.iter().enumerate() { 104 ret.push_str(&format!("\n\tidx {}: {}", idx, e)); 105 } 106 ret 107 } 108 SyncError::IO(e) => e.to_string(), 109 }; 110 write!(f, "{}", str) 111 } 112 } 113 114 impl Display for SyncError { fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result115 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 116 write!(f, "{:?}", self) 117 } 118 } 119 120 impl std::error::Error for SyncError {} 121