Debian Codegraph @

src/codegen/properties.rs

raw
1 use std::io::{Result, Write};
2
3 use super::{
4 general::{cfg_condition, cfg_deprecated, doc_alias, version_condition},
5 property_body,
6 };
7 use crate::{
8 analysis::{properties::Property, rust_type::RustType},
9 chunk::Chunk,
10 env::Env,
11 library,
12 traits::IntoString,
13 writer::{ToCode, primitives::tabs},
14 };
15
16 pub fn generate(
17 w: &mut dyn Write,
18 env: &Env,
19 prop: &Property,
20 in_trait: bool,
21 only_declaration: bool,
22 indent: usize,
23 ) -> Result<()> {
24 generate_prop_func(w, env, prop, in_trait, only_declaration, indent)?;
25
26 Ok(())
27 }
28
29 fn generate_prop_func(
30 w: &mut dyn Write,
31 env: &Env,
32 prop: &Property,
33 in_trait: bool,
34 only_declaration: bool,
35 indent: usize,
36 ) -> Result<()> {
37 let pub_prefix = if in_trait { "" } else { "pub " };
38 let decl_suffix = if only_declaration { ";" } else { " {" };
39 let commented = RustType::try_new(env, prop.typ).is_err();
40 let comment_prefix = if commented { "//" } else { "" };
41
42 writeln!(w)?;
43
44 let decl = declaration(env, prop);
45 cfg_deprecated(w, env, None, prop.deprecated_version, commented, indent)?;
46 cfg_condition(w, prop.cfg_condition.as_ref(), commented, indent)?;
47 version_condition(w, env, None, prop.version, commented, indent)?;
48 let add_doc_alias = if let Some(func_name_alias) = prop.func_name_alias.as_ref() {
49 &prop.name != func_name_alias && prop.name != prop.var_name
50 } else {
51 prop.name != prop.var_name
52 };
53 if add_doc_alias {
54 doc_alias(w, &prop.name, comment_prefix, indent)?;
55 }
56 writeln!(
57 w,
58 "{}{}{}{}{}",
59 tabs(indent),
60 comment_prefix,
61 pub_prefix,
62 decl,
63 decl_suffix
64 )?;
65
66 if !only_declaration {
67 let body = body(env, prop, in_trait).to_code(env);
68 for s in body {
69 writeln!(w, "{}{}{}", tabs(indent), comment_prefix, s)?;
70 }
71 }
72
73 Ok(())
74 }
75
76 fn declaration(env: &Env, prop: &Property) -> String {
77 let bound: String;
78 let set_param = if prop.is_get {
79 bound = String::new();
80 String::new()
81 } else if let Some(ref set_bound) = prop.set_bound {
82 bound = format!("<{}: IsA<{}>>", set_bound.alias, set_bound.type_str);
83 format!(", {}: Option<&{}>", prop.var_name, set_bound.alias)
84 } else {
85 bound = String::new();
86 let dir = library::ParameterDirection::In;
87 let param_type = RustType::builder(env, prop.typ)
88 .direction(dir)
89 .nullable(prop.nullable)
90 .ref_mode(prop.set_in_ref_mode)
91 .try_build_param()
92 .into_string();
93 format!(", {}: {}", prop.var_name, param_type)
94 };
95 let return_str = if prop.is_get {
96 let dir = library::ParameterDirection::Return;
97 let ret_type = RustType::builder(env, prop.typ)
98 .direction(dir)
99 .nullable(prop.nullable)
100 .ref_mode(prop.get_out_ref_mode)
101 .try_build_param()
102 .into_string();
103 format!(" -> {ret_type}")
104 } else {
105 String::new()
106 };
107 format!(
108 "fn {}{}(&self{}){}",
109 prop.func_name, bound, set_param, return_str
110 )
111 }
112
113 fn body(env: &Env, prop: &Property, in_trait: bool) -> Chunk {
114 let mut builder = property_body::Builder::new(env);
115 builder
116 .name(&prop.name)
117 .in_trait(in_trait)
118 .var_name(&prop.var_name)
119 .is_get(prop.is_get);
120
121 if let Ok(type_) = RustType::try_new(env, prop.typ) {
122 builder.type_(type_.as_str());
123 } else {
124 builder.type_("/*Unknown type*/");
125 }
126
127 builder.generate()
128 }