Debian Codegraph @

src/codegen/signal.rs

raw
1 use std::io::{Result, Write};
2
3 use super::{
4 general::{cfg_deprecated, doc_alias, doc_hidden, version_condition},
5 signal_body,
6 trampoline::{self, func_string},
7 };
8 use crate::{
9 analysis,
10 chunk::Chunk,
11 env::Env,
12 writer::{ToCode, primitives::tabs},
13 };
14
15 pub fn generate(
16 w: &mut dyn Write,
17 env: &Env,
18 analysis: &analysis::signals::Info,
19 in_trait: bool,
20 only_declaration: bool,
21 indent: usize,
22 ) -> Result<()> {
23 let commented = analysis.trampoline.is_err();
24 let comment_prefix = if commented { "//" } else { "" };
25 let pub_prefix = if in_trait { "" } else { "pub " };
26
27 let function_type = function_type_string(env, analysis, true);
28 let declaration = declaration(analysis, &function_type);
29 let suffix = if only_declaration { ";" } else { " {" };
30
31 writeln!(w)?;
32 cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;
33 version_condition(w, env, None, analysis.version, commented, indent)?;
34 doc_hidden(w, analysis.doc_hidden, comment_prefix, indent)?;
35 // Strip the "prefix" from "prefix::prop-name", if any.
36 // Ex.: "notify::is-locked".
37 doc_alias(
38 w,
39 analysis.signal_name.splitn(2, "::").last().unwrap(),
40 comment_prefix,
41 indent,
42 )?;
43 writeln!(
44 w,
45 "{}{}{}{}{}",
46 tabs(indent),
47 comment_prefix,
48 pub_prefix,
49 declaration,
50 suffix
51 )?;
52
53 if !only_declaration {
54 if !commented && let Ok(ref trampoline) = analysis.trampoline {
55 trampoline::generate(w, env, trampoline, in_trait, 2)?;
56 }
57 match function_type {
58 Some(_) => {
59 let body = body(analysis, in_trait).to_code(env);
60 for s in body {
61 writeln!(w, "{}{}", tabs(indent), s)?;
62 }
63 }
64 _ => {
65 if let Err(ref errors) = analysis.trampoline {
66 for error in errors {
67 writeln!(w, "{}{}\t{}", tabs(indent), comment_prefix, error)?;
68 }
69 writeln!(w, "{}{}}}", tabs(indent), comment_prefix)?;
70 } else {
71 writeln!(
72 w,
73 "{}{}\tTODO: connect to trampoline\n{0}{1}}}",
74 tabs(indent),
75 comment_prefix
76 )?;
77 }
78 }
79 }
80 }
81
82 if function_type.is_none() {
83 // Signal incomplete, can't generate emit
84 return Ok(());
85 }
86
87 if let Some(ref emit_name) = analysis.action_emit_name {
88 writeln!(w)?;
89 if !in_trait || only_declaration {
90 cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;
91 }
92 version_condition(w, env, None, analysis.version, commented, indent)?;
93
94 let function_type = function_type_string(env, analysis, false);
95
96 writeln!(
97 w,
98 "{}{}{}fn {}{}{}",
99 tabs(indent),
100 comment_prefix,
101 pub_prefix,
102 emit_name,
103 function_type.unwrap(),
104 suffix
105 )?;
106
107 if !only_declaration {
108 let trampoline = analysis.trampoline.as_ref().unwrap_or_else(|_| {
109 panic!(
110 "Internal error: can't find trampoline for signal '{}'",
111 analysis.signal_name,
112 )
113 });
114 let mut args = String::with_capacity(100);
115
116 for (pos, par) in trampoline.parameters.rust_parameters.iter().enumerate() {
117 // Skip the self parameter
118 if pos == 0 {
119 continue;
120 }
121
122 if pos > 1 {
123 args.push_str(", ");
124 }
125 args.push('&');
126 args.push_str(&par.name);
127 }
128
129 if trampoline.ret.typ != Default::default() {
130 writeln!(
131 w,
132 "{}self.emit_by_name(\"{}\", &[{}])",
133 tabs(indent + 1),
134 analysis.signal_name,
135 args,
136 )?;
137 } else {
138 writeln!(
139 w,
140 "{}self.emit_by_name::<()>(\"{}\", &[{}]);",
141 tabs(indent + 1),
142 analysis.signal_name,
143 args,
144 )?;
145 }
146 writeln!(w, "{}}}", tabs(indent))?;
147 }
148 }
149
150 Ok(())
151 }
152
153 fn function_type_string(
154 env: &Env,
155 analysis: &analysis::signals::Info,
156 closure: bool,
157 ) -> Option<String> {
158 analysis.trampoline.as_ref().ok()?;
159
160 let trampoline = analysis.trampoline.as_ref().unwrap_or_else(|_| {
161 panic!(
162 "Internal error: can't find trampoline for signal '{}'",
163 analysis.signal_name
164 )
165 });
166
167 let type_ = func_string(
168 env,
169 trampoline,
170 Some(if closure { "Self" } else { "self" }),
171 closure,
172 );
173 Some(type_)
174 }
175
176 fn declaration(analysis: &analysis::signals::Info, function_type: &Option<String>) -> String {
177 let bounds = bounds(function_type);
178 let param_str = if !analysis.is_detailed {
179 "&self, f: F"
180 } else {
181 "&self, detail: Option<&str>, f: F"
182 };
183 let return_str = " -> SignalHandlerId";
184 format!(
185 "fn {}<{}>({}){}",
186 analysis.connect_name, bounds, param_str, return_str
187 )
188 }
189
190 fn bounds(function_type: &Option<String>) -> String {
191 match function_type {
192 Some(type_) => format!("F: {type_}"),
193 _ => "Unsupported or ignored types".to_owned(),
194 }
195 }
196
197 fn body(analysis: &analysis::signals::Info, in_trait: bool) -> Chunk {
198 let mut builder = signal_body::Builder::new();
199
200 builder
201 .signal_name(&analysis.signal_name)
202 .trampoline_name(&analysis.trampoline.as_ref().unwrap().name)
203 .in_trait(in_trait)
204 .is_detailed(analysis.is_detailed);
205
206 builder.generate()
207 }