Skip to main content

metaxy_cli/parser/
extract.rs

1use std::fs;
2use std::path::Path;
3
4use anyhow::{Context, Result};
5use globset::{GlobBuilder, GlobSet, GlobSetBuilder};
6use syn::{Attribute, File, FnArg, Item, ItemFn, ReturnType};
7use walkdir::WalkDir;
8
9use super::serde as serde_attr;
10use super::types::{extract_rust_type, extract_struct_fields, extract_tuple_fields};
11use crate::config::InputConfig;
12use crate::model::{
13    EnumDef, EnumVariant, Manifest, Procedure, ProcedureKind, StructDef, VariantKind,
14};
15
16/// RPC attribute names recognized by the parser.
17const RPC_QUERY_ATTR: &str = "rpc_query";
18const RPC_MUTATION_ATTR: &str = "rpc_mutation";
19
20/// Builds a `GlobSet` from a list of glob pattern strings.
21fn build_glob_set(patterns: &[String]) -> Result<GlobSet> {
22    let mut builder = GlobSetBuilder::new();
23    for pattern in patterns {
24        let glob = GlobBuilder::new(pattern)
25            .literal_separator(false)
26            .build()
27            .with_context(|| format!("Invalid glob pattern: {pattern}"))?;
28        builder.add(glob);
29    }
30    builder.build().context("Failed to build glob set")
31}
32
33/// Scans `.rs` files in the configured directory and extracts RPC metadata.
34///
35/// Walks the directory recursively, applying `include`/`exclude` glob patterns
36/// from the config, then parsing each matching Rust source file for
37/// `#[rpc_query]` / `#[rpc_mutation]` annotated functions and `#[derive(Serialize)]` structs.
38pub fn scan_directory(input: &InputConfig) -> Result<Manifest> {
39    let mut manifest = Manifest::default();
40
41    let include_set = build_glob_set(&input.include)?;
42    let exclude_set = build_glob_set(&input.exclude)?;
43
44    let mut file_count = 0;
45    for entry in WalkDir::new(&input.dir)
46        .into_iter()
47        // Skip unreadable entries (e.g. permission denied); the scan should
48        // not abort because a single directory entry is inaccessible.
49        .filter_map(|e| e.ok())
50        .filter(|e| {
51            if e.path().extension().is_none_or(|ext| ext != "rs") {
52                return false;
53            }
54            let rel = e.path().strip_prefix(&input.dir).unwrap_or(e.path());
55            include_set.is_match(rel) && !exclude_set.is_match(rel)
56        })
57    {
58        file_count += 1;
59        let path = entry.path();
60        let file_manifest =
61            parse_file(path).with_context(|| format!("Failed to parse {}", path.display()))?;
62
63        manifest.procedures.extend(file_manifest.procedures);
64        manifest.structs.extend(file_manifest.structs);
65        manifest.enums.extend(file_manifest.enums);
66    }
67
68    if file_count == 0 {
69        anyhow::bail!("No .rs files found in {}", input.dir.display());
70    }
71
72    // Sort for deterministic output
73    manifest.procedures.sort_by(|a, b| a.name.cmp(&b.name));
74    manifest.structs.sort_by(|a, b| a.name.cmp(&b.name));
75    manifest.enums.sort_by(|a, b| a.name.cmp(&b.name));
76
77    Ok(manifest)
78}
79
80/// Parses a single Rust source file and extracts all RPC procedures and struct definitions.
81pub fn parse_file(path: &Path) -> Result<Manifest> {
82    let source =
83        fs::read_to_string(path).with_context(|| format!("Cannot read {}", path.display()))?;
84
85    let syntax: File =
86        syn::parse_file(&source).with_context(|| format!("Syntax error in {}", path.display()))?;
87
88    let mut manifest = Manifest::default();
89
90    for item in &syntax.items {
91        match item {
92            Item::Fn(func) => {
93                if let Some(procedure) = try_extract_procedure(func, path) {
94                    manifest.procedures.push(procedure);
95                }
96            }
97            Item::Struct(item_struct) => {
98                if has_serde_derive(&item_struct.attrs) {
99                    let generics = extract_generic_param_names(&item_struct.generics);
100                    let tuple_fields = extract_tuple_fields(&item_struct.fields);
101                    let fields = if tuple_fields.is_empty() {
102                        extract_struct_fields(&item_struct.fields)
103                    } else {
104                        vec![]
105                    };
106                    let docs = extract_docs(&item_struct.attrs);
107                    let rename_all = serde_attr::parse_rename_all(&item_struct.attrs);
108                    manifest.structs.push(StructDef {
109                        name: item_struct.ident.to_string(),
110                        generics,
111                        fields,
112                        tuple_fields,
113                        source_file: path.to_path_buf(),
114                        docs,
115                        rename_all,
116                    });
117                }
118            }
119            Item::Enum(item_enum) => {
120                if has_serde_derive(&item_enum.attrs) {
121                    let generics = extract_generic_param_names(&item_enum.generics);
122                    let rename_all = serde_attr::parse_rename_all(&item_enum.attrs);
123                    let tagging = serde_attr::parse_enum_tagging(&item_enum.attrs);
124                    let variants = extract_enum_variants(item_enum);
125                    let docs = extract_docs(&item_enum.attrs);
126                    manifest.enums.push(EnumDef {
127                        name: item_enum.ident.to_string(),
128                        generics,
129                        variants,
130                        source_file: path.to_path_buf(),
131                        docs,
132                        rename_all,
133                        tagging,
134                    });
135                }
136            }
137            _ => {}
138        }
139    }
140
141    Ok(manifest)
142}
143
144/// Extracts doc comments from `#[doc = "..."]` attributes (written as `///` in source).
145///
146/// Returns `None` if no doc comments are present.
147fn extract_docs(attrs: &[Attribute]) -> Option<String> {
148    let lines: Vec<String> = attrs
149        .iter()
150        .filter_map(|attr| {
151            if !attr.path().is_ident("doc") {
152                return None;
153            }
154            if let syn::Meta::NameValue(nv) = &attr.meta
155                && let syn::Expr::Lit(syn::ExprLit {
156                    lit: syn::Lit::Str(s),
157                    ..
158                }) = &nv.value
159            {
160                let text = s.value();
161                // `///` comments produce a leading space, strip it
162                return Some(text.strip_prefix(' ').unwrap_or(&text).to_string());
163            }
164            None
165        })
166        .collect();
167
168    if lines.is_empty() {
169        None
170    } else {
171        Some(lines.join("\n"))
172    }
173}
174
175/// Attempts to extract an RPC procedure from a function item.
176/// Returns `None` if the function doesn't have an RPC attribute.
177fn try_extract_procedure(func: &ItemFn, path: &Path) -> Option<Procedure> {
178    let kind = detect_rpc_kind(&func.attrs)?;
179    let name = func.sig.ident.to_string();
180    let docs = extract_docs(&func.attrs);
181
182    let input = func.sig.inputs.iter().find_map(|arg| {
183        let FnArg::Typed(pat) = arg else { return None };
184        // Skip the Headers parameter — it's not part of the RPC input.
185        if is_headers_type(&pat.ty) {
186            return None;
187        }
188        Some(extract_rust_type(&pat.ty))
189    });
190
191    let output = match &func.sig.output {
192        ReturnType::Default => None,
193        ReturnType::Type(_, ty) => {
194            let rust_type = extract_rust_type(ty);
195            // Unwrap Result<T, _> to just T
196            if rust_type.name == "Result" && !rust_type.generics.is_empty() {
197                rust_type.generics.into_iter().next()
198            } else {
199                Some(rust_type)
200            }
201        }
202    };
203
204    let timeout_ms = extract_timeout_ms(&func.attrs);
205    let idempotent = extract_idempotent(&func.attrs);
206
207    Some(Procedure {
208        name,
209        kind,
210        input,
211        output,
212        source_file: path.to_path_buf(),
213        docs,
214        timeout_ms,
215        idempotent,
216    })
217}
218
219/// Checks function attributes for `#[rpc_query]` or `#[rpc_mutation]`.
220fn detect_rpc_kind(attrs: &[Attribute]) -> Option<ProcedureKind> {
221    for attr in attrs {
222        if attr.path().is_ident(RPC_QUERY_ATTR) {
223            return Some(ProcedureKind::Query);
224        }
225        if attr.path().is_ident(RPC_MUTATION_ATTR) {
226            return Some(ProcedureKind::Mutation);
227        }
228    }
229    None
230}
231
232/// Extracts generic type parameter names from `syn::Generics`.
233///
234/// Only type parameters are extracted; lifetimes and const generics are skipped.
235fn extract_generic_param_names(generics: &syn::Generics) -> Vec<String> {
236    generics
237        .params
238        .iter()
239        .filter_map(|p| match p {
240            syn::GenericParam::Type(t) => Some(t.ident.to_string()),
241            _ => None,
242        })
243        .collect()
244}
245
246/// Extracts variants from a Rust enum into `EnumVariant` representations.
247fn extract_enum_variants(item_enum: &syn::ItemEnum) -> Vec<EnumVariant> {
248    item_enum
249        .variants
250        .iter()
251        .map(|v| {
252            let name = v.ident.to_string();
253            let rename = serde_attr::parse_rename(&v.attrs);
254            let kind = match &v.fields {
255                syn::Fields::Unit => VariantKind::Unit,
256                syn::Fields::Unnamed(fields) => {
257                    let types = fields
258                        .unnamed
259                        .iter()
260                        .map(|f| extract_rust_type(&f.ty))
261                        .collect();
262                    VariantKind::Tuple(types)
263                }
264                syn::Fields::Named(_) => {
265                    let fields = extract_struct_fields(&v.fields);
266                    VariantKind::Struct(fields)
267                }
268            };
269            EnumVariant { name, kind, rename }
270        })
271        .collect()
272}
273
274/// Returns `true` if the type path ends with `Headers` (e.g. `Headers`, `metaxy::Headers`).
275///
276/// Used to skip the `Headers` parameter when extracting RPC input types,
277/// since it carries request metadata rather than user-provided input.
278fn is_headers_type(ty: &syn::Type) -> bool {
279    if let syn::Type::Path(type_path) = ty
280        && let Some(segment) = type_path.path.segments.last()
281    {
282        return segment.ident == "Headers";
283    }
284    false
285}
286
287/// Extracts the `timeout` value from `#[rpc_query(timeout = "30s")]` or `#[rpc_mutation(timeout = "30s")]`.
288///
289/// Returns `Some(milliseconds)` if a valid timeout is found, `None` otherwise.
290/// Uses `Punctuated<Meta>` to handle mixed bare flags (e.g. `idempotent`) alongside key-value pairs.
291fn extract_timeout_ms(attrs: &[Attribute]) -> Option<u64> {
292    for attr in attrs {
293        if !attr.path().is_ident(RPC_QUERY_ATTR) && !attr.path().is_ident(RPC_MUTATION_ATTR) {
294            continue;
295        }
296        let Ok(parsed) = attr.parse_args_with(
297            syn::punctuated::Punctuated::<syn::Meta, syn::Token![,]>::parse_terminated,
298        ) else {
299            continue;
300        };
301        for meta in &parsed {
302            if let syn::Meta::NameValue(nv) = meta
303                && nv.path.is_ident("timeout")
304                && let syn::Expr::Lit(syn::ExprLit {
305                    lit: syn::Lit::Str(s),
306                    ..
307                }) = &nv.value
308            {
309                return parse_duration_to_ms(&s.value());
310            }
311        }
312    }
313    None
314}
315
316/// Parses a human-readable duration shorthand into milliseconds.
317///
318/// Lenient: returns `None` on any parse error instead of failing the scan.
319fn parse_duration_to_ms(s: &str) -> Option<u64> {
320    let (num_str, multiplier) = if let Some(n) = s.strip_suffix('s') {
321        (n, 1_000)
322    } else if let Some(n) = s.strip_suffix('m') {
323        (n, 60_000)
324    } else if let Some(n) = s.strip_suffix('h') {
325        (n, 3_600_000)
326    } else if let Some(n) = s.strip_suffix('d') {
327        (n, 86_400_000)
328    } else {
329        return None;
330    };
331    let num: u64 = num_str.parse().ok()?;
332    if num == 0 {
333        return None;
334    }
335    Some(num * multiplier)
336}
337
338/// Extracts the bare `idempotent` flag from `#[rpc_mutation(idempotent)]`.
339///
340/// Only checks `RPC_MUTATION_ATTR` attributes. Lenient: silently ignores
341/// `rpc_query(idempotent)` (the proc macro rejects it at compile time).
342fn extract_idempotent(attrs: &[Attribute]) -> bool {
343    for attr in attrs {
344        if !attr.path().is_ident(RPC_MUTATION_ATTR) {
345            continue;
346        }
347        let Ok(parsed) = attr.parse_args_with(
348            syn::punctuated::Punctuated::<syn::Meta, syn::Token![,]>::parse_terminated,
349        ) else {
350            continue;
351        };
352        for meta in &parsed {
353            if let syn::Meta::Path(path) = meta
354                && path.is_ident("idempotent")
355            {
356                return true;
357            }
358        }
359    }
360    false
361}
362
363/// Checks if a struct has `#[derive(Serialize)]` or `#[derive(serde::Serialize)]`.
364fn has_serde_derive(attrs: &[Attribute]) -> bool {
365    attrs.iter().any(|attr| {
366        if !attr.path().is_ident("derive") {
367            return false;
368        }
369        attr.parse_args_with(
370            syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated,
371        )
372        .is_ok_and(|nested| {
373            nested.iter().any(|path| {
374                path.is_ident("Serialize")
375                    || path.segments.last().is_some_and(|s| s.ident == "Serialize")
376            })
377        })
378    })
379}