kproc_parser/rust/
fmt.rs

1//! formatting module that contains the basic
2//! fmt function that convert in a string
3//! part of the rust syntax.
4use super::ast_nodes::{GenericParams, TyToken};
5use crate::warn;
6
7pub(crate) fn fmt_generics(generics: &GenericParams) -> String {
8    if generics.params.is_empty() {
9        return String::new();
10    }
11    let mut buff = "<".to_owned();
12    for generic in &generics.params {
13        buff += &format!("{generic}");
14    }
15
16    buff += ">";
17    buff
18}
19
20pub fn fmt_ty(ty: &TyToken) -> String {
21    let mut prefix = String::new();
22    if let Some(refer) = &ty.ref_tok {
23        prefix += &refer.to_string();
24    }
25
26    if let Some(mut_tok) = &ty.mut_tok {
27        prefix += &mut_tok.to_string();
28    }
29
30    if let Some(dyn_tok) = &ty.dyn_tok {
31        prefix += &dyn_tok.to_string();
32    }
33
34    let mut postfix = String::new();
35
36    // FIXME: the lifetime possible here is only one?
37    if let Some(lifetime) = &ty.lifetime {
38        postfix += &format!("'{lifetime}, ");
39    }
40
41    if let Some(generics) = &ty.generics {
42        postfix += "<";
43        postfix += &generics
44            .iter()
45            .map(|it| it.to_string())
46            .collect::<Vec<String>>()
47            .join(",");
48        postfix += ">";
49    } else {
50        let ident = ty.identifier.clone();
51        warn!(
52            ["Vec"].contains(&ident.to_string().as_str()),
53            ident, "the token required generics"
54        );
55    }
56
57    format!("{prefix} {}{postfix}", ty.identifier)
58}