From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 8AAC972305 for ; Mon, 12 Apr 2021 11:33:26 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 812861C70D for ; Mon, 12 Apr 2021 11:32:56 +0200 (CEST) Received: from elsa.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP id 007D61C6EB for ; Mon, 12 Apr 2021 11:32:54 +0200 (CEST) Received: by elsa.proxmox.com (Postfix, from userid 0) id C9AA9AEAFAE; Mon, 12 Apr 2021 11:32:48 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Mon, 12 Apr 2021 11:32:46 +0200 Message-Id: <20210412093247.14191-1-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RDNS_NONE 1.274 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH 1/2] sgutils2: use thiserror to derive Error X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Apr 2021 09:33:26 -0000 --- Cargo.toml | 1 + src/tools/sgutils2.rs | 42 ++++++++++-------------------------------- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1d47423e..8e85675c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ endian_trait = { version = "0.6", features = ["arrays"] } env_logger = "0.7" flate2 = "1.0" anyhow = "1.0" +thiserror = "1.0" futures = "0.3" h2 = { version = "0.3", features = [ "stream" ] } handlebars = "3.0" diff --git a/src/tools/sgutils2.rs b/src/tools/sgutils2.rs index 3570aca7..df00fbf5 100644 --- a/src/tools/sgutils2.rs +++ b/src/tools/sgutils2.rs @@ -17,16 +17,16 @@ use std::ffi::CStr; use proxmox::tools::io::ReadExt; -#[derive(Debug)] +#[derive(thiserror::Error, Debug)] pub struct SenseInfo { pub sense_key: u8, pub asc: u8, pub ascq: u8, } -impl ToString for SenseInfo { +impl std::fmt::Display for SenseInfo { - fn to_string(&self) -> String { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let sense_text = SENSE_KEY_DESCRIPTIONS .get(self.sense_key as usize) @@ -34,43 +34,21 @@ impl ToString for SenseInfo { .unwrap_or_else(|| format!("Invalid sense {:02X}", self.sense_key)); if self.asc == 0 && self.ascq == 0 { - return sense_text; + write!(f, "{}", sense_text)?; } let additional_sense_text = get_asc_ascq_string(self.asc, self.ascq); - format!("{}, {}", sense_text, additional_sense_text) + write!(f, "{}, {}", sense_text, additional_sense_text) } } -#[derive(Debug)] +#[derive(thiserror::Error, Debug)] pub enum ScsiError { - Error(Error), - Sense(SenseInfo), -} - -impl std::fmt::Display for ScsiError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - ScsiError::Error(err) => write!(f, "{}", err), - ScsiError::Sense(sense) => write!(f, "{}", sense.to_string()), - } - } -} - -impl std::error::Error for ScsiError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - ScsiError::Error(err) => err.source(), - ScsiError::Sense(_) => None, - } - } -} - -impl From for ScsiError { - fn from(error: anyhow::Error) -> Self { - Self::Error(error) - } + #[error("{0}")] + Error(#[from] Error), + #[error("{0}")] + Sense(#[from] SenseInfo), } impl From for ScsiError { -- 2.20.1