nodarium_macros/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
extern crate proc_macro;
use nodarium_types::NodeDefinition;
use proc_macro::TokenStream;
use quote::quote;
use std::env;
use std::fs;
use std::path::Path;
use syn::{parse_macro_input, LitStr};

#[proc_macro]
pub fn node_definition(input: TokenStream) -> TokenStream {
    let input_string = parse_macro_input!(input as LitStr).value();

    // Validate JSON format
    let json: NodeDefinition = match serde_json::from_str(&input_string) {
        Ok(json) => json,
        Err(e) => panic!("Invalid JSON input: {}", e),
    };

    // Convert the validated JSON back to a pretty-printed string
    let formatted_json = serde_json::to_string_pretty(&json).expect("Failed to serialize JSON");

    // Generate the output function
    let expanded = quote! {
        #[wasm_bindgen]
        pub fn get_definition() -> String {
            String::from(#formatted_json)
        }
    };

    // Convert the generated code back to a TokenStream
    TokenStream::from(expanded)
}

fn add_line_numbers(input: String) -> String {
    return input
        .split('\n')
        .enumerate()
        .map(|(i, line)| format!("{:2}: {}", i + 1, line))
        .collect::<Vec<String>>()
        .join("\n");
}

#[proc_macro]
pub fn include_definition_file(input: TokenStream) -> TokenStream {
    let file_path = syn::parse_macro_input!(input as syn::LitStr).value();

    // Retrieve the directory containing the Cargo.toml file
    let project_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let full_path = Path::new(&project_dir).join(&file_path);

    // Read the JSON file content
    let json_content = fs::read_to_string(full_path).unwrap_or_else(|err| {
        panic!(
            "Failed to read JSON file at '{}/{}': {}",
            project_dir, file_path, err
        )
    });

    // Optionally, validate that the content is valid JSON
    let _: NodeDefinition = serde_json::from_str(&json_content).unwrap_or_else(|err| {
        panic!(
            "JSON file contains invalid JSON: \n{} \n{}",
            err,
            add_line_numbers(json_content.clone())
        )
    });

    // Generate the function that returns the JSON string
    let expanded = quote! {
        #[wasm_bindgen]
        pub fn get_definition() -> String {
            String::from(#json_content)
        }
    };

    // Convert the generated code back to a TokenStream
    TokenStream::from(expanded)
}