Struct parsel::util::TokenStreamFormatter
source · [−]pub struct TokenStreamFormatter<S, W> { /* private fields */ }
Expand description
Helper type for correctly and reasonably “pretty”-printing any TokenStream
in
a grammar- and language-agnostic way. This mostly means dealing with parentheses,
so that nested structures don’t end up on one single long line.
Of course, it is not possible to perform pretty-printing in a completely generic
manner, but the primary purpose of this mechanism is not that – it’s merely trying
to be a useful debugging tool, of which the results are less unnecessarily verbose,
and therefore easier to read, than the output of #[derive(Debug)]
.
use parsel::util::TokenStreamFormatter;
use parsel::quote::quote;
let ts = quote!{
[
[
7.43 * {
zzz (
3333 + "52" - 'a / [
foo bar || &baz;
]
) != 5;
ww;
6 <<= 78 >>= 951,
$ foo $bar #![attribute]
},
x, y
]
]
};
let mut string = String::new();
let mut fmt = TokenStreamFormatter::new(&mut string);
fmt.write(ts)?;
assert_eq!(string, str::trim(r#"
[
[
7.43 * {
zzz (
3333 + "52" - 'a / [
foo bar || & baz ;
]
)
!= 5 ;
ww ;
6 <<= 78 >>= 951 ,
$ foo $ bar # ! [
attribute
]
}
,
x ,
y
]
]
"#));
Implementations
sourceimpl<S, W> TokenStreamFormatter<S, W> where
S: AsRef<str>,
impl<S, W> TokenStreamFormatter<S, W> where
S: AsRef<str>,
sourcepub fn with_indent(indent_string: S, writer: W) -> Result<Self>
pub fn with_indent(indent_string: S, writer: W) -> Result<Self>
Constructor for indenting with arbitrary whitespace.
This returns an error when non-whitespace characters are present in the indentation.
let ok = TokenStreamFormatter::with_indent(" \t", Cursor::new(&[] as &[u8]));
assert!(ok.is_ok());
let err = TokenStreamFormatter::with_indent(" not ws ", Cursor::new(&[] as &[u8]));
assert!(err.is_err());
sourceimpl<S, W> TokenStreamFormatter<S, W> where
S: AsRef<str>,
W: Write,
impl<S, W> TokenStreamFormatter<S, W> where
S: AsRef<str>,
W: Write,
pub fn write(&mut self, stream: TokenStream) -> Result
pub fn write_node<T>(&mut self, node: &T) -> Result where
T: ?Sized + ToTokens,
sourceimpl<W> TokenStreamFormatter<&'static str, W>
impl<W> TokenStreamFormatter<&'static str, W>
Trait Implementations
sourceimpl<S: Clone, W: Clone> Clone for TokenStreamFormatter<S, W>
impl<S: Clone, W: Clone> Clone for TokenStreamFormatter<S, W>
sourcefn clone(&self) -> TokenStreamFormatter<S, W>
fn clone(&self) -> TokenStreamFormatter<S, W>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<S: Debug, W: Debug> Debug for TokenStreamFormatter<S, W>
impl<S: Debug, W: Debug> Debug for TokenStreamFormatter<S, W>
sourceimpl<S: Hash, W: Hash> Hash for TokenStreamFormatter<S, W>
impl<S: Hash, W: Hash> Hash for TokenStreamFormatter<S, W>
sourceimpl<S: PartialEq, W: PartialEq> PartialEq<TokenStreamFormatter<S, W>> for TokenStreamFormatter<S, W>
impl<S: PartialEq, W: PartialEq> PartialEq<TokenStreamFormatter<S, W>> for TokenStreamFormatter<S, W>
sourcefn eq(&self, other: &TokenStreamFormatter<S, W>) -> bool
fn eq(&self, other: &TokenStreamFormatter<S, W>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &TokenStreamFormatter<S, W>) -> bool
fn ne(&self, other: &TokenStreamFormatter<S, W>) -> bool
This method tests for !=
.
impl<S: Copy, W: Copy> Copy for TokenStreamFormatter<S, W>
impl<S: Eq, W: Eq> Eq for TokenStreamFormatter<S, W>
impl<S, W> StructuralEq for TokenStreamFormatter<S, W>
impl<S, W> StructuralPartialEq for TokenStreamFormatter<S, W>
Auto Trait Implementations
impl<S, W> RefUnwindSafe for TokenStreamFormatter<S, W> where
S: RefUnwindSafe,
W: RefUnwindSafe,
impl<S, W> Send for TokenStreamFormatter<S, W> where
S: Send,
W: Send,
impl<S, W> Sync for TokenStreamFormatter<S, W> where
S: Sync,
W: Sync,
impl<S, W> Unpin for TokenStreamFormatter<S, W> where
S: Unpin,
W: Unpin,
impl<S, W> UnwindSafe for TokenStreamFormatter<S, W> where
S: UnwindSafe,
W: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more