dodo_bindgen_codegen/
util.rs1use std::cell::RefCell;
2
3use proc_macro2::Ident;
4pub use quote::{ToTokens, format_ident, quote};
5
6thread_local! {
7 static RAW: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
8}
9
10pub fn raw(text: impl Into<String>) -> Ident {
11 RAW.with_borrow_mut(|raw| {
12 raw.push(text.into());
13 format_ident!("__dodo_raw_{}__", raw.len() - 1)
14 })
15}
16
17pub fn expand_raw(mut source: String) -> String {
18 RAW.with_borrow(|raw| {
19 for (i, text) in raw.iter().enumerate().rev() {
20 let placeholder = format!("__dodo_raw_{i}__");
21 if source.contains(&placeholder) {
22 source = source.replace(&placeholder, text);
23 }
24 }
25 });
26 source
27}
28
29#[cfg(test)]
30pub fn deno_fmt(source: String) -> String {
31 use std::io::Write;
32 use std::process::{Command, Stdio};
33
34 let mut child = Command::new("deno")
35 .args(["fmt", "--ext", "ts", "-"])
36 .stdin(Stdio::piped())
37 .stdout(Stdio::piped())
38 .stderr(Stdio::piped())
39 .spawn()
40 .expect("failed to spawn `deno fmt`, is deno on PATH?");
41 child
42 .stdin
43 .take()
44 .unwrap()
45 .write_all(expand_raw(source).as_bytes())
46 .unwrap();
47 let output = child.wait_with_output().unwrap();
48 assert!(
49 output.status.success(),
50 "deno fmt failed:\n{}",
51 String::from_utf8_lossy(&output.stderr)
52 );
53 String::from_utf8(output.stdout).unwrap()
54}