Skip to content

Commit

Permalink
Deserialize extension_api.json and generate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Mar 22, 2022
1 parent 6859516 commit fbd1502
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 1,8 @@
.vscode
.idea
target/
Cargo.lock
Cargo.lock

# Input JSONs and code generation
/gdext-codegen/input/
/gdext-sys/src/gen/
4 changes: 2 additions & 2 deletions gdext-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 3,6 @@ name = "gdext-codegen"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
miniserde = "0.1.16"
quote = "1"
55 changes: 55 additions & 0 deletions gdext-codegen/src/api_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,55 @@
use miniserde::{json, Deserialize};
use quote::quote;
use std::path::Path;

// ----------------------------------------------------------------------------------------------------------------------------------------------
// JSON models

#[derive(Deserialize)]
struct ExtensionApi {
builtin_class_sizes: Vec<ClassSizes>,
}

#[derive(Deserialize)]
struct ClassSizes {
build_configuration: String,
sizes: Vec<ClassSize>,
}

#[derive(Deserialize)]
struct ClassSize {
name: String,
size: usize,
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Implementation

pub struct ApiParser {}

impl ApiParser {
pub fn generate_file() {
let tokens = quote! {
struct Constants {

}
};

Self::load_extension_api();

let string = tokens.to_string();

let _ = std::fs::create_dir("src/gen");
std::fs::write("src/gen/extensions.rs", string).expect("failed to write extension file");
}

fn load_extension_api() {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/input/extension_api.json");

let build_config = "float_32"; // TODO infer this

let json = std::fs::read_to_string(path).expect(&format!("failed to open file {:?}", path));
println!("JSON: {}", &json[0..30]);
let model: ExtensionApi = json::from_str(&json).expect("failed to deserialize JSON");
}
}
4 changes: 4 additions & 0 deletions gdext-codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
mod api_parser;

pub use api_parser::ApiParser;

#[cfg(test)]
mod tests {
#[test]
Expand Down
3 changes: 1 addition & 2 deletions gdext-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 8,6 @@ edition = "2021"
[features]
"real_is_double" = []

[dependencies]

[build-dependencies]
bindgen = "0.59"
gdext-codegen = { path = "../gdext-codegen" }
3 changes: 3 additions & 0 deletions gdext-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 1,4 @@
use gdext_codegen as gen;
use std::{env, path::PathBuf};

fn main() {
Expand All @@ -14,4 15,6 @@ fn main() {
bindings
.write_to_file(out_path.join("gdnative_interface.rs"))
.expect("could not write gdnative_interface Rust bindings!");

gen::ApiParser::generate_file();
}

0 comments on commit fbd1502

Please sign in to comment.