use cratestack_core::Schema;
use cratestack_core::route_naming::{pluralize, to_snake_case};
use crate::diagnostics::{SchemaError, span_error};
const CLIENT_METHODS: &[&str] = &["new", "runtime", "procedures", "rpc", "batch"];
const PROCEDURES_CLIENT_METHODS: &[&str] = &["new"];
pub(super) fn validate_client_method_collisions(schema: &Schema) -> Result<(), SchemaError> {
for model in &schema.models {
let accessor = pluralize(&to_snake_case(&model.name));
if CLIENT_METHODS.contains(&accessor.as_str()) {
return Err(span_error(
format!(
"model `{model}` collides with the generated Rust client's own \
`{accessor}()` method — the per-model accessor is \
`pluralize(to_snake_case(\"{model}\"))`, which is `{accessor}`, and \
`cratestack_schema::client::Client` already defines a method of that \
name, so the generated `impl` block fails to compile as \
`error[E0592]: duplicate definitions with name `{accessor}``; rename \
the model",
model = model.name,
),
model.name_span,
));
}
}
for procedure in &schema.procedures {
let method = to_snake_case(&procedure.name);
if PROCEDURES_CLIENT_METHODS.contains(&method.as_str()) {
return Err(span_error(
format!(
"procedure `{procedure}` collides with the generated Rust client's own \
`{method}()` method — the per-procedure method is \
`to_snake_case(\"{procedure}\")`, which is `{method}`, and \
`cratestack_schema::client::ProceduresClient` already defines a method \
of that name, so the generated `impl` block fails to compile as \
`error[E0592]: duplicate definitions with name `{method}``; rename the \
procedure",
procedure = procedure.name,
),
procedure.name_span,
));
}
}
Ok(())
}