reactive_graph_generator_codegen/targets/
rust.rs1use crate::CodeFormatter;
2use crate::CodeFormatterError;
3use crate::CodeGenerationConfig;
4use crate::CodeGenerationTarget;
5use proc_macro2::TokenStream;
6use quote::quote;
7use semver::VersionReq;
8use syn::{self};
9
10pub struct Rust {}
11
12impl Rust {
13 pub fn multiline_doc_comment<S: Into<String>>(comment: S) -> TokenStream {
14 let mut doc_comments = vec![];
15 for line in comment.into().split("\n") {
16 let mut line = line.trim().to_owned();
17 line.insert_str(0, " ");
18 doc_comments.push(quote! {
19 #[doc = #line]
20 });
21 }
22 quote! {
23 #(#doc_comments)*
24 }
25 }
26}
27impl CodeGenerationTarget for Rust {
28 fn name() -> String {
29 "rust".to_string()
30 }
31
32 fn version() -> VersionReq {
33 VersionReq::parse(">=1.80").unwrap()
34 }
35
36 fn extension() -> String {
37 "rs".to_string()
38 }
39}
40
41impl CodeFormatter for Rust {
42 fn format(unformatted: String, config: &CodeGenerationConfig) -> Result<String, CodeFormatterError> {
43 if config.formatting {
44 let file = match syn::parse_file(&unformatted) {
45 Ok(file) => file,
46 Err(e) => {
47 if config.ignore_formatter_errors {
48 return Ok(unformatted.clone());
49 }
50 return Err(CodeFormatterError::ParserError(e.to_string(), unformatted));
51 }
52 };
53 let formatted = prettyplease::unparse(&file);
54 let formatted = formatted.replace("#[doc(newline)]\n", "\n");
55 Ok(formatted)
56 } else {
57 Ok(unformatted)
58 }
59 }
60}