From 15d13af7d531c5f7c14243bb1d3f5f0bfb27fa26 Mon Sep 17 00:00:00 2001 From: veeso Date: Sun, 20 Jun 2021 11:00:31 +0200 Subject: [PATCH] wip --- Cargo.toml | 9 ++++++--- src/system/keys/keyringstorage.rs | 7 ++++--- src/system/keys/mod.rs | 28 +++++++++++++--------------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e5334aa..83fba59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ dirs = "3.0.1" edit = "0.1.3" getopts = "0.2.21" hostname = "0.3.1" +keyring = { version = "0.10.1", optional = true } lazy_static = "1.4.0" log = "0.4.14" magic-crypt = "3.1.7" @@ -70,14 +71,16 @@ version = "2.1.0" [features] githubActions = [] +with-containers = [] +with-keyring = [ "keyring" ] [target."cfg(any(target_os = \"unix\", target_os = \"macos\", target_os = \"linux\"))"] [target."cfg(any(target_os = \"unix\", target_os = \"macos\", target_os = \"linux\"))".dependencies] users = "0.11.0" -[target."cfg(any(target_os = \"windows\", target_os = \"macos\"))"] -[target."cfg(any(target_os = \"windows\", target_os = \"macos\"))".dependencies] -keyring = "0.10.1" +[target."cfg(any(target_os = \"windows\", target_os = \"macos\", target_os = \"linux\"))"] +[target."cfg(any(target_os = \"windows\", target_os = \"macos\", target_os = \"linux\"))".features] +default = [ "keyring" ] [target."cfg(target_os = \"windows\")"] [target."cfg(target_os = \"windows\")".dependencies] diff --git a/src/system/keys/keyringstorage.rs b/src/system/keys/keyringstorage.rs index 5fae681..8cdc2fa 100644 --- a/src/system/keys/keyringstorage.rs +++ b/src/system/keys/keyringstorage.rs @@ -39,7 +39,6 @@ pub struct KeyringStorage { username: String, } -#[cfg(not(tarpaulin_include))] impl KeyringStorage { /// ### new /// @@ -51,7 +50,6 @@ impl KeyringStorage { } } -#[cfg(not(tarpaulin_include))] impl KeyStorage for KeyringStorage { /// ### get_key /// @@ -68,7 +66,10 @@ impl KeyStorage for KeyringStorage { KeyringError::WindowsVaultError => Err(KeyStorageError::NoSuchKey), #[cfg(target_os = "macos")] KeyringError::MacOsKeychainError(_) => Err(KeyStorageError::NoSuchKey), - _ => panic!("{}", e), + #[cfg(target_os = "linux")] + KeyringError::SecretServiceError(SsError) => Err(KeyStorageError::ProviderError), + KeyringError::Parse(_) => Err(KeyStorageError::BadSytax), + _ => Err(KeyStorageError::ProviderError), }, } } diff --git a/src/system/keys/mod.rs b/src/system/keys/mod.rs index 4933a8e..f89418c 100644 --- a/src/system/keys/mod.rs +++ b/src/system/keys/mod.rs @@ -29,28 +29,22 @@ pub mod filestorage; #[cfg(any(target_os = "windows", target_os = "macos"))] pub mod keyringstorage; +// ext +use thiserror::Error; /// ## KeyStorageError /// /// defines the error type for the `KeyStorage` -#[derive(PartialEq, std::fmt::Debug)] +#[derive(Debug, Error, PartialEq)] pub enum KeyStorageError { - //BadKey, + #[error("Key has a bad syntax")] + BadSytax, + #[error("Provider service error")] ProviderError, + #[error("No such key")] NoSuchKey, } -impl std::fmt::Display for KeyStorageError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let err: String = String::from(match &self { - //KeyStorageError::BadKey => "Bad key syntax", - KeyStorageError::ProviderError => "Provider service error", - KeyStorageError::NoSuchKey => "No such key", - }); - write!(f, "{}", err) - } -} - /// ## KeyStorage /// /// this traits provides the methods to communicate and interact with the key storage. @@ -83,11 +77,15 @@ mod tests { #[test] fn test_system_keys_mod_errors() { assert_eq!( - format!("{}", KeyStorageError::ProviderError), + KeyStorageError::BadSytax.to_string(), + String::from("Key has a bad syntax") + ); + assert_eq!( + KeyStorageError::ProviderError.to_string(), String::from("Provider service error") ); assert_eq!( - format!("{}", KeyStorageError::NoSuchKey), + KeyStorageError::NoSuchKey.to_string(), String::from("No such key") ); }