Improve Debug representations
diff --git a/src/lib.rs b/src/lib.rs
index 7b9a919..21becec 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -251,7 +251,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub enum TokenTree {
Group(Group),
Term(Term),
@@ -314,7 +314,20 @@
}
}
-#[derive(Clone, Debug)]
+impl fmt::Debug for TokenTree {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ // Each of these has the name in the struct type in the derived debug,
+ // so don't bother with an extra layer of indirection
+ match *self {
+ TokenTree::Group(ref t) => t.fmt(f),
+ TokenTree::Term(ref t) => t.fmt(f),
+ TokenTree::Op(ref t) => t.fmt(f),
+ TokenTree::Literal(ref t) => t.fmt(f),
+ }
+ }
+}
+
+#[derive(Clone)]
pub struct Group {
delimiter: Delimiter,
stream: TokenStream,
@@ -361,7 +374,18 @@
}
}
-#[derive(Copy, Clone, Debug)]
+impl fmt::Debug for Group {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ let mut debug = fmt.debug_struct("Group");
+ debug.field("delimiter", &self.delimiter);
+ debug.field("stream", &self.stream);
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
+ }
+}
+
+#[derive(Copy, Clone)]
pub struct Op {
op: char,
spacing: Spacing,
@@ -406,6 +430,17 @@
}
}
+impl fmt::Debug for Op {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ let mut debug = fmt.debug_struct("Op");
+ debug.field("op", &self.op);
+ debug.field("spacing", &self.spacing);
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
+ }
+}
+
#[derive(Copy, Clone)]
pub struct Term {
inner: imp::Term,
diff --git a/src/stable.rs b/src/stable.rs
index ad9870c..1ca48a5 100644
--- a/src/stable.rs
+++ b/src/stable.rs
@@ -16,7 +16,7 @@
use {Delimiter, Group, Op, Spacing, TokenTree};
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub struct TokenStream {
inner: Vec<TokenTree>,
}
@@ -111,6 +111,13 @@
}
}
+impl fmt::Debug for TokenStream {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("TokenStream ")?;
+ f.debug_list().entries(self.clone()).finish()
+ }
+}
+
#[cfg(feature = "proc-macro")]
impl From<::proc_macro::TokenStream> for TokenStream {
fn from(inner: ::proc_macro::TokenStream) -> TokenStream {
@@ -314,7 +321,7 @@
}
}
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span {
#[cfg(procmacro2_semver_exempt)]
lo: u32,
@@ -393,6 +400,16 @@
}
}
+impl fmt::Debug for Span {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ #[cfg(procmacro2_semver_exempt)]
+ return write!(f, "bytes({}..{})", self.lo, self.hi);
+
+ #[cfg(not(procmacro2_semver_exempt))]
+ write!(f, "Span")
+ }
+}
+
#[derive(Copy, Clone)]
pub struct Term {
intern: usize,
@@ -466,7 +483,11 @@
impl fmt::Debug for Term {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- f.debug_tuple("Term").field(&self.as_str()).finish()
+ let mut debug = f.debug_struct("Term");
+ debug.field("sym", &format_args!("{}", self.as_str()));
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
}
}
@@ -508,7 +529,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub struct Literal {
text: String,
span: Span,
@@ -629,6 +650,16 @@
}
}
+impl fmt::Debug for Literal {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ let mut debug = fmt.debug_struct("Literal");
+ debug.field("lit", &format_args!("{}", self.text));
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
+ }
+}
+
fn token_stream(mut input: Cursor) -> PResult<::TokenStream> {
let mut trees = Vec::new();
loop {
diff --git a/tests/test.rs b/tests/test.rs
index caed47c..27429ee 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -302,3 +302,56 @@
}
assert!(tts.next().is_none());
}
+
+#[test]
+fn test_debug() {
+ let tts = TokenStream::from_str("[a + 1]").unwrap();
+
+ #[cfg(not(procmacro2_semver_exempt))]
+ let expected = "\
+TokenStream [
+ Group {
+ delimiter: Bracket,
+ stream: TokenStream [
+ Term {
+ sym: a
+ },
+ Op {
+ op: '+',
+ spacing: Alone
+ },
+ Literal {
+ lit: 1
+ }
+ ]
+ }
+]\
+ ";
+
+ #[cfg(procmacro2_semver_exempt)]
+ let expected = "\
+TokenStream [
+ Group {
+ delimiter: Bracket,
+ stream: TokenStream [
+ Term {
+ sym: a,
+ span: bytes(2..3)
+ },
+ Op {
+ op: '+',
+ spacing: Alone,
+ span: bytes(4..5)
+ },
+ Literal {
+ lit: 1,
+ span: bytes(6..7)
+ }
+ ],
+ span: bytes(1..8)
+ }
+]\
+ ";
+
+ assert_eq!(expected, format!("{:#?}", tts));
+}