Skip to main content

hyprstream_rpc_derive/schema/
types.rs

1//! Type definitions for parsed Cap'n Proto schemas.
2//!
3//! Re-exported from `hyprstream_rpc_build::schema::types` — the canonical definitions
4//! live in the build crate so they can be shared between the proc-macro and the
5//! TypeScript codegen binary.
6
7pub use hyprstream_rpc_build::schema::types::*;
8
9/// Collect all struct names that need Data structs generated.
10pub fn collect_list_struct_types(schema: &ParsedSchema) -> Vec<String> {
11    let mut types = Vec::new();
12
13    let add_type = |type_name: &str, types: &mut Vec<String>| {
14        if type_name.starts_with("List(") {
15            let inner = &type_name[5..type_name.len() - 1];
16            if !is_primitive_capnp_type(inner)
17                && !types.contains(&inner.to_owned())
18                && schema.enums.iter().all(|e| e.name != inner)
19            {
20                types.push(inner.to_owned());
21            }
22        } else if !is_primitive_capnp_type(type_name)
23            && !type_name.starts_with("List(")
24            && schema.enums.iter().all(|e| e.name != type_name)
25            && schema.structs.iter().any(|s| s.name == type_name)
26            && !types.contains(&type_name.to_owned())
27        {
28            types.push(type_name.to_owned());
29        }
30    };
31
32    for v in &schema.response_variants {
33        add_type(&v.type_name, &mut types);
34    }
35    for sc in &schema.scoped_clients {
36        for v in &sc.inner_response_variants {
37            add_type(&v.type_name, &mut types);
38        }
39    }
40    for s in &schema.structs {
41        for f in s.non_union_fields() {
42            add_type(&f.type_name, &mut types);
43        }
44        // Union-arm payload types are referenced by the generated enum variants
45        // (e.g. `WorktreeResult(WorktreeResponse)`), so they must be generated too.
46        // The flat `non_union_fields` walk above does not cover them.
47        for arm in &s.union_arms {
48            match &arm.payload {
49                ArmPayload::Void => {}
50                ArmPayload::Type(name) => add_type(name, &mut types),
51                ArmPayload::Group(leaves) => {
52                    for leaf in leaves {
53                        add_type(&leaf.type_name, &mut types);
54                    }
55                }
56            }
57        }
58    }
59    for v in &schema.request_variants {
60        add_type(&v.type_name, &mut types);
61        if let Some(s) = schema.structs.iter().find(|s| s.name == v.type_name) {
62            for f in s.non_union_fields() {
63                add_type(&f.type_name, &mut types);
64            }
65        }
66    }
67    for sc in &schema.scoped_clients {
68        collect_from_scoped(sc, schema, &mut types);
69    }
70
71    types
72}
73
74fn collect_from_scoped(sc: &ScopedClient, schema: &ParsedSchema, types: &mut Vec<String>) {
75    let add_type = |type_name: &str, types: &mut Vec<String>| {
76        if type_name.starts_with("List(") {
77            let inner = &type_name[5..type_name.len() - 1];
78            if !is_primitive_capnp_type(inner)
79                && !types.contains(&inner.to_owned())
80                && schema.enums.iter().all(|e| e.name != inner)
81            {
82                types.push(inner.to_owned());
83            }
84        } else if !is_primitive_capnp_type(type_name)
85            && !type_name.starts_with("List(")
86            && schema.enums.iter().all(|e| e.name != type_name)
87            && schema.structs.iter().any(|s| s.name == type_name)
88            && !types.contains(&type_name.to_owned())
89        {
90            types.push(type_name.to_owned());
91        }
92    };
93    for v in &sc.inner_response_variants {
94        add_type(&v.type_name, types);
95    }
96    for v in &sc.inner_request_variants {
97        add_type(&v.type_name, types);
98        if let Some(s) = schema.structs.iter().find(|s| s.name == v.type_name) {
99            for f in s.non_union_fields() {
100                add_type(&f.type_name, types);
101            }
102        }
103    }
104    for nested in &sc.nested_clients {
105        collect_from_scoped(nested, schema, types);
106    }
107}