use proc_macro::TokenStream;
use syn::LitStr;
pub(super) fn guard_server_grpc_transport(
schema_path: &LitStr,
schema: &cratestack_core::Schema,
) -> Result<(), TokenStream> {
if !schema_declares_grpc_transport(schema) {
return Ok(());
}
if cfg!(feature = "grpc") {
return Ok(());
}
Err(TokenStream::from(
syn::Error::new(
schema_path.span(),
"schema declares `transport grpc`, but `cratestack-macros` was compiled without \
its `grpc` Cargo feature — enable it via \
`cratestack = { package = \"cratestack-pg\", features = [\"grpc\"] }` in your \
Cargo.toml. Without the feature, `cratestack generate-proto` can still emit this \
schema's `.proto` contract including its `service` block (tracking: \
https://github.com/cratestack/cratestack/issues/171).",
)
.to_compile_error(),
))
}
pub(super) fn guard_client_grpc_transport(
schema_path: &LitStr,
schema: &cratestack_core::Schema,
) -> Result<(), TokenStream> {
if !schema_declares_grpc_transport(schema) {
return Ok(());
}
if cfg!(feature = "grpc") {
return Ok(());
}
Err(TokenStream::from(
syn::Error::new(
schema_path.span(),
"schema declares `transport grpc`, but `cratestack-macros` was compiled without \
its `grpc` Cargo feature — enable it via \
`cratestack = { package = \"cratestack-pg\", features = [\"grpc\"] }` in your \
Cargo.toml. Without the feature, `cratestack generate-proto` can still emit this \
schema's `.proto` contract including its `service` block (tracking: \
https://github.com/cratestack/cratestack/issues/209).",
)
.to_compile_error(),
))
}
pub(super) fn guard_embedded_grpc_transport(
schema_path: &LitStr,
schema: &cratestack_core::Schema,
) -> Result<(), TokenStream> {
if !schema_declares_grpc_transport(schema) {
return Ok(());
}
Err(TokenStream::from(
syn::Error::new(
schema_path.span(),
"schema declares `transport grpc`, but `include_embedded_schema!` has no gRPC \
codegen and never will — the embedded role (rusqlite, no HTTP surface) has no \
transport at all, REST/RPC/RPC-batch included. `include_server_schema!` (behind \
its `grpc` Cargo feature, ticket #171) and `include_client_schema!` (behind the \
same feature, ticket #209) both support `transport grpc`; \
`include_embedded_schema!` does not and is not a place to add it. \
`cratestack generate-proto` can still emit this schema's `.proto` contract today \
for use with a non-CrateStack gRPC client.",
)
.to_compile_error(),
))
}
fn schema_declares_grpc_transport(schema: &cratestack_core::Schema) -> bool {
schema.transport == cratestack_core::TransportStyle::Grpc
}
#[cfg(test)]
mod tests {
use super::schema_declares_grpc_transport;
#[test]
fn flags_grpc_transport_schema() {
let schema = cratestack_parser::parse_schema(
r#"
transport grpc
model Widget {
id Int @id
}
"#,
)
.expect("schema should parse");
assert!(schema_declares_grpc_transport(&schema));
}
#[test]
fn does_not_flag_rest_transport_schema() {
let schema = cratestack_parser::parse_schema(
r#"
model Widget {
id Int @id
}
"#,
)
.expect("schema should parse");
assert!(!schema_declares_grpc_transport(&schema));
}
#[test]
fn does_not_flag_rpc_transport_schema() {
let schema = cratestack_parser::parse_schema(
r#"
transport rpc
model Widget {
id Int @id
}
"#,
)
.expect("schema should parse");
assert!(!schema_declares_grpc_transport(&schema));
}
}