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 UTF8SMTPS id 8F29B7239D for ; Mon, 12 Apr 2021 12:05:25 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with UTF8SMTP id 7C16C1CD5B for ; Mon, 12 Apr 2021 12:04:55 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with UTF8SMTPS id 420B31CD4B for ; Mon, 12 Apr 2021 12:04:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with UTF8SMTP id 0D1C542009; Mon, 12 Apr 2021 12:04:54 +0200 (CEST) Message-ID: Date: Mon, 12 Apr 2021 12:04:53 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:88.0) Gecko/20100101 Thunderbird/88.0 Content-Language: en-US To: Proxmox Backup Server development discussion , Dietmar Maurer References: <20210412093247.14191-1-dietmar@proxmox.com> From: Dominik Csapak In-Reply-To: <20210412093247.14191-1-dietmar@proxmox.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.166 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment NICE_REPLY_A -0.001 Looks like a legit reply (A) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: Re: [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 10:05:25 -0000 comment inline: On 4/12/21 11:32, Dietmar Maurer wrote: > --- > 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)?; here a return is missing, else we print sense_text twice and always get the additional_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 { >