Allow static generation of schema code

This commit is contained in:
Nadja Reitzenstein 2021-12-01 17:12:57 +01:00
parent 8c28e50cac
commit bedde0e19f
5 changed files with 61 additions and 11 deletions

View File

@ -6,6 +6,10 @@ build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
generated = []
gen_static = []
[dependencies]
capnp = "0.14.3"
capnpc = "0.14.4"
@ -13,4 +17,4 @@ capnpc = "0.14.4"
[build-dependencies]
capnpc = "0.14.4"
# Used in build.rs to iterate over all files in schema/
walkdir = "2.3.2"
walkdir = "2.3.2"

View File

@ -7,6 +7,7 @@ fn is_hidden(entry: &DirEntry) -> bool {
.unwrap_or(false)
}
#[cfg(not(feature = "gen_static"))]
fn main() {
let mut compile_command = ::capnpc::CompilerCommand::new();
compile_command
@ -34,3 +35,32 @@ fn main() {
println!("Compiling schemas...");
compile_command.run().expect("Failed to generate API code");
}
#[cfg(feature = "gen_static")]
fn main() {
let mut compile_command = ::capnpc::CompilerCommand::new();
compile_command
.src_prefix("schema")
.output_path("src/schema")
.default_parent_module(vec!["schema".to_string()]);
for entry in WalkDir::new("schema")
.max_depth(2)
.into_iter()
.filter_entry(|e| !is_hidden(e))
.filter_map(Result::ok) // Filter all entries that access failed on
.filter(|e| !e.file_type().is_dir()) // Filter directories
// Filter non-schema files
.filter(|e| e.file_name()
.to_str()
.map(|s| s.ends_with(".capnp"))
.unwrap_or(false)
)
{
println!("Collecting schema file {}", entry.path().display());
compile_command
.file(entry.path());
}
compile_command.run().expect("Failed to generate extra API code");
}

View File

@ -3,7 +3,7 @@
//!
//! This crate contains slightly nicer and better documented bindings for the FabAccess API.
#[allow(dead_code)]
pub mod schema;
/// Authentication subsystem

View File

@ -1,41 +1,57 @@
pub use capnpc::schema_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod auth_capnp {
include!(concat!(env!("OUT_DIR"), "/auth_capnp.rs"));
}
#[cfg(not(feature = "generated"))]
pub mod auth_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod main_capnp {
include!(concat!(env!("OUT_DIR"), "/main_capnp.rs"));
}
#[cfg(not(feature = "generated"))]
pub mod main_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod utils_capnp {
include!(concat!(env!("OUT_DIR"), "/utils_capnp.rs"));
}
#[cfg(not(feature = "generated"))]
pub mod utils_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod resource_capnp {
include!(concat!(env!("OUT_DIR"), "/resource_capnp.rs"));
}
#[cfg(not(feature = "generated"))]
pub mod resource_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod resources_capnp {
include!(concat!(env!("OUT_DIR"), "/resources_capnp.rs"));
}
#[cfg(not(feature = "generated"))]
pub mod resources_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod role_capnp {
include!(concat!(env!("OUT_DIR"), "/role_capnp.rs"));
}
#[cfg(not(feature = "generated"))]
pub mod role_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod user_capnp {
include!(concat!(env!("OUT_DIR"), "/user_capnp.rs"));
}
#[cfg(not(feature = "generated"))]
pub mod user_capnp;
#[allow(dead_code)]
#[cfg(feature = "generated")]
pub mod users_capnp {
include!(concat!(env!("OUT_DIR"), "/users_capnp.rs"));
}
}
#[cfg(not(feature = "generated"))]
pub mod users_capnp;

0
api/src/schema/.gitkeep Normal file
View File