rustc_ast_pretty/pprust/
mod.rs

1#[cfg(test)]
2mod tests;
3
4pub mod state;
5use std::borrow::Cow;
6
7use rustc_ast as ast;
8use rustc_ast::token::{Nonterminal, Token, TokenKind};
9use rustc_ast::tokenstream::{TokenStream, TokenTree};
10pub use state::{AnnNode, Comments, PpAnn, PrintState, State, print_crate};
11
12pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
13    State::new().nonterminal_to_string(nt)
14}
15
16/// Print the token kind precisely, without converting `$crate` into its respective crate name.
17pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> {
18    State::new().token_kind_to_string(tok)
19}
20
21/// Print the token precisely, without converting `$crate` into its respective crate name.
22pub fn token_to_string(token: &Token) -> Cow<'static, str> {
23    State::new().token_to_string(token)
24}
25
26pub fn ty_to_string(ty: &ast::Ty) -> String {
27    State::new().ty_to_string(ty)
28}
29
30pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String {
31    State::new().bounds_to_string(bounds)
32}
33
34pub fn where_bound_predicate_to_string(where_bound_predicate: &ast::WhereBoundPredicate) -> String {
35    State::new().where_bound_predicate_to_string(where_bound_predicate)
36}
37
38pub fn pat_to_string(pat: &ast::Pat) -> String {
39    State::new().pat_to_string(pat)
40}
41
42pub fn expr_to_string(e: &ast::Expr) -> String {
43    State::new().expr_to_string(e)
44}
45
46pub fn tt_to_string(tt: &TokenTree) -> String {
47    State::new().tt_to_string(tt)
48}
49
50pub fn tts_to_string(tokens: &TokenStream) -> String {
51    State::new().tts_to_string(tokens)
52}
53
54pub fn item_to_string(i: &ast::Item) -> String {
55    State::new().item_to_string(i)
56}
57
58pub fn path_to_string(p: &ast::Path) -> String {
59    State::new().path_to_string(p)
60}
61
62pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
63    State::new().path_segment_to_string(p)
64}
65
66pub fn vis_to_string(v: &ast::Visibility) -> String {
67    State::new().vis_to_string(v)
68}
69
70pub fn meta_list_item_to_string(li: &ast::MetaItemInner) -> String {
71    State::new().meta_list_item_to_string(li)
72}
73
74pub fn attribute_to_string(attr: &ast::Attribute) -> String {
75    State::new().attribute_to_string(attr)
76}
77
78pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
79    State::to_string(f)
80}
81
82pub fn crate_to_string_for_macros(krate: &ast::Crate) -> String {
83    State::to_string(|s| {
84        s.print_inner_attributes(&krate.attrs);
85        for item in &krate.items {
86            s.print_item(item);
87        }
88    })
89}