]> git.lizzy.rs Git - rust.git/blobdiff - crates/tt/src/lib.rs
Merge #6586
[rust.git] / crates / tt / src / lib.rs
index 20c3f5eabfb8dfe54e2e6e75ff3f51882239c737..6c1bf8d092765dff65c7852eb64eb981c234a621 100644 (file)
@@ -1,10 +1,7 @@
 //! `tt` crate defines a `TokenTree` data structure: this is the interface (both
 //! input and output) of macros. It closely mirrors `proc_macro` crate's
 //! `TokenTree`.
-use std::{
-    fmt::{self, Debug},
-    panic::RefUnwindSafe,
-};
+use std::{fmt, panic::RefUnwindSafe};
 
 use stdx::impl_from;
 
@@ -139,7 +136,7 @@ fn print_debug_token(f: &mut fmt::Formatter<'_>, tkn: &TokenTree, level: usize)
     Ok(())
 }
 
-impl Debug for Subtree {
+impl fmt::Debug for Subtree {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         print_debug_subtree(f, self, 0)
     }
@@ -240,7 +237,18 @@ pub enum ExpansionError {
     ExpansionError(String),
 }
 
-pub trait TokenExpander: Debug + Send + Sync + RefUnwindSafe {
+impl fmt::Display for ExpansionError {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self {
+            ExpansionError::IOError(e) => write!(f, "I/O error: {}", e),
+            ExpansionError::JsonError(e) => write!(f, "JSON decoding error: {}", e),
+            ExpansionError::Unknown(e) => e.fmt(f),
+            ExpansionError::ExpansionError(e) => write!(f, "proc macro returned error: {}", e),
+        }
+    }
+}
+
+pub trait TokenExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
     fn expand(&self, subtree: &Subtree, attrs: Option<&Subtree>)
         -> Result<Subtree, ExpansionError>;
 }