From f9ba001072fa8545565a1305edeb3f122db07367 Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Tue, 1 Oct 2024 11:55:36 -0500 Subject: [PATCH] fix simple_type_name when handling lists or types with generics --- src/attachment.rs | 12 +++++++++++- src/context.rs | 8 ++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/attachment.rs b/src/attachment.rs index e6d538f..66dd457 100644 --- a/src/attachment.rs +++ b/src/attachment.rs @@ -19,6 +19,12 @@ impl Debug for A where A: fmt::Debug + Send + Sync + 'static {} #[derive(Debug)] pub struct Dbg(pub A); +impl Dbg { + pub fn format(attachment: impl fmt::Debug) -> Self { + Self(format!("{attachment:?}")) + } +} + impl fmt::Display for Dbg { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.0) @@ -237,8 +243,12 @@ pub fn hms_string(duration: Duration) -> String { hms } -pub(crate) fn simple_type_name() -> &'static str { +pub fn simple_type_name() -> &'static str { let full_type = std::any::type_name::(); + // Option, [T], Vec + if full_type.contains(['<', '[']) { + return full_type; + } full_type.rsplit_once("::").map_or(full_type, |t| t.1) } diff --git a/src/context.rs b/src/context.rs index f4fddb2..7793fd0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -3,8 +3,8 @@ use std::{path::Path, time::Duration}; use error_stack::Context; use crate::{ - attachment::{self, simple_type_name, FromTo, Unsupported}, - ty, AttachExt, Report, Reportable, + attachment::{self, simple_type_name, Display, FromTo, Unsupported}, + ty, AttachExt, Index, Report, Reportable, }; use crate::{attachment::DisplayDuration, reportable, Field}; @@ -189,6 +189,10 @@ impl NotFound { pub fn with_field(field: &'static str) -> Report { Report::new(Self).attach_printable(Field::new(field, attachment::Missing)) } + + pub fn with_index(key: K) -> Report { + Self::with_kv(Index(key), ty!(T)) + } } impl ParseError {