miniextendr_macros/miniextendr_impl/
env_class.rs1use super::ParsedImpl;
12
13pub fn generate_env_class(parsed_impl: &ParsedImpl) -> super::ClassWrapper {
46 use crate::r_class_formatter::{
47 ClassDocBuilder, MethodDocBuilder, ParsedImplExt, should_export_from_tags,
48 };
49
50 let class_name = parsed_impl.class_name();
51 let type_ident = &parsed_impl.type_ident;
52 let class_has_no_rd = crate::roxygen::has_roxygen_tag(&parsed_impl.doc_tags, "noRd")
56 || (parsed_impl.noexport && !parsed_impl.internal);
57
58 let mut w = super::ClassWrapper::default();
59 let mut lines = Vec::new();
60
61 let class_docs = ClassDocBuilder::new(&class_name, type_ident, &parsed_impl.doc_tags, "")
63 .with_export_control(parsed_impl.internal, parsed_impl.noexport);
64 lines.extend(class_docs.build());
65 w.class_docs = class_docs.docs();
66 w.mark_class_doc_lines(&mut lines, parsed_impl);
67 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} <- new.env(parent = emptyenv())",
class_name))
})format!("{} <- new.env(parent = emptyenv())", class_name));
68 lines.push(String::new());
69
70 if let Some(ctx) = parsed_impl.constructor_context() {
72 w.member_names.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}$new`", class_name))
})format!("`{class_name}$new`"));
73 lines.push(ctx.source_comment(type_ident));
74 if !class_has_no_rd {
76 let choice_docs = ctx.choice_param_docs();
77 let method_doc =
78 MethodDocBuilder::new(&class_name, "new", type_ident, &ctx.method.doc_tags)
79 .with_source_method(&ctx.method.ident)
80 .with_name_prefix("$")
81 .with_params_as_details()
82 .with_r_params(&ctx.params)
83 .with_choice_param_docs(&choice_docs);
84 lines.extend(method_doc.build());
85 }
86 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}$new <- function({1}) {{",
class_name, ctx.params))
})format!("{}$new <- function({}) {{", class_name, ctx.params));
87 for check in ctx.precondition_checks() {
88 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", check))
})format!(" {}", check));
89 }
90 for line in ctx.match_arg_prelude() {
92 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", line))
})format!(" {}", line));
93 }
94 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" .val <- {0}", ctx.static_call()))
})format!(" .val <- {}", ctx.static_call()));
95 lines.extend(crate::method_return_builder::condition_check_lines(" "));
96 lines.push(" self <- .val".to_string());
97 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" class(self) <- \"{0}\"",
class_name))
})format!(" class(self) <- \"{}\"", class_name));
98 lines.push(" self".to_string());
99 lines.push("}".to_string());
100 lines.push(String::new());
101 }
102 w.head = std::mem::take(&mut lines);
103
104 for ctx in parsed_impl.instance_method_contexts() {
106 let method_name = ctx.method.r_method_name();
107 w.member_names.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}${1}`", class_name,
method_name))
})format!("`{class_name}${method_name}`"));
108 lines.push(ctx.source_comment(type_ident));
109 if !class_has_no_rd {
111 let choice_docs = ctx.choice_param_docs();
112 let method_doc =
113 MethodDocBuilder::new(&class_name, &method_name, type_ident, &ctx.method.doc_tags)
114 .with_source_method(&ctx.method.ident)
115 .with_name_prefix("$")
116 .with_params_as_details()
117 .with_r_params(&ctx.params)
118 .with_choice_param_docs(&choice_docs);
119 lines.extend(method_doc.build());
120 }
121
122 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}${1} <- function({2}) {{",
class_name, crate::naming::r_def_name(&method_name),
ctx.params))
})format!(
124 "{}${} <- function({}) {{",
125 class_name,
126 crate::naming::r_def_name(&method_name),
127 ctx.params
128 ));
129
130 let what = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}${1}", class_name, method_name))
})format!("{}${}", class_name, method_name);
131 ctx.emit_method_prelude(&mut lines, " ", &what, &ctx.params);
132
133 let call = ctx.instance_call(".mx_self");
134 let strategy = crate::ReturnStrategy::for_method(ctx.method);
135 let return_builder = crate::MethodReturnBuilder::new(call)
136 .with_strategy(strategy)
137 .with_invisible(ctx.method.is_invisible())
138 .with_chain_var(".mx_self".to_string())
139 .with_class_name(class_name.clone())
140 .with_return_class_from_method(ctx.method, &type_ident.to_string());
141 lines.extend(return_builder.build());
142
143 lines.push("}".to_string());
144 lines.push(String::new());
145 }
146
147 for ctx in parsed_impl.static_method_contexts() {
149 let method_name = ctx.method.r_method_name();
150 w.member_names.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}${1}`", class_name,
method_name))
})format!("`{class_name}${method_name}`"));
151 lines.push(ctx.source_comment(type_ident));
152 if !class_has_no_rd {
154 let choice_docs = ctx.choice_param_docs();
155 let doc_tags = parsed_impl.static_doc_tags(
158 ctx.method,
159 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}${1}()", class_name,
method_name))
})format!("{class_name}${method_name}()"),
160 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}$new()", class_name))
})format!("{class_name}$new()"),
161 false,
162 );
163 let method_doc =
164 MethodDocBuilder::new(&class_name, &method_name, type_ident, &doc_tags)
165 .with_source_method(&ctx.method.ident)
166 .with_name_prefix("$")
167 .with_params_as_details()
168 .with_r_params(&ctx.params)
169 .with_choice_param_docs(&choice_docs);
170 lines.extend(method_doc.build());
171 }
172
173 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}${1} <- function({2}) {{",
class_name, crate::naming::r_def_name(&method_name),
ctx.params))
})format!(
174 "{}${} <- function({}) {{",
175 class_name,
176 crate::naming::r_def_name(&method_name),
177 ctx.params
178 ));
179
180 let what = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}${1}", class_name, method_name))
})format!("{}${}", class_name, method_name);
181 ctx.emit_method_prelude(&mut lines, " ", &what, &ctx.params);
182
183 let strategy = crate::ReturnStrategy::for_method(ctx.method);
184 let return_builder = crate::MethodReturnBuilder::new(ctx.static_call())
185 .with_strategy(strategy)
186 .with_invisible(ctx.method.is_invisible())
187 .with_class_name(class_name.clone())
188 .with_return_class_from_method(ctx.method, &type_ident.to_string());
189 lines.extend(return_builder.build());
190
191 lines.push("}".to_string());
192 lines.push(String::new());
193 }
194 w.members = std::mem::take(&mut lines);
195
196 let should_export = should_export_from_tags(
199 &parsed_impl.doc_tags,
200 parsed_impl.noexport || parsed_impl.internal,
201 );
202
203 let fields = parsed_impl.r_data_accessors.enabled();
214 if fields {
215 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("# Field syntax for the `#[r_data]` fields of {0} (`env(r_data_accessors)`).",
type_ident))
})format!(
216 "# Field syntax for the `#[r_data]` fields of {type_ident} (`env(r_data_accessors)`)."
217 ));
218 lines.push(super::field_syntax::env_load_check(parsed_impl));
219 lines.push(String::new());
220 }
221 if class_has_no_rd {
222 lines.push("#' @noRd".to_string());
223 lines.push("#' @export".to_string());
224 } else if !should_export {
225 lines.push("#' @export".to_string());
226 } else {
227 lines.extend(super::field_syntax::page_lines(
229 parsed_impl,
230 &[
231 ("self", "The object instance."),
232 (
233 "name",
234 if fields {
235 super::field_syntax::env_name_param(parsed_impl)
236 } else {
237 "Method name for dispatch."
238 },
239 ),
240 ],
241 ));
242 lines.push("#' @export".to_string());
243 }
244 let mut lookup = Vec::new();
248 if fields {
249 lookup.extend(super::field_syntax::env_field_branch(parsed_impl));
250 }
251 let method_lookup = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(".miniextendr_env_method(self, name, {0})",
class_name))
})format!(".miniextendr_env_method(self, name, {class_name})");
252 if fields {
253 lookup.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" obj <- {0}", method_lookup))
})format!(" obj <- {method_lookup}"));
254 lookup.push(" if (is.null(obj)) {".to_string());
255 lookup.extend(super::field_syntax::env_unknown_name_check(parsed_impl));
256 lookup.push(" }".to_string());
257 lookup.push(" obj".to_string());
258 } else {
259 lookup.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", method_lookup))
})format!(" {method_lookup}"));
260 }
261 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`$.{0}` <- function(self, name) {{",
class_name))
})format!("`$.{}` <- function(self, name) {{", class_name));
262 lines.extend(lookup.iter().cloned());
263 lines.push("}".to_string());
264 if class_has_no_rd {
268 lines.push("#' @noRd".to_string());
269 lines.push("#' @export".to_string());
270 } else if !should_export {
271 lines.push("#' @export".to_string());
272 } else {
273 lines.extend(super::field_syntax::page_lines(
274 parsed_impl,
275 &[
276 ("...", "Refused: `[[` takes one name."),
277 ("exact", "Ignored: names match exactly."),
278 ],
279 ));
280 lines.push("#' @export".to_string());
281 }
282 lines.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`[[.{1}` <- function({0}) {{",
super::field_syntax::ENV_SUBSET_FORMALS, class_name))
})format!(
283 "`[[.{class_name}` <- function({}) {{",
284 super::field_syntax::ENV_SUBSET_FORMALS
285 ));
286 lines.push(super::field_syntax::env_subset_dots_check(parsed_impl));
287 lines.extend(lookup);
288 lines.push("}".to_string());
289 if fields {
290 lines.extend(super::field_syntax::env_dollar_names(
291 parsed_impl,
292 !class_has_no_rd && should_export,
293 ));
294 }
295 if fields && parsed_impl.r_data_accessors.writers() {
296 lines.extend(super::field_syntax::env_replacement_methods(
297 parsed_impl,
298 !class_has_no_rd && should_export,
299 ));
300 }
301
302 w.tail = lines;
303 w
304}
305
306#[cfg(test)]
308pub fn generate_env_r_wrapper(parsed_impl: &ParsedImpl) -> String {
309 generate_env_class(parsed_impl).render()
310}