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
36/// # Panics
37///
38/// Panics if `pat.kind` is `PatKind::Missing`.
39pub fn pat_to_string(pat: &ast::Pat) -> String {
40    State::new().pat_to_string(pat)
41}
42
43pub fn expr_to_string(e: &ast::Expr) -> String {
44    State::new().expr_to_string(e)
45}
46
47pub fn tt_to_string(tt: &TokenTree) -> String {
48    State::new().tt_to_string(tt)
49}
50
51pub fn tts_to_string(tokens: &TokenStream) -> String {
52    State::new().tts_to_string(tokens)
53}
54
55pub fn item_to_string(i: &ast::Item) -> String {
56    State::new().item_to_string(i)
57}
58
59pub fn assoc_item_to_string(i: &ast::AssocItem) -> String {
60    State::new().assoc_item_to_string(i)
61}
62
63pub fn foreign_item_to_string(i: &ast::ForeignItem) -> String {
64    State::new().foreign_item_to_string(i)
65}
66
67pub fn stmt_to_string(s: &ast::Stmt) -> String {
68    State::new().stmt_to_string(s)
69}
70
71pub fn path_to_string(p: &ast::Path) -> String {
72    State::new().path_to_string(p)
73}
74
75pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
76    State::new().path_segment_to_string(p)
77}
78
79pub fn vis_to_string(v: &ast::Visibility) -> String {
80    State::new().vis_to_string(v)
81}
82
83pub fn meta_list_item_to_string(li: &ast::MetaItemInner) -> String {
84    State::new().meta_list_item_to_string(li)
85}
86
87pub fn attribute_to_string(attr: &ast::Attribute) -> String {
88    State::new().attribute_to_string(attr)
89}
90
91pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
92    State::to_string(f)
93}
94
95pub fn crate_to_string_for_macros(krate: &ast::Crate) -> String {
96    State::to_string(|s| {
97        s.print_inner_attributes(&krate.attrs);
98        for item in &krate.items {
99            s.print_item(item);
100        }
101    })
102}