Skip to content

Commit a8f611e

Browse files
committed
refactor(server-macros): emit fixed AUTH_METADATA const per service
The previous trait-derived const name turned `OpenShell` into `OPEN_SHELL_AUTH_METADATA`, splitting the project name across an underscore. Each impl already lives in its own module (`crate::grpc::`, `crate::inference::`), so the module path is enough to disambiguate between services — a fixed `AUTH_METADATA` name reads more naturally. Aggregator in `auth/method_authz.rs` now references `crate::grpc::AUTH_METADATA` and `crate::inference::AUTH_METADATA` directly. Addresses review comment on PR #1596. Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
1 parent 03128df commit a8f611e

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

crates/openshell-server-macros/src/lib.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,12 @@ fn snake_to_pascal(ident: &str) -> String {
196196
out
197197
}
198198

199-
/// `OpenShell` → `OPEN_SHELL_AUTH_METADATA`.
200-
fn const_ident_from_trait(trait_ident: &Ident) -> Ident {
201-
let name = trait_ident.to_string();
202-
let mut buf = String::with_capacity(name.len() * 2);
203-
for (i, c) in name.chars().enumerate() {
204-
if c.is_ascii_uppercase() && i != 0 {
205-
buf.push('_');
206-
}
207-
buf.extend(c.to_uppercase());
208-
}
209-
buf.push_str("_AUTH_METADATA");
210-
Ident::new(&buf, trait_ident.span())
211-
}
199+
/// Name of the per-service const emitted alongside the impl block. The
200+
/// service module is what disambiguates between services — every impl
201+
/// lives in its own module (`crate::grpc::AUTH_METADATA`,
202+
/// `crate::inference::AUTH_METADATA`), so a fixed name reads more
203+
/// naturally than `OPEN_SHELL_AUTH_METADATA` / `INFERENCE_AUTH_METADATA`.
204+
const AUTH_METADATA_CONST: &str = "AUTH_METADATA";
212205

213206
fn trait_ident(item: &ItemImpl) -> Result<Ident> {
214207
let (_, path, _) = item.trait_.as_ref().ok_or_else(|| {
@@ -235,8 +228,10 @@ pub fn rpc_authz(args: TokenStream, item: TokenStream) -> TokenStream {
235228
}
236229

237230
fn expand(args: &AuthzArgs, item: &mut ItemImpl) -> Result<proc_macro2::TokenStream> {
231+
// `trait_ident` is still called for its validation side effect: the
232+
// macro must be applied to a trait impl (`impl Trait for Type`).
238233
let trait_ident = trait_ident(item)?;
239-
let const_name = const_ident_from_trait(&trait_ident);
234+
let const_name = Ident::new(AUTH_METADATA_CONST, trait_ident.span());
240235
let service = args.service.value();
241236

242237
let mut entries: Vec<proc_macro2::TokenStream> = Vec::new();

crates/openshell-server/src/auth/method_authz.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
//! consumed by `authz.rs` (role/scope), `oidc.rs` (unauthenticated
1010
//! check), and `sandbox_methods.rs` (sandbox principal allowlist).
1111
12-
use crate::grpc::OPEN_SHELL_AUTH_METADATA;
13-
use crate::inference::INFERENCE_AUTH_METADATA;
14-
1512
/// Per-method auth metadata emitted by `#[rpc_authz]`.
1613
///
1714
/// Built at compile time and looked up at request-dispatch time.
@@ -54,8 +51,10 @@ pub enum Role {
5451

5552
/// All per-service auth tables in one flat list.
5653
///
57-
/// Add a new service by appending its generated const here.
58-
const SERVICES: &[&[MethodAuth]] = &[OPEN_SHELL_AUTH_METADATA, INFERENCE_AUTH_METADATA];
54+
/// Add a new service by appending its module's `AUTH_METADATA` const here.
55+
/// The constant name is fixed by `#[rpc_authz]`; service disambiguation
56+
/// comes from the module path.
57+
const SERVICES: &[&[MethodAuth]] = &[crate::grpc::AUTH_METADATA, crate::inference::AUTH_METADATA];
5958

6059
/// Find the auth metadata for `method`, if any.
6160
#[must_use]

0 commit comments

Comments
 (0)