Skip to content

Commit

Permalink
fix simple_type_name when handling lists or types with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
mkatychev committed Oct 1, 2024
1 parent a40c3a9 commit f9ba001
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ impl<A> Debug for A where A: fmt::Debug + Send + Sync + 'static {}
#[derive(Debug)]
pub struct Dbg<A: Debug>(pub A);

impl Dbg<String> {
pub fn format(attachment: impl fmt::Debug) -> Self {
Self(format!("{attachment:?}"))
}
}

impl<A: Debug> fmt::Display for Dbg<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
Expand Down Expand Up @@ -237,8 +243,12 @@ pub fn hms_string(duration: Duration) -> String {
hms
}

pub(crate) fn simple_type_name<T: ?Sized>() -> &'static str {
pub fn simple_type_name<T: ?Sized>() -> &'static str {
let full_type = std::any::type_name::<T>();
// Option<T>, [T], Vec<T>
if full_type.contains(['<', '[']) {
return full_type;
}
full_type.rsplit_once("::").map_or(full_type, |t| t.1)
}

Expand Down
8 changes: 6 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -189,6 +189,10 @@ impl NotFound {
pub fn with_field(field: &'static str) -> Report<Self> {
Report::new(Self).attach_printable(Field::new(field, attachment::Missing))
}

pub fn with_index<T, K: Display>(key: K) -> Report<Self> {
Self::with_kv(Index(key), ty!(T))
}
}

impl ParseError {
Expand Down

0 comments on commit f9ba001

Please sign in to comment.