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