From cdc4c172c42b5c31b3063afd20cc7055d60f9af8 Mon Sep 17 00:00:00 2001 From: Laurent Mazare Date: Fri, 15 Mar 2024 08:37:27 +0100 Subject: [PATCH] Implement the error trait for DTypeParseError. (#1852) --- candle-core/src/dtype.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/candle-core/src/dtype.rs b/candle-core/src/dtype.rs index 94ca57d8..1a698a35 100644 --- a/candle-core/src/dtype.rs +++ b/candle-core/src/dtype.rs @@ -23,7 +23,15 @@ pub enum DType { } #[derive(Debug, PartialEq, Eq)] -pub struct DTypeParseError; +pub struct DTypeParseError(String); + +impl std::fmt::Display for DTypeParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "cannot parse '{}' as a dtype", self.0) + } +} + +impl std::error::Error for DTypeParseError {} impl std::str::FromStr for DType { type Err = DTypeParseError; @@ -36,7 +44,7 @@ impl std::str::FromStr for DType { "f16" => Ok(Self::F16), "f32" => Ok(Self::F32), "f64" => Ok(Self::F64), - _ => Err(DTypeParseError), + _ => Err(DTypeParseError(s.to_string())), } } }